play.js original
unknown
javascript
a year ago
7.6 kB
11
Indexable
const { SlashCommandBuilder } = require('@discordjs/builders');
const { EmbedBuilder } = require('discord.js');
const { QueryType, useMainPlayer, useQueue } = require('discord-player');
const radioState = require('../../eventos/musica/radioState');
module.exports = {
data: new SlashCommandBuilder()
.setName('play')
.setDescription('Play a song (works best with YouTube or Soundcloud links)')
.addStringOption(option =>
option.setName('song')
.setDescription('Song link or query')
.setRequired(true))
.addBooleanOption(option =>
option.setName('playnext')
.setDescription('Puts the song at the top of the queue')
.setRequired(false)),
async run({interaction, client}) {
const MAX_QUEUE_SIZE = 10000;
const player = useMainPlayer();
const guild = interaction.guild;
const queue = useQueue(guild.id);
if (!interaction.member.voice.channel) {
return interaction.editReply({ embeds: [new EmbedBuilder().setColor(0xff0000).setDescription('You must be in a voice channel.').setTimestamp()] });
}
const song = interaction.options.getString('song');
const playNext = interaction.options.getBoolean('playnext');
if (!song) {
return interaction.editReply({ embeds: [new EmbedBuilder().setColor(0xff0000).setDescription('Please enter a song URL or query to search.').setTimestamp()] });
}
let embed = new EmbedBuilder()
.setColor(0xffffff)
.setDescription('Request received, fetching...')
.setTimestamp()
.setFooter({ text: 'Age restricted videos might not work.' });
let research = await player.search(song, {
requestedBy: interaction.member,
searchEngine: QueryType.AUTO_SEARCH,
});
const msg = await interaction.editReply({ embeds: [embed], fetchReply: true });
try {
const linkRegex = /(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])/igm;
const spotifyRegex = /^(?:https:\/\/open\.spotify\.com\/(?:intl-[a-zA-Z]{0,3}\/)?(?:user\/[A-Za-z0-9]+\/)?|spotify:)(?:track\/)([A-Za-z0-9]+).*$/;
if ((spotifyRegex.test(song) || !linkRegex.test(song))) {
if (spotifyRegex.test(song)) {
research = await player.search(song, {
requestedBy: interaction.member,
searchEngine: QueryType.SPOTIFY_SONG,
});
}
if (!research.hasTracks()) {
return interaction.editReply({ embeds: [new EmbedBuilder().setColor(0xff0000).setDescription('No results found').setTimestamp()] });
}
let newResearch = research.hasTracks() ? `${research.tracks[0].title} - ${research.tracks[0].author}` : song;
research = await player.search(newResearch, {
requestedBy: interaction.member,
searchEngine: QueryType.YOUTUBE_SEARCH,
});
embed = new EmbedBuilder()
.setColor(0xffffff)
.setTitle('Type in chat the number you want to play')
.setDescription('Not entering a number will make it play the best match')
.setTimestamp();
const choices = research.tracks.slice(0, 10);
choices.forEach((track, index) => {
embed.addFields({ name: `${index + 1} - ${track.title}`, value: `By ${track.author}` });
});
await msg.edit({ embeds: [embed] });
const filter = (m) => m.author.id === interaction.user.id;
try {
const collected = await interaction.channel.awaitMessages({ filter, max: 1, time: 10000, errors: ['time'] });
const responseMessage = collected.first();
research = choices[parseInt(responseMessage.content) - 1];
responseMessage.delete();
} catch {
research = choices[0];
}
} else {
research = await player.search(song, {
requestedBy: interaction.member,
searchEngine: QueryType.AUTO,
});
if (!research.hasTracks()) {
embed = new EmbedBuilder()
.setColor(0xff0000)
.setDescription('No results found')
.setTimestamp();
await msg.edit({ embeds: [embed] });
}
}
if (research?.tracks?.length + (queue?.size ?? 0) > MAX_QUEUE_SIZE) {
return await msg.edit({ embeds: [new EmbedBuilder().setColor(0xff0000).setDescription(`Cannot enqueue more than ${MAX_QUEUE_SIZE} tracks.`).setTimestamp()] });
}
if (playNext && queue) {
for (const track of research.tracks.reverse()) queue.insertTrack(track, 0);
res = { searchResult: { hasPlaylist: () => false } };
res.track = research.tracks[0];
res.searchResult.hasPlaylist = () => research.tracks.length > 1;
} else {
res = await player.play(interaction.member.voice.channel.id, research, {
nodeOptions: {
metadata: {
channel: interaction.channel,
client: interaction.guild.members.me,
requestedBy: interaction.user,
guild: interaction.guild,
},
volume: 50,
maxSize: MAX_QUEUE_SIZE,
bufferingTimeout: 15000,
leaveOnStop: true,
leaveOnStopCooldown: 0,
leaveOnEnd: true,
leaveOnEndCooldown: 15000,
leaveOnEmpty: true,
leaveOnEmptyCooldown: 300000,
skipOnNoStream: true,
},
});
}
console.log(`Playing [${res.track.title}] in [${interaction.member.voice.channel.name}]`);
embed = new EmbedBuilder()
.setColor(0x00ff00)
.setTitle(`${!queue?.currentTrack ? 'Track now playing!' : 'Track enqueued!'}`)
.setThumbnail(res.track.thumbnail)
.setDescription(`[${res.track.title}](${res.track.url})`)
.addFields(
{ name: 'Will play next', value: playNext && queue ? 'Yes' : 'No' }
)
.setTimestamp();
if (res.searchResult?.playlist) {
embed.addFields({ name: 'Playlist', value: res.searchResult.playlist.title });
}
await msg.edit({ embeds: [embed] });
} catch (err) {
console.error(err);
return await msg.edit({ embeds: [new EmbedBuilder().setColor(0xff0000).setDescription('Failed to fetch / play the requested track').setTimestamp()] });
}
},
};
Editor is loading...
Leave a Comment