diff options
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/utilities/highlight-!.ts | 6 | ||||
-rw-r--r-- | src/commands/utilities/highlight-add.ts | 61 | ||||
-rw-r--r-- | src/commands/utilities/highlight-block.ts | 11 | ||||
-rw-r--r-- | src/commands/utilities/highlight-clear.ts | 13 | ||||
-rw-r--r-- | src/commands/utilities/highlight-matches.ts | 56 | ||||
-rw-r--r-- | src/commands/utilities/highlight-remove.ts | 46 | ||||
-rw-r--r-- | src/commands/utilities/highlight-show.ts | 33 |
7 files changed, 153 insertions, 73 deletions
diff --git a/src/commands/utilities/highlight-!.ts b/src/commands/utilities/highlight-!.ts index 687990f..dd6da8d 100644 --- a/src/commands/utilities/highlight-!.ts +++ b/src/commands/utilities/highlight-!.ts @@ -40,7 +40,7 @@ export const highlightCommandArgs: { name: 'target', description: 'What user/channel would you like to prevent from triggering your highlights?', retry: '{error} Enter a valid user or channel.', - type: ApplicationCommandOptionType.Mentionable, + type: ApplicationCommandOptionType.String, required: true } ], @@ -49,7 +49,7 @@ export const highlightCommandArgs: { name: 'target', description: 'What user/channel would you like to allow triggering your highlights again?', retry: '{error} Enter a valid user or channel.', - type: ApplicationCommandOptionType.Mentionable, + type: ApplicationCommandOptionType.String, required: true } ], @@ -105,7 +105,7 @@ export default class HighlightCommand extends BushCommand { channel: 'guild', clientPermissions: (m) => util.clientSendAndPermCheck(m), userPermissions: [], - ownerOnly: true + superUserOnly: true }); } diff --git a/src/commands/utilities/highlight-add.ts b/src/commands/utilities/highlight-add.ts index 316a931..9c7506e 100644 --- a/src/commands/utilities/highlight-add.ts +++ b/src/commands/utilities/highlight-add.ts @@ -1,6 +1,5 @@ -import { AllowedMentions, BushCommand, Highlight, type ArgType, type BushMessage, type BushSlashMessage } from '#lib'; +import { AllowedMentions, BushCommand, type ArgType, type BushMessage, type BushSlashMessage } from '#lib'; import assert from 'assert'; -import { ArgumentGeneratorReturn } from 'discord-akairo'; import { highlightCommandArgs, highlightSubcommands } from './highlight-!.js'; export default class HighlightAddCommand extends BushCommand { @@ -9,6 +8,28 @@ export default class HighlightAddCommand extends BushCommand { aliases: [], category: 'utilities', description: highlightSubcommands.add, + args: [ + { + id: 'word', + description: highlightCommandArgs.add[0].description, + type: 'string', + match: 'rest', + prompt: highlightCommandArgs.add[0].description, + retry: highlightCommandArgs.add[0].retry, + optional: !highlightCommandArgs.add[0].required, + only: 'text', + slashType: false + }, + { + id: 'regex', + description: highlightCommandArgs.add[1].description, + match: 'flag', + flag: '--regex', + prompt: highlightCommandArgs.add[1].description, + only: 'text', + slashType: false + } + ], usage: [], examples: [], clientPermissions: [], @@ -16,25 +37,6 @@ export default class HighlightAddCommand extends BushCommand { }); } - public override *args(): ArgumentGeneratorReturn { - const word: ArgType<'string'> = yield { - type: 'string', - match: 'rest', - prompt: { - start: highlightCommandArgs.add[0].description, - retry: highlightCommandArgs.add[0].retry, - optional: !highlightCommandArgs.add[0].required - } - }; - - const regex: boolean = yield { - match: 'flag', - flag: 'regex' - }; - - return { word, regex }; - } - public override async exec( message: BushMessage | BushSlashMessage, args: { word: ArgType<'string'>; regex: ArgType<'boolean'> } @@ -58,22 +60,19 @@ export default class HighlightAddCommand extends BushCommand { } } - const [highlight] = await Highlight.findOrCreate({ - where: { - guild: message.guild.id, - user: message.author.id - } + const res = await client.highlightManager.addHighlight(message.guild.id, message.author.id, { + word: args.word, + regex: args.regex }); - if (highlight.words.some((w) => w.word === args.word)) + if (typeof res === 'string') + return await message.util.reply({ content: `${util.emojis.error} ${res}`, allowedMentions: AllowedMentions.none() }); + else if (!res) return await message.util.reply({ - content: `${util.emojis.error} You have already highlighted "${args.word}".`, + content: `${util.emojis.error} There was an error highlighting "${args.word}".`, allowedMentions: AllowedMentions.none() }); - highlight.words = util.addToArray(highlight.words, { word: args.word, regex: args.regex }); - await highlight.save(); - return await message.util.reply({ content: `${util.emojis.success} Successfully added "${args.word}" to your highlight list.`, allowedMentions: AllowedMentions.none() diff --git a/src/commands/utilities/highlight-block.ts b/src/commands/utilities/highlight-block.ts index f546958..efc5437 100644 --- a/src/commands/utilities/highlight-block.ts +++ b/src/commands/utilities/highlight-block.ts @@ -33,11 +33,16 @@ export default class HighlightBlockCommand extends BushCommand { public override async exec( message: BushMessage | BushSlashMessage, - args: { target: ArgType<'user'> | ArgType<'role'> | ArgType<'member'> } + args: { target: string | ArgType<'member'> | ArgType<'channel'> } ) { assert(message.inGuild()); - if (!(args.target instanceof GuildMember || args.target instanceof Channel)) + args.target = + typeof args.target === 'string' + ? (await util.arg.cast(util.arg.union('member', 'channel'), message, args.target))! + : args.target; + + if (!args.target || !(args.target instanceof GuildMember || args.target instanceof Channel)) return await message.util.reply(`${util.emojis.error} You can only block users or channels.`); if (args.target instanceof Channel && !args.target.isTextBased()) @@ -54,6 +59,7 @@ export default class HighlightBlockCommand extends BushCommand { if (highlight[key].includes(args.target.id)) return await message.util.reply({ + // eslint-disable-next-line @typescript-eslint/no-base-to-string content: `${util.emojis.error} You have already blocked ${args.target}.`, allowedMentions: AllowedMentions.none() }); @@ -62,6 +68,7 @@ export default class HighlightBlockCommand extends BushCommand { await highlight.save(); return await message.util.reply({ + // eslint-disable-next-line @typescript-eslint/no-base-to-string content: `${util.emojis.success} Successfully blocked ${args.target} from triggering your highlights.`, allowedMentions: AllowedMentions.none() }); diff --git a/src/commands/utilities/highlight-clear.ts b/src/commands/utilities/highlight-clear.ts index e1cce2d..33893b9 100644 --- a/src/commands/utilities/highlight-clear.ts +++ b/src/commands/utilities/highlight-clear.ts @@ -1,4 +1,4 @@ -import { AllowedMentions, BushCommand, ConfirmationPrompt, Highlight, type BushMessage, type BushSlashMessage } from '#lib'; +import { AllowedMentions, BushCommand, ConfirmationPrompt, type BushMessage, type BushSlashMessage } from '#lib'; import assert from 'assert'; import { highlightSubcommands } from './highlight-!.js'; @@ -18,18 +18,13 @@ export default class HighlightClearCommand extends BushCommand { public override async exec(message: BushMessage | BushSlashMessage) { assert(message.inGuild()); - const [highlight] = await Highlight.findOrCreate({ - where: { - guild: message.guild.id, - user: message.author.id - } - }); + if (message.util.isSlashMessage(message)) await message.interaction.deferReply(); const confirm = await ConfirmationPrompt.send(message, { content: `Are you sure you want to clear your highlight list?` }); if (!confirm) return await message.util.reply(`${util.emojis.warn} You decided not to clear your highlight list.`); - highlight.words = []; - await highlight.save(); + const success = await client.highlightManager.removeAllHighlights(message.author.id, message.guild.id); + if (!success) return await message.util.reply(`${util.emojis.error} There was an error clearing your highlight list.`); return await message.util.reply({ content: `${util.emojis.success} Successfully cleared your highlight list.`, diff --git a/src/commands/utilities/highlight-matches.ts b/src/commands/utilities/highlight-matches.ts index e69de29..aa8308c 100644 --- a/src/commands/utilities/highlight-matches.ts +++ b/src/commands/utilities/highlight-matches.ts @@ -0,0 +1,56 @@ +import { BushCommand, ButtonPaginator, type ArgType, type BushMessage, type BushSlashMessage } from '#lib'; +import assert from 'assert'; +import { ArgumentGeneratorReturn } from 'discord-akairo'; +import { APIEmbed } from 'discord-api-types/v9'; +import { highlightCommandArgs, highlightSubcommands } from './highlight-!.js'; + +export default class HighlightMatchesCommand extends BushCommand { + public constructor() { + super('highlight-matches', { + aliases: [], + category: 'utilities', + description: highlightSubcommands.matches, + usage: [], + examples: [], + clientPermissions: [], + userPermissions: [] + }); + } + + public override *args(): ArgumentGeneratorReturn { + const phrase: ArgType<'string'> = yield { + type: 'string', + match: 'rest', + prompt: { + start: highlightCommandArgs.matches[0].description, + retry: highlightCommandArgs.matches[0].retry, + optional: !highlightCommandArgs.matches[0].required + } + }; + + return { phrase }; + } + + public override async exec(message: BushMessage | BushSlashMessage, args: { phrase: ArgType<'string'> }) { + assert(message.inGuild()); + + const res = await client.highlightManager.checkPhrase(message.guild.id, message.author.id, args.phrase); + + if (!res.size) return await message.util.reply(`${util.emojis.error} You are not highlighting any words`); + + const lines = res.map( + (passed, hl) => `${passed ? util.emojis.check : util.emojis.cross} ${hl.regex ? `/${hl.word}/gi` : hl.word}` + ); + const chunked = util.chunk(lines, 10); + + const pages = chunked.map( + (chunk): APIEmbed => ({ + title: `Matches`, + description: chunk.join('\n'), + color: util.colors.default + }) + ); + + return pages.length > 1 ? await ButtonPaginator.send(message, pages) : message.util.send({ embeds: pages }); + } +} diff --git a/src/commands/utilities/highlight-remove.ts b/src/commands/utilities/highlight-remove.ts index 1f7c4c0..7e8c416 100644 --- a/src/commands/utilities/highlight-remove.ts +++ b/src/commands/utilities/highlight-remove.ts @@ -1,6 +1,5 @@ -import { AllowedMentions, BushCommand, Highlight, type ArgType, type BushMessage, type BushSlashMessage } from '#lib'; +import { AllowedMentions, BushCommand, type ArgType, type BushMessage, type BushSlashMessage } from '#lib'; import assert from 'assert'; -import { ArgumentGeneratorReturn } from 'discord-akairo'; import { highlightCommandArgs, highlightSubcommands } from './highlight-!.js'; export default class HighlightRemoveCommand extends BushCommand { @@ -9,6 +8,19 @@ export default class HighlightRemoveCommand extends BushCommand { aliases: [], category: 'utilities', description: highlightSubcommands.remove, + args: [ + { + id: 'word', + description: highlightCommandArgs.remove[0].description, + type: 'string', + match: 'rest', + prompt: highlightCommandArgs.remove[0].description, + retry: highlightCommandArgs.remove[0].retry, + optional: !highlightCommandArgs.remove[0].required, + only: 'text', + slashType: false + } + ], usage: [], examples: [], clientPermissions: [], @@ -16,39 +28,19 @@ export default class HighlightRemoveCommand extends BushCommand { }); } - public override *args(): ArgumentGeneratorReturn { - const word: ArgType<'string'> = yield { - type: 'string', - match: 'rest', - prompt: { - start: highlightCommandArgs.remove[0].description, - retry: highlightCommandArgs.remove[0].retry, - optional: !highlightCommandArgs.remove[0].required - } - }; - - return { word }; - } - public override async exec(message: BushMessage | BushSlashMessage, args: { word: ArgType<'string'> }) { assert(message.inGuild()); - const [highlight] = await Highlight.findOrCreate({ - where: { - guild: message.guild.id, - user: message.author.id - } - }); + const res = await client.highlightManager.removeHighlight(message.guild.id, message.author.id, args.word); - if (!highlight.words.some((w) => w.word === args.word)) + if (typeof res === 'string') + return await message.util.reply({ content: `${util.emojis.error} ${res}`, allowedMentions: AllowedMentions.none() }); + else if (!res) return await message.util.reply({ - content: `${util.emojis.error} You have not highlighted "${args.word}".`, + content: `${util.emojis.error} There was an error unhighlighting "${args.word}".`, allowedMentions: AllowedMentions.none() }); - highlight.words = util.removeFromArray(highlight.words, highlight.words.find((w) => w.word === args.word)!); - await highlight.save(); - return await message.util.reply({ content: `${util.emojis.success} Successfully removed "${args.word}" from your highlight list.`, allowedMentions: AllowedMentions.none() diff --git a/src/commands/utilities/highlight-show.ts b/src/commands/utilities/highlight-show.ts index b9f414f..c3c6723 100644 --- a/src/commands/utilities/highlight-show.ts +++ b/src/commands/utilities/highlight-show.ts @@ -26,8 +26,39 @@ export default class HighlightShowCommand extends BushCommand { } }); + if (!highlight.words.length) return message.util.reply(`${util.emojis.error} You are not highlighting any words.`); + + const embed = new Embed() + .setTitle('Highlight List') + .setDescription( + highlight.words + .map((hl) => (hl.regex ? `/${hl.word}/gi` : hl.word)) + .join('\n') + .substring(0, 4096) + ) + .setColor(util.colors.default); + + if (highlight.blacklistedChannels.length) + embed.addField({ + name: 'Ignored Channels', + value: highlight.blacklistedChannels + .map((c) => `<#${c}>`) + .join('\n') + .substring(0, 1024), + inline: true + }); + if (highlight.blacklistedUsers.length) + embed.addField({ + name: 'Ignored Users', + value: highlight.blacklistedUsers + .map((u) => `<@!${u}>`) + .join('\n') + .substring(0, 1024), + inline: true + }); + return await message.util.reply({ - embeds: [new Embed().setTitle('Highlight List').setDescription(highlight.words.join('\n')).setColor(util.colors.default)], + embeds: [embed], allowedMentions: AllowedMentions.none() }); } |