From 26ae2871d32bd62b9fc08cdd73854206c37b7428 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 5 Jul 2023 11:37:42 +0200 Subject: [PATCH] Added /play command to play a track --- commands/play.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 commands/play.js diff --git a/commands/play.js b/commands/play.js new file mode 100644 index 0000000..a24e068 --- /dev/null +++ b/commands/play.js @@ -0,0 +1,54 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const { EmbedBuilder } = require('discord.js'); +module.exports = { + data: new SlashCommandBuilder() + .setName('play') + .setDescription('Play a track provided by search terms or URL.') + .addStringOption((option) => + option + .setName('query') + .setDescription('Search terms or URL.') + .setRequired(true) + ), + run: async ({ client, interaction, player }) => { + if (!interaction.member.voice.channel) { + return interaction.editReply( + 'You need to be in a voice channel to use this command.' + ); + } + + let query = interaction.options.getString('track'); + + try { + const { track } = await player.play( + interaction.member.voice.channel, + query, + { + requestedBy: interaction.user, + nodeOptions: { + leaveOnEmptyCooldown: 60000, + leaveOnEndCooldown: 60000, + leaveOnStopCooldown: 60000 + } + } + ); + + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor({ + name: interaction.user.username, + iconURL: interaction.user.avatarURL() + }) + .setDescription( + `**Added to queue**\n\`[${track.duration}]\` **[${track.title}](${track.url})**` + ) + .setThumbnail(track.thumbnail) + ] + }); + } catch (e) { + console.log(`Error occured while trying to play track:\n\n${e}`); + throw e; + } + } +};