diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-06-20 22:52:50 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-06-20 22:52:50 -0400 |
commit | 5c3da90f441c321f55ae735d6002f4da91f2481e (patch) | |
tree | ac6a993595eebe38fd5e7bd79ade4c5ec71be373 /src/listeners/commands | |
parent | 87e77ae8cc69d0d7f1e3d6f614b03c9297e85ab3 (diff) | |
download | tanzanite-5c3da90f441c321f55ae735d6002f4da91f2481e.tar.gz tanzanite-5c3da90f441c321f55ae735d6002f4da91f2481e.tar.bz2 tanzanite-5c3da90f441c321f55ae735d6002f4da91f2481e.zip |
feat(*): aaaaa
Diffstat (limited to 'src/listeners/commands')
-rw-r--r-- | src/listeners/commands/commandBlocked.ts | 66 | ||||
-rw-r--r-- | src/listeners/commands/commandError.ts | 5 | ||||
-rw-r--r-- | src/listeners/commands/commandStarted.ts | 2 | ||||
-rw-r--r-- | src/listeners/commands/slashBlocked.ts | 81 | ||||
-rw-r--r-- | src/listeners/commands/slashCommandError.ts | 73 | ||||
-rw-r--r-- | src/listeners/commands/slashStarted.ts | 21 |
6 files changed, 211 insertions, 37 deletions
diff --git a/src/listeners/commands/commandBlocked.ts b/src/listeners/commands/commandBlocked.ts index 61433a6..febfc93 100644 --- a/src/listeners/commands/commandBlocked.ts +++ b/src/listeners/commands/commandBlocked.ts @@ -1,6 +1,6 @@ -import { Command } from 'discord-akairo'; -import { Message } from 'discord.js'; +import { BushCommand } from '../../lib/extensions/BushCommand'; import { BushListener } from '../../lib/extensions/BushListener'; +import { BushMessage } from '../../lib/extensions/BushMessage'; export default class CommandBlockedListener extends BushListener { public constructor() { @@ -10,24 +10,70 @@ export default class CommandBlockedListener extends BushListener { }); } - public async exec(message: Message, command: Command, reason: string): Promise<void> { + public async exec(message: BushMessage, command: BushCommand, reason: string): Promise<unknown> { this.client.console.info( 'CommandBlocked', `<<${message.author.tag}>> tried to run <<${message.util.parsed.command}>> but was blocked because <<${reason}>>.`, false ); + const reasons = this.client.consts.BlockedReasons; switch (reason) { - case 'owner': { - await message.util.send(`You must be an owner to run command \`${message.util.parsed.command}\``); - break; + case reasons.OWNER: { + return await message.util.reply({ + content: `${this.client.util.emojis.error} Only my developers can run the \`${message.util.parsed.command}\` command.` + }); } - case 'blacklist': { - // pass - break; + case reasons.SUPER_USER: { + return await message.util.reply({ + content: `${this.client.util.emojis.error} You must be a superuser to run the \`${message.util.parsed.command}\` command.` + }); + } + case reasons.DISABLED_GLOBAL: { + return await message.util.reply({ + content: `${this.client.util.emojis.error} My developers disabled the \`${message.util.parsed.command}\` command.` + }); + } + case reasons.DISABLED_GUILD: { + return await message.util.reply({ + content: `${this.client.util.emojis.error} The \`${command.aliases[0]}\` command is currently disabled in \`${message.guild.name}\`.` + }); + } + case reasons.CHANNEL_BLACKLIST: { + return; + } + case reasons.USER_BLACKLIST: { + return; + } + case reasons.ROLE_BLACKLIST: { + return; + } + case reasons.RESTRICTED_CHANNEL: { + const channels = command.restrictedChannels; + const names = []; + channels.forEach((c) => { + names.push(`<#${c}>`); + }); + const pretty = this.client.util.oxford(names, 'and', undefined); + return await message.util.reply({ + content: `${this.client.util.emojis.error} \`${command}\` can only be run in ${pretty}.` + }); + } + case reasons.RESTRICTED_GUILD: { + const guilds = command.restrictedGuilds; + const names = []; + guilds.forEach((g) => { + names.push(`\`${this.client.guilds.cache.get(g).name}\``); + }); + const pretty = this.client.util.oxford(names, 'and', undefined); + return await message.util.reply({ + content: `${this.client.util.emojis.error} \`${command}\` can only be run in ${pretty}.` + }); } default: { - await message.util.send(`Command blocked with reason \`${reason}\``); + return await message.util.reply({ + content: `${this.client.util.emojis.error} Command blocked with reason \`${reason}\`` + }); } } } diff --git a/src/listeners/commands/commandError.ts b/src/listeners/commands/commandError.ts index cb8c5d2..7f765ae 100644 --- a/src/listeners/commands/commandError.ts +++ b/src/listeners/commands/commandError.ts @@ -19,7 +19,7 @@ export default class CommandErrorListener extends BushListener { .setDescription( stripIndents`**User:** ${message.author} (${message.author.tag}) **Command:** ${command} - **Channel:** ${message.channel} (${message.channel.id}) + **Channel:** ${message.channel} (${message.channel?.id}) **Message:** [link](${message.url})` ) .addField('Error', await this.client.util.codeblock(`${error?.stack}`, 1024, 'js')) @@ -59,7 +59,8 @@ export default class CommandErrorListener extends BushListener { this.client.console.error( 'CommandError', `an error occurred with the <<${command}>> command in <<${channel}>> triggered by <<${message?.author?.tag}>>:\n` + - error?.stack + error?.stack, + false ); } } diff --git a/src/listeners/commands/commandStarted.ts b/src/listeners/commands/commandStarted.ts index 28ed0f8..8c860f8 100644 --- a/src/listeners/commands/commandStarted.ts +++ b/src/listeners/commands/commandStarted.ts @@ -4,7 +4,7 @@ import { BushListener } from '../../lib/extensions/BushListener'; export default class CommandStartedListener extends BushListener { constructor() { - super('logCommands', { + super('commandStarted', { emitter: 'commandHandler', event: 'commandStarted' }); diff --git a/src/listeners/commands/slashBlocked.ts b/src/listeners/commands/slashBlocked.ts new file mode 100644 index 0000000..d8ef736 --- /dev/null +++ b/src/listeners/commands/slashBlocked.ts @@ -0,0 +1,81 @@ +import { BushCommand } from '../../lib/extensions/BushCommand'; +import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushListener } from '../../lib/extensions/BushListener'; + +export default class SlashBlockedListener extends BushListener { + public constructor() { + super('slashBlocked', { + emitter: 'commandHandler', + event: 'slashBlocked' + }); + } + + public async exec(message: BushInteractionMessage, command: BushCommand, reason: string): Promise<unknown> { + this.client.console.info( + 'SlashBlocked', + `<<${message.author.tag}>> tried to run <<${message.util.parsed.command}>> but was blocked because <<${reason}>>.`, + false + ); + + const reasons = this.client.consts.BlockedReasons; + + switch (reason) { + case reasons.OWNER: { + return await message.util.reply({ + content: `${this.client.util.emojis.error} Only my developers can run the \`${message.util.parsed.command}\` command.` + }); + } + case reasons.SUPER_USER: { + return await message.util.reply({ + content: `${this.client.util.emojis.error} You must be a superuser to run the \`${message.util.parsed.command}\` command.` + }); + } + case reasons.DISABLED_GLOBAL: { + return await message.util.reply({ + content: `${this.client.util.emojis.error} My developers disabled the \`${message.util.parsed.command}\` command.` + }); + } + case reasons.DISABLED_GUILD: { + return await message.util.reply({ + content: `${this.client.util.emojis.error} The \`${command.aliases[0]}\` command is currently disabled in \`${message.guild.name}\`.` + }); + } + case reasons.CHANNEL_BLACKLIST: { + return; + } + case reasons.USER_BLACKLIST: { + return; + } + case reasons.ROLE_BLACKLIST: { + return; + } + case reasons.RESTRICTED_CHANNEL: { + const channels = command.restrictedChannels; + const names = []; + channels.forEach((c) => { + names.push(`<#${c}>`); + }); + const pretty = this.client.util.oxford(names, 'and', undefined); + return await message.util.reply({ + content: `${this.client.util.emojis.error} \`${command}\` can only be run in ${pretty}.` + }); + } + case reasons.RESTRICTED_GUILD: { + const guilds = command.restrictedGuilds; + const names = []; + guilds.forEach((g) => { + names.push(`\`${this.client.guilds.cache.get(g).name}\``); + }); + const pretty = this.client.util.oxford(names, 'and', undefined); + return await message.util.reply({ + content: `${this.client.util.emojis.error} \`${command}\` can only be run in ${pretty}.` + }); + } + default: { + return await message.util.reply({ + content: `${this.client.util.emojis.error} Command blocked with reason \`${reason}\`` + }); + } + } + } +} diff --git a/src/listeners/commands/slashCommandError.ts b/src/listeners/commands/slashCommandError.ts index b8d9b6b..718e992 100644 --- a/src/listeners/commands/slashCommandError.ts +++ b/src/listeners/commands/slashCommandError.ts @@ -1,6 +1,7 @@ import { stripIndents } from 'common-tags'; -import { CommandInteraction, MessageEmbed, TextChannel } from 'discord.js'; +import { MessageEmbed } from 'discord.js'; import { BushCommand } from '../../lib/extensions/BushCommand'; +import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; import { BushListener } from '../../lib/extensions/BushListener'; export default class SlashCommandErrorListener extends BushListener { @@ -10,31 +11,55 @@ export default class SlashCommandErrorListener extends BushListener { event: 'slashError' }); } - async exec(error: Error, message: CommandInteraction, command: BushCommand): Promise<void> { - const errorNumber = Math.floor(Math.random() * 6969696969) + 69; // hehe funy numbers - const errorDevEmbed = this.client.util - .createEmbed(this.client.util.colors.error) - .setTitle(`Slash Error # \`${errorNumber}\`: An error occurred`) + async exec(error: Error, message: BushInteractionMessage, command: BushCommand): Promise<void> { + const errorNo = Math.floor(Math.random() * 6969696969) + 69; // hehe funny number + const errorEmbed: MessageEmbed = new MessageEmbed() + .setTitle(`Slash Error # \`${errorNo}\`: An error occurred`) .setDescription( - stripIndents`**User:** <@${message.user.id}> (${message.user.tag}) - **Slash Command:** ${command} - **Channel:** <#${message.channelID}> (${message.channelID}) - **Message:** [link](https://discord.com/${message.guildID}/${message.channelID}/${message.id})` + stripIndents`**User:** ${message.author} (${message.author.tag}) + **Slash Command:** ${command} + **Channel:** ${message.channel} (${message.channel.id}) + **Message:** [link](https://discord.com/${message.guild.id}/${message.guild.id}/${message.id})` ) - .addField('Error', `${await this.client.util.haste(error.stack)}`); - let errorUserEmbed: MessageEmbed; - if (command) { - errorUserEmbed = this.client.util - .createEmbed(this.client.util.colors.error) - .setTitle('An error occurred') - .setDescription( - stripIndents`Whoops! It appears like something broke. - The developers have been notified of this. If you contact them, give them code \`${errorNumber}\`. - ` - ); + .addField('Error', await this.client.util.codeblock(`${error?.stack}`, 1024, 'js')) + .setColor(this.client.util.colors.error) + .setTimestamp(); + + if (message) { + if (!this.client.config.owners.includes(message.author.id)) { + const errorUserEmbed: MessageEmbed = new MessageEmbed() + .setTitle('An error occurred') + .setColor(this.client.util.colors.error) + .setTimestamp(); + await this.client.logger.channelError({ embeds: [errorEmbed] }); + if (!command) + errorUserEmbed.setDescription(`Oh no! An error occurred. Please give the developers code \`${errorNo}\`.`); + else + errorUserEmbed.setDescription( + `Oh no! While running the command \`${command.id}\`, an error occurred. Please give the developers code \`${errorNo}\`.` + ); + await message.util.send({ embeds: [errorUserEmbed] }).catch((e) => { + const channel = message.channel.type === 'dm' ? message.channel.recipient.tag : message.channel.name; + this.client.console.warn('SlashError', `Failed to send user error embed in <<${channel}>>:\n` + e?.stack); + }); + } else { + const errorDevEmbed = new MessageEmbed() + .setTitle('An error occurred') + .setColor(this.client.util.colors.error) + .setTimestamp() + .setDescription(await this.client.util.codeblock(`${error?.stack}`, 2048, 'js')); + await message.util.send({ embeds: [errorDevEmbed] }).catch((e) => { + const channel = message.channel.type === 'dm' ? message.channel.recipient.tag : message.channel.name; + this.client.console.warn('SlashError', `Failed to send owner error stack in <<${channel}>>.` + e?.stack); + }); + } } - const channel = (await this.client.channels.fetch(this.client.config.channels.log)) as TextChannel; - await channel.send({ embeds: [errorDevEmbed] }); - if (errorUserEmbed) await message.reply({ embeds: [errorUserEmbed] }); + const channel = message.channel.type === 'dm' ? message.channel.recipient.tag : message.channel.name; + this.client.console.error( + 'SlashError', + `an error occurred with the <<${command}>> command in <<${channel}>> triggered by <<${message?.author?.tag}>>:\n` + + error?.stack, + false + ); } } diff --git a/src/listeners/commands/slashStarted.ts b/src/listeners/commands/slashStarted.ts new file mode 100644 index 0000000..6a45546 --- /dev/null +++ b/src/listeners/commands/slashStarted.ts @@ -0,0 +1,21 @@ +import { Message } from 'discord.js'; +import { BushCommand } from '../../lib/extensions/BushCommand'; +import { BushListener } from '../../lib/extensions/BushListener'; + +export default class SlashStartedListener extends BushListener { + constructor() { + super('slashStarted', { + emitter: 'commandHandler', + event: 'slashStarted' + }); + } + exec(message: Message, command: BushCommand): void { + this.client.logger.info( + 'SlashCommand', + `The <<${command.id}>> command was used by <<${message.author.tag}>> in ${ + message.channel.type === 'dm' ? `their <<DMs>>` : `<<#${message.channel.name}>> in <<${message.guild?.name}>>` + }.`, + false // I don't want to spam the log channel when people use commands + ); + } +} |