Refactor seek command as slash command

This commit is contained in:
Federico fuji97 Rapetti 2021-12-27 18:26:37 +01:00
parent e7a87c4f52
commit 65e1f975b9

View file

@ -1,21 +1,22 @@
import {Message, TextChannel} from 'discord.js'; import {CommandInteraction} from 'discord.js';
import {TYPES} from '../types.js'; import {TYPES} from '../types.js';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player.js'; import PlayerManager from '../managers/player.js';
import LoadingMessage from '../utils/loading-message.js';
import errorMsg from '../utils/error-msg.js'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
import {parseTime} from '../utils/time.js'; import {parseTime, prettyTime} from '../utils/time.js';
import {SlashCommandBuilder} from '@discordjs/builders';
@injectable() @injectable()
export default class implements Command { export default class implements Command {
public name = 'seek'; public readonly slashCommand = new SlashCommandBuilder()
public aliases = []; .setName('seek')
public examples = [ .setDescription('seek to a position from beginning of song')
['seek 10', 'seeks to 10 seconds from beginning of song'], .addStringOption(option =>
['seek 1:30', 'seeks to 1 minute and 30 seconds from beginning of song'], option.setName('time')
['seek 1:00:00', 'seeks to 1 hour from beginning of song'], .setDescription('time to seek')
]; .setRequired(true),
);
public requiresVC = true; public requiresVC = true;
@ -25,22 +26,28 @@ export default class implements Command {
this.playerManager = playerManager; this.playerManager = playerManager;
} }
public async execute(msg: Message, args: string []): Promise<void> { public async executeFromInteraction(interaction: CommandInteraction): Promise<void> {
const player = this.playerManager.get(msg.guild!.id); const player = this.playerManager.get(interaction.guild!.id);
const currentSong = player.getCurrent(); const currentSong = player.getCurrent();
if (!currentSong) { if (!currentSong) {
await msg.channel.send(errorMsg('nothing is playing')); await interaction.reply({
content: errorMsg('nothing is playing'),
ephemeral: true,
});
return; return;
} }
if (currentSong.isLive) { if (currentSong.isLive) {
await msg.channel.send(errorMsg('can\'t seek in a livestream')); await interaction.reply({
content: errorMsg('can\'t seek in a livestream'),
ephemeral: true,
});
return; return;
} }
const time = args[0]; const time = interaction.options.getString('time')!;
let seekTime = 0; let seekTime = 0;
@ -51,20 +58,18 @@ export default class implements Command {
} }
if (seekTime > currentSong.length) { if (seekTime > currentSong.length) {
await msg.channel.send(errorMsg('can\'t seek past the end of the song')); await interaction.reply({
content: errorMsg('can\'t seek past the end of the song'),
ephemeral: true,
});
return; return;
} }
const loading = new LoadingMessage(msg.channel as TextChannel); await Promise.all([
player.seek(seekTime),
interaction.deferReply(),
]);
await loading.start(); await interaction.editReply(`👍 seeked to ${prettyTime(player.getPosition())}`);
try {
await player.seek(seekTime);
await loading.stop();
} catch (error: unknown) {
await loading.stop(errorMsg(error as Error));
}
} }
} }