mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-01 14:38:48 +00:00
167 lines
6.4 KiB
JavaScript
167 lines
6.4 KiB
JavaScript
const { SlashCommandBuilder } = require('@discordjs/builders');
|
|
const { useMainPlayer, useQueue } = require('discord-player');
|
|
const { EmbedBuilder } = require('discord.js');
|
|
const { embedColors, playerOptions, botInfo } = require('../config.json');
|
|
|
|
module.exports = {
|
|
data: new SlashCommandBuilder()
|
|
.setName('play')
|
|
.setDescription(
|
|
'Add a track or playlist to the queue by searching or url.'
|
|
)
|
|
.setDMPermission(false)
|
|
.addStringOption((option) =>
|
|
option
|
|
.setName('query')
|
|
.setDescription('Search query or URL.')
|
|
.setRequired(true)
|
|
),
|
|
run: async ({ interaction }) => {
|
|
if (!interaction.member.voice.channel) {
|
|
return await interaction.editReply({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setDescription(
|
|
'**Failed**\nYou need to be in a voice channel to use this command.'
|
|
)
|
|
.setColor(embedColors.colorWarning)
|
|
]
|
|
});
|
|
}
|
|
|
|
const player = useMainPlayer();
|
|
const query = interaction.options.getString('query');
|
|
|
|
const searchResult = await player.search(query, {
|
|
requestedBy: interaction.user
|
|
});
|
|
|
|
if (!searchResult || searchResult.tracks.length === 0) {
|
|
return await interaction.editReply({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setDescription(
|
|
`**No track found**\nNo results found for \`${query}\`.`
|
|
)
|
|
.setColor(embedColors.colorWarning)
|
|
]
|
|
});
|
|
}
|
|
|
|
if (
|
|
searchResult.tracks[0].raw.live &&
|
|
searchResult.tracks[0].raw.duration === 0 &&
|
|
searchResult.tracks[0].source === 'youtube'
|
|
) {
|
|
return await interaction.editReply({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setDescription(
|
|
`**Unsupported Source**\nThis audio source is a YouTube live stream, which is currently not a supported format.\n\n_If you think that this is incorrect, please submit a bug report in the bot [support server](${botInfo.supportServerInviteUrl})._`
|
|
)
|
|
.setColor(embedColors.colorWarning)
|
|
]
|
|
});
|
|
}
|
|
|
|
const { track } = await player.play(
|
|
interaction.member.voice.channel,
|
|
searchResult,
|
|
{
|
|
requestedBy: interaction.user,
|
|
nodeOptions: {
|
|
leaveOnEmpty: playerOptions.leaveOnEmpty ?? true,
|
|
leaveOnEmptyCooldown:
|
|
playerOptions.leaveOnEmptyCooldown ?? 60000,
|
|
leaveOnEnd: playerOptions.leaveOnEnd ?? true,
|
|
leaveOnEndCooldown:
|
|
playerOptions.leaveOnEndCooldown ?? 60000,
|
|
leaveOnStop: playerOptions.leaveOnStop ?? true,
|
|
leaveOnStopCooldown:
|
|
playerOptions.leaveOnStopCooldown ?? 60000,
|
|
maxSize: playerOptions.maxQueueSize ?? 1000,
|
|
maxHistorySize: playerOptions.maxHistorySize ?? 100,
|
|
volume: playerOptions.defaultVolume ?? 50
|
|
}
|
|
}
|
|
);
|
|
|
|
const queue = useQueue(interaction.guild.id);
|
|
|
|
if (!queue) {
|
|
return await interaction.editReply({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setDescription(
|
|
'**Failed**\nFailed to add track to queue. Please try again.'
|
|
)
|
|
.setColor(embedColors.colorError)
|
|
]
|
|
});
|
|
}
|
|
|
|
if (track.source === 'arbitrary') {
|
|
track.thumbnail =
|
|
'https://raw.githubusercontent.com/mariusbegby/cadence-discord-bot/main/assets/logo-rounded-128px.png';
|
|
}
|
|
|
|
let durationFormat =
|
|
track.raw.duration === 0 || track.duration === '0:00'
|
|
? ''
|
|
: `\`${track.duration}\``;
|
|
|
|
if (searchResult.playlist && searchResult.tracks.length > 1) {
|
|
return await interaction.editReply({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setAuthor({
|
|
name: interaction.user.username,
|
|
iconURL: interaction.user.avatarURL()
|
|
})
|
|
.setDescription(
|
|
`**Added playlist to queue**\n${durationFormat} **[${
|
|
track.title
|
|
}](${track.url})**\n\nAnd **${
|
|
searchResult.tracks.length - 1
|
|
}** more tracks... \`/queue\` to view all.`
|
|
)
|
|
.setThumbnail(track.thumbnail)
|
|
.setColor(embedColors.colorSuccess)
|
|
]
|
|
});
|
|
}
|
|
|
|
if (queue.currentTrack === track) {
|
|
return await interaction.editReply({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setAuthor({
|
|
name: interaction.user.username,
|
|
iconURL: interaction.user.avatarURL()
|
|
})
|
|
.setDescription(
|
|
`**Started playing**\n${durationFormat} **[${track.title}](${track.url})**`
|
|
)
|
|
.setThumbnail(track.thumbnail)
|
|
.setColor(embedColors.colorSuccess)
|
|
]
|
|
});
|
|
}
|
|
|
|
return await interaction.editReply({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setAuthor({
|
|
name: interaction.user.username,
|
|
iconURL: interaction.user.avatarURL()
|
|
})
|
|
.setDescription(
|
|
`**Added to queue**\n${durationFormat} **[${track.title}](${track.url})**`
|
|
)
|
|
.setThumbnail(track.thumbnail)
|
|
.setColor(embedColors.colorSuccess)
|
|
]
|
|
});
|
|
}
|
|
};
|