aboutsummaryrefslogtreecommitdiff
path: root/src/commands/remove.ts
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2022-02-05 16:16:17 -0600
committerGitHub <[email protected]>2022-02-05 16:16:17 -0600
commit56a469a999774c8b8683adeb59b243fdf85b14c9 (patch)
tree2e11f067d6e4caae9301986a0b432825cc65839e /src/commands/remove.ts
parente883275d831f3d9bf3a57bd91e0deeeaf4951242 (diff)
downloadmuse-56a469a999774c8b8683adeb59b243fdf85b14c9.tar.xz
muse-56a469a999774c8b8683adeb59b243fdf85b14c9.zip
Migrate to slash commands (#431)
Co-authored-by: Federico fuji97 Rapetti <[email protected]>
Diffstat (limited to 'src/commands/remove.ts')
-rw-r--r--src/commands/remove.ts79
1 files changed, 25 insertions, 54 deletions
diff --git a/src/commands/remove.ts b/src/commands/remove.ts
index 9c40a71..55c416f 100644
--- a/src/commands/remove.ts
+++ b/src/commands/remove.ts
@@ -1,18 +1,24 @@
-import {Message} from 'discord.js';
+import {CommandInteraction} from 'discord.js';
import {inject, injectable} from 'inversify';
import {TYPES} from '../types.js';
import PlayerManager from '../managers/player.js';
import Command from '.';
-import errorMsg from '../utils/error-msg.js';
+import {SlashCommandBuilder} from '@discordjs/builders';
@injectable()
export default class implements Command {
- public name = 'remove';
- public aliases = ['rm'];
- public examples = [
- ['remove 1', 'removes the next song in the queue'],
- ['rm 5-7', 'remove every song in range 5 - 7 (inclusive) from the queue'],
- ];
+ public readonly slashCommand = new SlashCommandBuilder()
+ .setName('remove')
+ .setDescription('remove songs from the queue')
+ .addIntegerOption(option =>
+ option.setName('position')
+ .setDescription('position of the song to remove [default: 1]')
+ .setRequired(false),
+ )
+ .addIntegerOption(option =>
+ option.setName('range')
+ .setDescription('number of songs to remove [default: 1]')
+ .setRequired(false));
private readonly playerManager: PlayerManager;
@@ -20,57 +26,22 @@ export default class implements Command {
this.playerManager = playerManager;
}
- public async execute(msg: Message, args: string []): Promise<void> {
- const player = this.playerManager.get(msg.guild!.id);
+ public async execute(interaction: CommandInteraction): Promise<void> {
+ const player = this.playerManager.get(interaction.guild!.id);
- if (args.length === 0) {
- await msg.channel.send(errorMsg('missing song position or range'));
- return;
- }
-
- const reg = /^(\d+)-(\d+)$|^(\d+)$/g; // Expression has 3 groups: x-y or z. x-y is range, z is a single digit.
- const match = reg.exec(args[0]);
+ const position = interaction.options.getInteger('position') ?? 1;
+ const range = interaction.options.getInteger('range') ?? 1;
- if (match === null) {
- await msg.channel.send(errorMsg('incorrect format'));
- return;
+ if (position < 1) {
+ throw new Error('position must be at least 1');
}
- if (match[3] === undefined) { // 3rd group (z) doesn't exist -> a range
- const range = [parseInt(match[1], 10), parseInt(match[2], 10)];
-
- if (range[0] < 1) {
- await msg.channel.send(errorMsg('position must be greater than 0'));
- return;
- }
-
- if (range[1] > player.queueSize()) {
- await msg.channel.send(errorMsg('position is outside of the queue\'s range'));
- return;
- }
-
- if (range[0] < range[1]) {
- player.removeFromQueue(range[0], range[1] - range[0] + 1);
- } else {
- await msg.channel.send(errorMsg('range is backwards'));
- return;
- }
- } else { // 3rd group exists -> just one song
- const index = parseInt(match[3], 10);
-
- if (index < 1) {
- await msg.channel.send(errorMsg('position must be greater than 0'));
- return;
- }
-
- if (index > player.queueSize()) {
- await msg.channel.send(errorMsg('position is outside of the queue\'s range'));
- return;
- }
-
- player.removeFromQueue(index, 1);
+ if (range < 1) {
+ throw new Error('range must be at least 1');
}
- await msg.channel.send(':wastebasket: removed');
+ player.removeFromQueue(position, range);
+
+ await interaction.reply(':wastebasket: removed');
}
}