aboutsummaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorTymanWasTaken <tyman@tyman.tech>2021-05-16 22:23:12 -0400
committerTymanWasTaken <tyman@tyman.tech>2021-05-16 22:23:12 -0400
commit4d63c4af57a7391dd61106b79874b8e83c14971a (patch)
tree4d9aa589b3501169d9ddfe8f0fdff50d9080b553 /src/commands
parent759e93bec4e9e2eb86db7434007345c24b0a0252 (diff)
downloadtanzanite-4d63c4af57a7391dd61106b79874b8e83c14971a.tar.gz
tanzanite-4d63c4af57a7391dd61106b79874b8e83c14971a.tar.bz2
tanzanite-4d63c4af57a7391dd61106b79874b8e83c14971a.zip
change all existing slash commands to have no repeating code, add help slash command
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/admin/prefix.ts25
-rw-r--r--src/commands/info/botinfo.ts14
-rw-r--r--src/commands/info/help.ts38
-rw-r--r--src/commands/moulberry-bush/level.ts37
4 files changed, 68 insertions, 46 deletions
diff --git a/src/commands/admin/prefix.ts b/src/commands/admin/prefix.ts
index 32af649..6d0273b 100644
--- a/src/commands/admin/prefix.ts
+++ b/src/commands/admin/prefix.ts
@@ -1,5 +1,5 @@
import { ApplicationCommandOptionType } from 'discord-api-types';
-import { CommandInteraction, Message } from 'discord.js';
+import { CommandInteraction, Message, Guild as DiscordGuild } from 'discord.js';
import { BotCommand } from '../../lib/extensions/BotCommand';
import { Guild } from '../../lib/models';
@@ -29,16 +29,24 @@ export default class PrefixCommand extends BotCommand {
]
});
}
- async exec(message: Message, { prefix }: { prefix?: string }): Promise<void> {
+
+ async changePrefix(guild: DiscordGuild, prefix?: string): Promise<void> {
if (prefix) {
- const row = await Guild.findByPk(message.guild.id);
+ const row = await Guild.findByPk(guild.id);
row.prefix = prefix;
await row.save();
- await message.util.send(`Sucessfully set prefix to \`${prefix}\``);
} else {
- const row = await Guild.findByPk(message.guild.id);
+ const row = await Guild.findByPk(guild.id);
row.prefix = this.client.config.prefix;
await row.save();
+ }
+ }
+
+ async exec(message: Message, { prefix }: { prefix?: string }): Promise<void> {
+ await this.changePrefix(message.guild, prefix);
+ if (prefix) {
+ await message.util.send(`Sucessfully set prefix to \`${prefix}\``);
+ } else {
await message.util.send(
`Sucessfully reset prefix to \`${this.client.config.prefix}\``
);
@@ -50,15 +58,10 @@ export default class PrefixCommand extends BotCommand {
| string
| undefined;
+ await this.changePrefix(message.guild, prefix);
if (prefix) {
- const row = await Guild.findByPk(message.guild.id);
- row.prefix = prefix;
- await row.save();
await message.reply(`Sucessfully set prefix to \`${prefix}\``);
} else {
- const row = await Guild.findByPk(message.guild.id);
- row.prefix = this.client.config.prefix;
- await row.save();
await message.reply(
`Sucessfully reset prefix to \`${this.client.config.prefix}\``
);
diff --git a/src/commands/info/botinfo.ts b/src/commands/info/botinfo.ts
index 306c142..8f5f055 100644
--- a/src/commands/info/botinfo.ts
+++ b/src/commands/info/botinfo.ts
@@ -1,4 +1,4 @@
-import { MessageEmbed, Message } from 'discord.js';
+import { MessageEmbed, Message, CommandInteraction } from 'discord.js';
import { BotCommand } from '../../lib/extensions/BotCommand';
import { duration } from 'moment';
@@ -14,7 +14,7 @@ export default class BotInfoCommand extends BotCommand {
});
}
- public async exec(message: Message): Promise<void> {
+ private async generateEmbed(): Promise<MessageEmbed> {
const owners = (await this.client.util.mapIDs(this.client.ownerID))
.map((u) => u.tag)
.join('\n');
@@ -52,6 +52,14 @@ export default class BotInfoCommand extends BotCommand {
}
])
.setTimestamp();
- await message.util.send(embed);
+ return embed;
+ }
+
+ public async exec(message: Message): Promise<void> {
+ await message.util.send(await this.generateEmbed());
+ }
+
+ public async execSlash(message: CommandInteraction): Promise<void> {
+ await message.reply(await this.generateEmbed());
}
}
diff --git a/src/commands/info/help.ts b/src/commands/info/help.ts
index 73dcdbb..cdddb51 100644
--- a/src/commands/info/help.ts
+++ b/src/commands/info/help.ts
@@ -1,6 +1,8 @@
import { Message, MessageEmbed } from 'discord.js';
import { BotCommand } from '../../lib/extensions/BotCommand';
import { stripIndent } from 'common-tags';
+import { ApplicationCommandOptionType } from 'discord-api-types';
+import { CommandInteraction } from 'discord.js';
export default class HelpCommand extends BotCommand {
constructor() {
@@ -17,14 +19,19 @@ export default class HelpCommand extends BotCommand {
id: 'command',
type: 'commandAlias'
}
+ ],
+ slashCommandOptions: [
+ {
+ type: ApplicationCommandOptionType.STRING,
+ name: 'command',
+ description: 'The command to get help for',
+ required: false
+ }
]
});
}
- public async exec(
- message: Message,
- { command }: { command: BotCommand }
- ): Promise<Message> {
+ private generateEmbed(command?: BotCommand): MessageEmbed {
const prefix = this.handler.prefix;
if (!command) {
const embed = new MessageEmbed()
@@ -49,7 +56,7 @@ export default class HelpCommand extends BotCommand {
.join(' ')}`
);
}
- return message.util.send(embed);
+ return embed;
}
const embed = new MessageEmbed()
@@ -72,7 +79,26 @@ export default class HelpCommand extends BotCommand {
`\`${command.description.examples.join('`\n`')}\``,
true
);
+ return embed;
+ }
- return message.util.send(embed);
+ public async exec(
+ message: Message,
+ { command }: { command: BotCommand }
+ ): Promise<void> {
+ await message.util.send(this.generateEmbed(command));
+ }
+
+ public async execSlash(message: CommandInteraction): Promise<void> {
+ const command = message.options.find((o) => o.name === 'command')?.value as
+ | string
+ | undefined;
+ if (command) {
+ await message.reply(
+ this.generateEmbed(this.handler.findCommand(command) as BotCommand)
+ );
+ } else {
+ await message.reply(this.generateEmbed());
+ }
}
}
diff --git a/src/commands/moulberry-bush/level.ts b/src/commands/moulberry-bush/level.ts
index 0eb0044..ab41f42 100644
--- a/src/commands/moulberry-bush/level.ts
+++ b/src/commands/moulberry-bush/level.ts
@@ -37,38 +37,23 @@ export default class LevelCommand extends BotCommand {
});
}
- async exec(message: Message, { user }: { user?: User }): Promise<void> {
- const userLevelRow = await Level.findByPk(
- user ? user.id : message.author.id
- );
+ private async getResponse(user: User): Promise<string> {
+ const userLevelRow = await Level.findByPk(user.id);
if (userLevelRow) {
- await message.reply(
- `${user ? `${user.tag}'s` : 'Your'} level is ${userLevelRow.level} (${
- userLevelRow.xp
- } XP)`
- );
+ return `${user ? `${user.tag}'s` : 'Your'} level is ${
+ userLevelRow.level
+ } (${userLevelRow.xp} XP)`;
} else {
- await message.reply(
- `${user ? `${user.tag} does` : 'You do'} not have a level yet!`
- );
+ return `${user ? `${user.tag} does` : 'You do'} not have a level yet!`;
}
}
+
+ async exec(message: Message, { user }: { user?: User }): Promise<void> {
+ await message.reply(await this.getResponse(user || message.author));
+ }
async execSlash(message: CommandInteraction): Promise<void> {
const user =
message.options.find((o) => o.name === 'user')?.user || message.user;
- const userLevelRow = await Level.findByPk(user.id);
- if (userLevelRow) {
- await message.reply(
- `${user.id !== message.user.id ? `${user.tag}'s` : 'Your'} level is ${
- userLevelRow.level
- } (${userLevelRow.xp} XP)`
- );
- } else {
- await message.reply(
- `${
- user.id !== message.user.id ? `${user.tag} does` : 'You do'
- } not have a level yet!`
- );
- }
+ await message.reply(await this.getResponse(user));
}
}