Réécriture complète en Typescript
This commit is contained in:
		
							
								
								
									
										21
									
								
								src/commands/player/loop.ts
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										21
									
								
								src/commands/player/loop.ts
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js' | ||||
| import { useQueue } from'discord-player' | ||||
|  | ||||
| module.exports = { | ||||
| 	data: new SlashCommandBuilder() | ||||
| 		.setName('loop') | ||||
| 		.setDescription('Boucler la musique en cours de lecture.') | ||||
| 		.addIntegerOption(option => option.setName('loop') | ||||
| 			.setDescription('Mode de boucle (0 = Off, 1 = Titre, 2 = File d\'Attente; 3 = Autoplay)') | ||||
| 			.setRequired(true) | ||||
| 			.setMinValue(0) | ||||
| 			.setMaxValue(3)), | ||||
| 	async execute(interaction: ChatInputCommandInteraction) { | ||||
| 		let loop = interaction.options.getInteger('loop') | ||||
| 		let queue = useQueue(interaction.guild?.id ?? '') | ||||
| 		if (!queue) return interaction.followUp({ content: 'Aucune file d\'attente en cours, recherche une musique plutôt !' }) | ||||
| 		 | ||||
| 		queue.setRepeatMode(loop as number) | ||||
| 		return await interaction.reply(`Boucle ${loop === 0 ? 'désactivée' : loop === 1 ? 'en mode Titre' : loop === 2 ? 'en mode File d\'Attente' : 'en autoplay'}.`) | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										45
									
								
								src/commands/player/lyrics.ts
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										45
									
								
								src/commands/player/lyrics.ts
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,45 @@ | ||||
| import { ChatInputCommandInteraction, SlashCommandBuilder, EmbedBuilder } from 'discord.js' | ||||
| import { useQueue } from 'discord-player' | ||||
| import { lyricsExtractor } from '@discord-player/extractor' | ||||
|  | ||||
| module.exports = { | ||||
| 	data: new SlashCommandBuilder() | ||||
| 		.setName('lyrics') | ||||
| 		.setDescription('Rechercher les paroles d\'une musique.') | ||||
| 		.addStringOption(option => option.setName('recherche').setDescription('Chercher une musique spécifique')), | ||||
| 	async execute(interaction: ChatInputCommandInteraction) { | ||||
| 		await interaction.deferReply() | ||||
|  | ||||
| 		let query = interaction.options.getString('recherche', false) | ||||
| 		if (!query) { | ||||
| 			let queue = useQueue(interaction.guild?.id ?? '') | ||||
| 			if (!queue) return interaction.followUp({ content: 'Aucune file d\'attente en cours, recherche une musique plutôt !' }) | ||||
| 			let track = queue.currentTrack | ||||
| 			if (!track) return interaction.followUp({ content: 'Aucune musique en cours, recherche en une plutôt !' }) | ||||
| 			 | ||||
| 			if (track.raw.source === 'spotify') query = `${track.author} ${track.title}` | ||||
| 			else query = track.title | ||||
| 		} | ||||
|  | ||||
| 		let lyricsFinder = lyricsExtractor() | ||||
|  | ||||
| 		let lyrics = await lyricsFinder.search(query).catch(() => null) | ||||
| 		if (!lyrics) return interaction.followUp({ content: 'Pas de paroles trouvées !' }) | ||||
|  | ||||
| 		let trimmedLyrics = lyrics.lyrics.substring(0, 1997) | ||||
|  | ||||
| 		let embed = new EmbedBuilder() | ||||
| 			.setColor('#ffc370') | ||||
| 			.setTitle(lyrics.title) | ||||
| 			.setURL(lyrics.url) | ||||
| 			.setThumbnail(lyrics.thumbnail) | ||||
| 			.setAuthor({ | ||||
| 				name: lyrics.artist.name, | ||||
| 				iconURL: lyrics.artist.image, | ||||
| 				url: lyrics.artist.url | ||||
| 			}) | ||||
| 			.setDescription(trimmedLyrics.length === 1997 ? `${trimmedLyrics}...` : trimmedLyrics) | ||||
|  | ||||
| 		return interaction.followUp({ embeds: [embed] }) | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										15
									
								
								src/commands/player/pause.ts
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										15
									
								
								src/commands/player/pause.ts
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js' | ||||
| import { useQueue } from 'discord-player' | ||||
|  | ||||
| module.exports = { | ||||
| 	data: new SlashCommandBuilder() | ||||
| 		.setName('pause') | ||||
| 		.setDescription('Met en pause la musique.'), | ||||
| 	async execute(interaction: ChatInputCommandInteraction) { | ||||
| 		let queue = useQueue(interaction.guild?.id ?? '') | ||||
| 		if (!queue) return interaction.followUp({ content: 'Aucune file d\'attente en cours, recherche une musique plutôt !' }) | ||||
| 		 | ||||
| 		queue.node.setPaused(!queue.node.isPaused()) | ||||
| 		return await interaction.reply('Musique mise en pause !') | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										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() } | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										15
									
								
								src/commands/player/previous.ts
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										15
									
								
								src/commands/player/previous.ts
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js' | ||||
| import { useHistory } from 'discord-player' | ||||
|  | ||||
| module.exports = { | ||||
| 	data: new SlashCommandBuilder() | ||||
| 		.setName('previous') | ||||
| 		.setDescription('Joue la musique précédente.'), | ||||
| 	async execute(interaction: ChatInputCommandInteraction) { | ||||
| 		let history = useHistory(interaction.guild?.id ?? '') | ||||
| 		if (!history) return await interaction.reply('Il n\'y a pas d\'historique de musique !') | ||||
|  | ||||
| 		await history.previous() | ||||
| 		return await interaction.reply('Musique précédente jouée !') | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										15
									
								
								src/commands/player/resume.ts
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										15
									
								
								src/commands/player/resume.ts
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js' | ||||
| import { useQueue } from 'discord-player' | ||||
|  | ||||
| module.exports = { | ||||
| 	data: new SlashCommandBuilder() | ||||
| 		.setName('resume') | ||||
| 		.setDescription('Reprendre la musique.'), | ||||
| 	async execute(interaction: ChatInputCommandInteraction) { | ||||
| 		let queue = useQueue(interaction.guild?.id ?? '') | ||||
| 		if (!queue) return interaction.followUp({ content: 'Aucune file d\'attente en cours, recherche une musique plutôt !' }) | ||||
| 		 | ||||
| 		queue.node.setPaused(!queue.node.isPaused()) | ||||
| 		return await interaction.reply('Musique reprise !') | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										15
									
								
								src/commands/player/shuffle.ts
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										15
									
								
								src/commands/player/shuffle.ts
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js' | ||||
| import { useQueue } from 'discord-player' | ||||
|  | ||||
| module.exports = { | ||||
| 	data: new SlashCommandBuilder() | ||||
| 		.setName('shuffle') | ||||
| 		.setDescription('Mélange la file d\'attente.'), | ||||
| 	async execute(interaction: ChatInputCommandInteraction) { | ||||
| 		let queue = useQueue(interaction.guild?.id ?? '') | ||||
| 		if (!queue) return interaction.followUp({ content: 'Aucune file d\'attente en cours, recherche une musique plutôt !' }) | ||||
| 		 | ||||
| 		queue.tracks.shuffle() | ||||
| 		return await interaction.reply('File d\'attente mélangée !') | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										15
									
								
								src/commands/player/skip.ts
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										15
									
								
								src/commands/player/skip.ts
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js' | ||||
| import { useQueue } from 'discord-player' | ||||
|  | ||||
| module.exports = { | ||||
| 	data: new SlashCommandBuilder() | ||||
| 		.setName('skip') | ||||
| 		.setDescription('Passer la musique en cours.'), | ||||
| 	async execute(interaction: ChatInputCommandInteraction) { | ||||
| 		let queue = useQueue(interaction.guild?.id ?? '') | ||||
| 		if (!queue) return interaction.followUp({ content: 'Aucune file d\'attente en cours, recherche une musique plutôt !' }) | ||||
| 		 | ||||
| 		queue.node.skip() | ||||
| 		return await interaction.reply('Musique passée !') | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										21
									
								
								src/commands/player/stop.ts
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										21
									
								
								src/commands/player/stop.ts
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js' | ||||
| import { useQueue } from 'discord-player' | ||||
| import writeEnv from '../../utils/writeEnv' | ||||
|  | ||||
| module.exports = { | ||||
| 	data: new SlashCommandBuilder() | ||||
| 		.setName('stop') | ||||
| 		.setDescription('Arrêter la musique.'), | ||||
| 	async execute(interaction: ChatInputCommandInteraction) { | ||||
| 		let queue = useQueue(interaction.guild?.id ?? '') | ||||
| 		if (!queue) return interaction.followUp({ content: 'Aucune file d\'attente en cours, recherche une musique plutôt !' }) | ||||
|  | ||||
| 		queue.delete() | ||||
| 		writeEnv('DISCORD_MUSIC_TEXTCHANNEL_ID', '') | ||||
| 		writeEnv('DISCORD_MUSIC_VOICECHANNEL_ID', '') | ||||
| 		writeEnv('DISCORD_MUSIC_CURRENT_TRACK', '') | ||||
| 		writeEnv('DISCORD_MUSIC_CURRENT_PROGRESS', '') | ||||
| 		 | ||||
| 		return await interaction.reply('Musique arrêtée !') | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										21
									
								
								src/commands/player/volume.ts
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										21
									
								
								src/commands/player/volume.ts
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js' | ||||
| import { useQueue } from 'discord-player' | ||||
|  | ||||
| module.exports = { | ||||
| 	data: new SlashCommandBuilder() | ||||
| 		.setName('volume') | ||||
| 		.setDescription('Modifie le volume de la musique.') | ||||
| 		.addIntegerOption(option => option.setName('volume') | ||||
| 			.setDescription('Le volume à mettre (%)') | ||||
| 			.setRequired(true) | ||||
| 			.setMinValue(1) | ||||
| 			.setMaxValue(100)), | ||||
| 	async execute(interaction: ChatInputCommandInteraction) { | ||||
| 		let volume = interaction.options.getInteger('volume') | ||||
| 		let queue = useQueue(interaction.guild?.id ?? '') | ||||
| 		if (!queue) return interaction.followUp({ content: 'Aucune file d\'attente en cours, recherche une musique plutôt !' }) | ||||
| 		 | ||||
| 		queue.node.setVolume(volume as number) | ||||
| 		return await interaction.reply(`Volume modifié à ${volume}% !`) | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user