diff options
-rw-r--r-- | src/commands/utilities/highlight-block.ts | 8 | ||||
-rw-r--r-- | src/commands/utilities/highlight-unblock.ts | 8 | ||||
-rw-r--r-- | src/lib/common/HighlightManager.ts | 104 |
3 files changed, 69 insertions, 51 deletions
diff --git a/src/commands/utilities/highlight-block.ts b/src/commands/utilities/highlight-block.ts index 0240da4..a450d71 100644 --- a/src/commands/utilities/highlight-block.ts +++ b/src/commands/utilities/highlight-block.ts @@ -2,7 +2,7 @@ import { AllowedMentions, BushCommand, emojis, type ArgType, type CommandMessage import assert from 'assert/strict'; import { Argument, ArgumentGeneratorReturn } from 'discord-akairo'; import { BaseChannel, GuildMember, User } from 'discord.js'; -import { BlockResult } from '../../lib/common/HighlightManager.js'; +import { HighlightBlockResult } from '../../lib/common/HighlightManager.js'; import { highlightSubcommands } from './highlight-!.js'; export default class HighlightBlockCommand extends BushCommand { @@ -51,11 +51,11 @@ export default class HighlightBlockCommand extends BushCommand { /* eslint-disable @typescript-eslint/no-base-to-string */ const content = (() => { switch (res) { - case BlockResult.ALREADY_BLOCKED: + case HighlightBlockResult.ALREADY_BLOCKED: return `${emojis.error} You have already blocked ${args.target}.`; - case BlockResult.ERROR: + case HighlightBlockResult.ERROR: return `${emojis.error} An error occurred while blocking ${args.target}.`; - case BlockResult.SUCCESS: + case HighlightBlockResult.SUCCESS: return `${emojis.success} Successfully blocked ${args.target} from triggering your highlights.`; } })(); diff --git a/src/commands/utilities/highlight-unblock.ts b/src/commands/utilities/highlight-unblock.ts index 2e9dd73..702fa65 100644 --- a/src/commands/utilities/highlight-unblock.ts +++ b/src/commands/utilities/highlight-unblock.ts @@ -2,7 +2,7 @@ import { AllowedMentions, BushCommand, emojis, type ArgType, type CommandMessage import assert from 'assert'; import { Argument, ArgumentGeneratorReturn } from 'discord-akairo'; import { BaseChannel, GuildMember, User } from 'discord.js'; -import { UnblockResult } from '../../lib/common/HighlightManager.js'; +import { HighlightUnblockResult } from '../../lib/common/HighlightManager.js'; import { highlightSubcommands } from './highlight-!.js'; export default class HighlightUnblockCommand extends BushCommand { @@ -51,11 +51,11 @@ export default class HighlightUnblockCommand extends BushCommand { /* eslint-disable @typescript-eslint/no-base-to-string */ const content = (() => { switch (res) { - case UnblockResult.NOT_BLOCKED: + case HighlightUnblockResult.NOT_BLOCKED: return `${emojis.error} ${args.target} is not blocked so cannot be unblock.`; - case UnblockResult.ERROR: + case HighlightUnblockResult.ERROR: return `${emojis.error} An error occurred while unblocking ${args.target}.`; - case UnblockResult.SUCCESS: + case HighlightUnblockResult.SUCCESS: return `${emojis.success} Successfully allowed ${args.target} to trigger your highlights.`; } })(); 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<BlockResult> { + public async addBlock( + guild: Snowflake, + user: Snowflake, + target: GuildMember | TextBasedChannel + ): Promise<HighlightBlockResult> { 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<UnblockResult> { + public async removeBlock(guild: Snowflake, user: Snowflake, target: GuildMember | Channel): Promise<HighlightUnblockResult> { 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 |