Réécriture complète en Typescript
This commit is contained in:
91
src/commands/player/play.ts
Executable file
91
src/commands/player/play.ts
Executable file
@@ -0,0 +1,91 @@
|
||||
import { SlashCommandBuilder, ChatInputCommandInteraction, AutocompleteInteraction, GuildMember } from 'discord.js'
|
||||
import { useMainPlayer, useQueue, QueryType } from 'discord-player'
|
||||
import writeEnv from '../../utils/writeEnv'
|
||||
|
||||
export interface TrackSearchResult { name: string, value: string }
|
||||
|
||||
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: AutocompleteInteraction) {
|
||||
let query = interaction.options.getString('recherche', true)
|
||||
if (!query) return interaction.respond([])
|
||||
|
||||
let player = useMainPlayer()
|
||||
|
||||
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: TrackSearchResult[] = []
|
||||
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: ChatInputCommandInteraction) {
|
||||
let member = interaction.member as GuildMember
|
||||
let voiceChannel = 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 = useMainPlayer()
|
||||
let queue = useQueue(interaction.guild?.id ?? '')
|
||||
|
||||
if (!queue) {
|
||||
if (interaction.guild) 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: 30000,
|
||||
leaveOnEnd: true,
|
||||
leaveOnEndCooldown: 300000
|
||||
})
|
||||
else return
|
||||
}
|
||||
try { if (!queue.connection) await queue.connect(voiceChannel) }
|
||||
catch (error: any) { console.error(error); return interaction.followUp(`Y'a eu un problème, <@223831938346123275> ! (${error.message})`) }
|
||||
|
||||
// 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)
|
||||
|
||||
// Search the song
|
||||
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]
|
||||
console.log(track.duration)
|
||||
console.log(track.durationMS)
|
||||
|
||||
let entry = queue.tasksQueue.acquire()
|
||||
await entry.getTask()
|
||||
queue.addTrack(track)
|
||||
|
||||
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: any) { console.error(error); return interaction.followUp(`Y'a eu un problème, <@223831938346123275> ! (${error.message})`) }
|
||||
finally { queue.tasksQueue.release() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user