From e301238dd9a7de5c43870c438245f7a9900696b8 Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Sat, 13 Aug 2022 15:35:37 -0400 Subject: check if a user is offline when deciding whether to delay hl --- src/lib/common/HighlightManager.ts | 104 ++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 43 deletions(-) (limited to 'src/lib') diff --git a/src/lib/common/HighlightManager.ts b/src/lib/common/HighlightManager.ts index 8784b35..b3f12ac 100644 --- a/src/lib/common/HighlightManager.ts +++ b/src/lib/common/HighlightManager.ts @@ -97,40 +97,37 @@ export class HighlightManager { const guildCache = this.guildHighlights.get(message.guildId)!; for (const [word, users] of guildCache.entries()) { - if (this.isMatch(message.content, word)) { - for (const user of users) { - if (!ret.has(user)) { - if (!message.channel.permissionsFor(user)?.has('ViewChannel')) continue; - - const blockedUsers = this.userBlocks.get(message.guildId)?.get(user) ?? new Set(); - if (blockedUsers.has(message.author.id)) { - void this.client.console.verbose( - 'Highlight', - `Highlight ignored because <<${user}>> blocked the user <<${message.author.id}>>` - ); - continue; - } - - const blockedChannels = this.channelBlocks.get(message.guildId)?.get(user) ?? new Set(); - if (blockedChannels.has(message.channel.id)) { - void this.client.console.verbose( - 'Highlight', - `Highlight ignored because <<${user}>> blocked the channel <<${message.channel.id}>>` - ); - continue; - } - - if (message.mentions.has(user)) { - void this.client.console.verbose( - 'Highlight', - `Highlight ignored because <<${user}>> is already mentioned in the message.` - ); - continue; - } - - ret.set(user, word); - } + if (!this.isMatch(message.content, word)) continue; + + for (const user of users) { + if (ret.has(user)) continue; + + if (!message.channel.permissionsFor(user)?.has('ViewChannel')) continue; + + const blockedUsers = this.userBlocks.get(message.guildId)?.get(user) ?? new Set(); + if (blockedUsers.has(message.author.id)) { + void this.client.console.verbose( + 'Highlight', + `Highlight ignored because <<${user}>> blocked the user <<${message.author.id}>>` + ); + continue; + } + const blockedChannels = this.channelBlocks.get(message.guildId)?.get(user) ?? new Set(); + if (blockedChannels.has(message.channel.id)) { + void this.client.console.verbose( + 'Highlight', + `Highlight ignored because <<${user}>> blocked the channel <<${message.channel.id}>>` + ); + continue; + } + if (message.mentions.has(user)) { + void this.client.console.verbose( + 'Highlight', + `Highlight ignored because <<${user}>> is already mentioned in the message.` + ); + continue; } + ret.set(user, word); } } @@ -258,25 +255,29 @@ export class HighlightManager { * @param target The target that is being blocked. * @returns The result of the operation. */ - public async addBlock(guild: Snowflake, user: Snowflake, target: GuildMember | TextBasedChannel): Promise { + public async addBlock( + guild: Snowflake, + user: Snowflake, + target: GuildMember | TextBasedChannel + ): Promise { const cacheKey = `${target instanceof GuildMember ? 'user' : 'channel'}Blocks` as const; const databaseKey = `blacklisted${target instanceof GuildMember ? 'Users' : 'Channels'}` as const; const [highlight] = await Highlight.findOrCreate({ where: { guild, user } }); - if (highlight[databaseKey].includes(target.id)) return BlockResult.ALREADY_BLOCKED; + if (highlight[databaseKey].includes(target.id)) return HighlightBlockResult.ALREADY_BLOCKED; const newBlocks = addToArray(highlight[databaseKey], target.id); highlight[databaseKey] = newBlocks; const res = await highlight.save().catch(() => false); - if (!res) return BlockResult.ERROR; + if (!res) return HighlightBlockResult.ERROR; if (!this[cacheKey].has(guild)) this[cacheKey].set(guild, new Collection()); const guildBlocks = this[cacheKey].get(guild)!; guildBlocks.set(user, new Set(newBlocks)); - return BlockResult.SUCCESS; + return HighlightBlockResult.SUCCESS; } /** @@ -286,25 +287,25 @@ export class HighlightManager { * @param target The target that is being unblocked. * @returns The result of the operation. */ - public async removeBlock(guild: Snowflake, user: Snowflake, target: GuildMember | Channel): Promise { + public async removeBlock(guild: Snowflake, user: Snowflake, target: GuildMember | Channel): Promise { const cacheKey = `${target instanceof GuildMember ? 'user' : 'channel'}Blocks` as const; const databaseKey = `blacklisted${target instanceof GuildMember ? 'Users' : 'Channels'}` as const; const [highlight] = await Highlight.findOrCreate({ where: { guild, user } }); - if (!highlight[databaseKey].includes(target.id)) return UnblockResult.NOT_BLOCKED; + if (!highlight[databaseKey].includes(target.id)) return HighlightUnblockResult.NOT_BLOCKED; const newBlocks = removeFromArray(highlight[databaseKey], target.id); highlight[databaseKey] = newBlocks; const res = await highlight.save().catch(() => false); - if (!res) return UnblockResult.ERROR; + if (!res) return HighlightUnblockResult.ERROR; if (!this[cacheKey].has(guild)) this[cacheKey].set(guild, new Collection()); const guildBlocks = this[cacheKey].get(guild)!; guildBlocks.set(user, new Set(newBlocks)); - return UnblockResult.SUCCESS; + return HighlightUnblockResult.SUCCESS; } /** @@ -333,6 +334,23 @@ export class HighlightManager { const lastTalked = this.userLastTalkedCooldown.get(message.guildId)?.get(user); if (!lastTalked) break talkCooldown; + presence: { + // incase the bot left the guild + if (message.client.guilds.cache.has(message.guildId)) { + const guild = message.client.guilds.cache.get(message.guildId)!; + const member = guild.members.cache.get(user); + if (!member) break presence; + + const presence = member.presence; + if (!presence) break presence; + + if (presence.status === 'offline') { + void message.client.console.verbose('Highlight', `User <<${user}>> is offline.`); + break talkCooldown; + } + } + } + const now = new Date().getTime(); const talked = lastTalked.getTime(); @@ -400,13 +418,13 @@ export class HighlightManager { } } -export enum BlockResult { +export enum HighlightBlockResult { ALREADY_BLOCKED, ERROR, SUCCESS } -export enum UnblockResult { +export enum HighlightUnblockResult { NOT_BLOCKED, ERROR, SUCCESS -- cgit