84 lines
3.6 KiB
JavaScript
Executable File
84 lines
3.6 KiB
JavaScript
Executable File
const { SlashCommandBuilder } = require('discord.js')
|
|
const { useMasterPlayer, useQueue, QueryType } = require('discord-player')
|
|
|
|
module.exports = {
|
|
data: new SlashCommandBuilder()
|
|
.setName('play')
|
|
.setDescription('Jouer une musique.')
|
|
.addStringOption(option => option.setName('recherche').setDescription('Titre de la musique à chercher').setRequired(true).setAutocomplete(true)),
|
|
async autocompleteRun(interaction) {
|
|
let query = interaction.options.getString('recherche', true)
|
|
if (!query) return interaction.respond([])
|
|
|
|
let player = useMasterPlayer()
|
|
|
|
const resultsYouTube = await player.search(query, { searchEngine: QueryType.YOUTUBE })
|
|
const resultsSpotify = await player.search(query, { searchEngine: QueryType.SPOTIFY_SEARCH })
|
|
|
|
const tracksYouTube = resultsYouTube.tracks.slice(0, 5).map((t) => ({
|
|
name: `YouTube: ${`${t.title} - ${t.author} (${t.duration})`.length > 75 ? `${`${t.title} - ${t.author}`.substring(0, 75)}... (${t.duration})` : `${t.title} - ${t.author} (${t.duration})`}`,
|
|
value: t.url
|
|
}))
|
|
const tracksSpotify = resultsSpotify.tracks.slice(0, 5).map((t) => ({
|
|
name: `Spotify: ${`${t.title} - ${t.author} (${t.duration})`.length > 75 ? `${`${t.title} - ${t.author}`.substring(0, 75)}... (${t.duration})` : `${t.title} - ${t.author} (${t.duration})`}`,
|
|
value: t.url
|
|
}))
|
|
|
|
const tracks = []
|
|
tracksYouTube.forEach((t) => tracks.push({ name: t.name, value: t.value }));
|
|
tracksSpotify.forEach((t) => tracks.push({ name: t.name, value: t.value }));
|
|
|
|
return interaction.respond(tracks)
|
|
},
|
|
async execute(interaction) {
|
|
let voiceChannel = interaction.member.voice.channel
|
|
if (!voiceChannel) return await interaction.reply({ content: "T'es pas dans un vocal, idiot !", ephemeral: true })
|
|
|
|
let botChannel = interaction.guild.members.me.voice.channel
|
|
if (botChannel && voiceChannel.id !== botChannel.id) return await interaction.reply({ content: "T'es pas dans mon vocal !", ephemeral: true })
|
|
|
|
await interaction.deferReply()
|
|
|
|
let query = interaction.options.getString('recherche', true)
|
|
let player = useMasterPlayer()
|
|
let queue = useQueue(interaction.guild.id)
|
|
|
|
if (!queue) {
|
|
queue = player.nodes.create(interaction.guild, {
|
|
metadata: {
|
|
channel: interaction.channel,
|
|
client: interaction.guild.members.me,
|
|
requestedBy: interaction.user
|
|
},
|
|
selfDeaf: true,
|
|
volume: 20,
|
|
leaveOnEmpty: true,
|
|
leaveOnEmptyCooldown: 300000,
|
|
leaveOnEnd: true,
|
|
leaveOnEndCooldown: 300000,
|
|
skipOnNoStream: true
|
|
})
|
|
}
|
|
try { if (!queue.connection) await queue.connect(voiceChannel) }
|
|
catch (error) { console.error(error); return interaction.followUp(`Y'a eu un problème, <@223831938346123275> ! (${error.message})`) }
|
|
|
|
let result = await player.search(query, { requestedBy: interaction.user })
|
|
if (!result.hasTracks()) return interaction.followUp(`Aucune musique trouvée pour **${query}** !`)
|
|
let track = result.tracks[0]
|
|
|
|
let entry = queue.tasksQueue.acquire()
|
|
await entry.getTask()
|
|
queue.addTrack(track)
|
|
|
|
// Write the values in the .env file to recover the player if the bot restarts
|
|
writeEnv('DISCORD_MUSIC_TEXTCHANNEL_ID', interaction.channel.id)
|
|
writeEnv('DISCORD_MUSIC_VOICECHANNEL_ID', voiceChannel.id)
|
|
|
|
try {
|
|
if (!queue.isPlaying()) await queue.node.play()
|
|
let track_source = track.source === 'youtube' ? 'Youtube' : track.source === 'spotify' ? 'Spotify' : 'Inconnu'
|
|
return interaction.followUp(`Chargement de la musique **${track.title}** de **${track.author}** sur **${track_source}**...`)
|
|
} catch (error) { console.error(error); return interaction.followUp(`Y'a eu un problème, <@223831938346123275> ! (${error.message})`) }
|
|
finally { queue.tasksQueue.release() }
|
|
}
|
|
} |