diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-02-07 23:55:54 -0500 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-02-07 23:55:54 -0500 |
commit | 2bcf6828b67a6eb3c2bbff3892ce5ca53d00e713 (patch) | |
tree | 77d706d1d33c92eab7823c415629d14c0b887960 /src/commands/utilities/highlight-add.ts | |
parent | c3ae31a7539ecb5455df811a2e1d5603beb5c9ae (diff) | |
download | tanzanite-2bcf6828b67a6eb3c2bbff3892ce5ca53d00e713.tar.gz tanzanite-2bcf6828b67a6eb3c2bbff3892ce5ca53d00e713.tar.bz2 tanzanite-2bcf6828b67a6eb3c2bbff3892ce5ca53d00e713.zip |
started working on highlight command
Diffstat (limited to 'src/commands/utilities/highlight-add.ts')
-rw-r--r-- | src/commands/utilities/highlight-add.ts | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/commands/utilities/highlight-add.ts b/src/commands/utilities/highlight-add.ts new file mode 100644 index 0000000..ec5443c --- /dev/null +++ b/src/commands/utilities/highlight-add.ts @@ -0,0 +1,82 @@ +import { AllowedMentions, BushCommand, Highlight, type ArgType, type BushMessage, type BushSlashMessage } from '#lib'; +import assert from 'assert'; +import { ArgumentGeneratorReturn } from 'discord-akairo'; +import { highlightCommandArgs, highlightSubcommands } from './highlight-!'; + +export default class HighlightAddCommand extends BushCommand { + public constructor() { + super('highlight-add', { + aliases: [], + category: 'utilities', + description: highlightSubcommands.add, + usage: [], + examples: [], + clientPermissions: [], + userPermissions: [] + }); + } + + 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'> } + ) { + assert(message.inGuild()); + + if (!args.regex) { + if (args.word.length < 2) + return message.util.send(`${util.emojis.error} You can only highlight words that are longer than 2 characters.`); + if (args.word.length > 50) + return await message.util.reply(`${util.emojis.error} You can only highlight words that are shorter than 50 characters.`); + } else { + try { + new RegExp(args.word); + } catch (e) { + assert(e instanceof SyntaxError); + return message.util.send({ + content: `${util.emojis.error} Invalid regex ${util.format.inlineCode(e.message)}.`, + allowedMentions: AllowedMentions.none() + }); + } + } + + const [highlight] = await Highlight.findOrCreate({ + where: { + guild: message.guild.id, + user: message.author.id + } + }); + + if (highlight.words.some((w) => w.word === args.word)) + return await message.util.reply({ + content: `${util.emojis.error} You have already highlighted "${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() + }); + } +} |