Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { Client, GatewayIntentBits } = require('discord.js');
- const { prefix, token } = require('./config.json');
- const ytdl = require('ytdl-core');
- const client = new Client({
- intents: [
- GatewayIntentBits.Guilds,
- GatewayIntentBits.GuildMessages,
- GatewayIntentBits.MessageContent,
- GatewayIntentBits.GuildMembers,
- ],
- });
- const queue = new Map();
- client.once('ready', () => {
- console.log('Bot is ready!');
- });
- client.on('messageCreate', async message => {
- if (!message.content.startsWith(prefix) || message.author.bot) return;
- const args = message.content.slice(prefix.length).trim().split(/ +/);
- const command = args.shift().toLowerCase();
- if (command === 'play') {
- execute(message, args);
- return;
- } else if (command === 'skip') {
- skip(message);
- return;
- } else if (command === 'stop') {
- stop(message);
- return;
- }
- });
- async function execute(message, args) {
- const voiceChannel = message.member.voice.channel;
- if (!voiceChannel) {
- return message.reply('vous devez être dans un canal vocal pour utiliser cette commande !');
- }
- const permissions = voiceChannel.permissionsFor(message.client.user);
- if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) {
- return message.reply('Je n\'ai pas la permission de me connecter ou de parler dans ce canal vocal !');
- }
- const songInfo = await ytdl.getInfo(args[0]);
- const song = {
- title: songInfo.videoDetails.title,
- url: songInfo.videoDetails.video_url,
- };
- if (!queue.has(message.guild.id)) {
- const queueContruct = {
- textChannel: message.channel,
- voiceChannel: voiceChannel,
- connection: null,
- songs: [],
- volume: 5,
- playing: true,
- };
- queue.set(message.guild.id, queueContruct);
- queueContruct.songs.push(song);
- try {
- const connection = await voiceChannel.join();
- queueContruct.connection = connection;
- play(message.guild, queueContruct.songs[0]);
- } catch (error) {
- console.error(`Je n'ai pas pu rejoindre le canal vocal : ${error}`);
- queue.delete(message.guild.id);
- return message.channel.send(`Je n'ai pas pu rejoindre le canal vocal : ${error}`);
- }
- } else {
- queue.get(message.guild.id).songs.push(song);
- return message.channel.send(`${song.title} a été ajouté à la file d'attente !`);
- }
- }
- function skip(message) {
- if (!message.member.voice.channel) {
- return message.reply('vous devez être dans un canal vocal pour utiliser cette commande !');
- }
- if (!queue.has(message.guild.id)) {
- return message.reply('il n\'y a pas de musique dans la file d\'attente !');
- }
- queue.get(message.guild.id).connection.dispatcher.end();
- }
- function stop(message) {
- if (!message.member.voice.channel) {
- return message.reply('vous devez être dans un canal vocal pour utiliser cette commande !');
- }
- if (!queue.has(message.guild.id)) {
- return message.reply('il n\'y a pas de musique dans la file d\'attente !');
- }
- queue.get(message.guild.id).songs = [];
- queue.get(message.guild.id).connection.dispatcher.end();
- }
- function play(guild, song) {
- const serverQueue = queue.get(guild.id);
- if (!song) {
- serverQueue.voiceChannel.leave();
- queue.delete(guild.id);
- return;
- }
- const dispatcher = serverQueue.connection
- .play(ytdl(song.url))
- .on('finish', () => {
- serverQueue.songs.shift();
- play(guild, serverQueue.songs[0]);
- })
- .on('error', error => console.error(error));
- dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
- serverQueue.textChannel.send(`Lecture en cours : **${song.title}**`);
- }
- client.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement