Advertisement
Guest User

index.js

a guest
Jul 4th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | Source Code | 0 0
  1. const { Client, GatewayIntentBits } = require('discord.js');
  2. const { prefix, token } = require('./config.json');
  3. const ytdl = require('ytdl-core');
  4.  
  5. const client = new Client({
  6. intents: [
  7. GatewayIntentBits.Guilds,
  8. GatewayIntentBits.GuildMessages,
  9. GatewayIntentBits.MessageContent,
  10. GatewayIntentBits.GuildMembers,
  11. ],
  12. });
  13.  
  14. const queue = new Map();
  15.  
  16. client.once('ready', () => {
  17. console.log('Bot is ready!');
  18. });
  19.  
  20. client.on('messageCreate', async message => {
  21. if (!message.content.startsWith(prefix) || message.author.bot) return;
  22.  
  23. const args = message.content.slice(prefix.length).trim().split(/ +/);
  24. const command = args.shift().toLowerCase();
  25.  
  26. if (command === 'play') {
  27. execute(message, args);
  28. return;
  29. } else if (command === 'skip') {
  30. skip(message);
  31. return;
  32. } else if (command === 'stop') {
  33. stop(message);
  34. return;
  35. }
  36. });
  37.  
  38. async function execute(message, args) {
  39. const voiceChannel = message.member.voice.channel;
  40. if (!voiceChannel) {
  41. return message.reply('vous devez être dans un canal vocal pour utiliser cette commande !');
  42. }
  43.  
  44. const permissions = voiceChannel.permissionsFor(message.client.user);
  45. if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) {
  46. return message.reply('Je n\'ai pas la permission de me connecter ou de parler dans ce canal vocal !');
  47. }
  48.  
  49. const songInfo = await ytdl.getInfo(args[0]);
  50. const song = {
  51. title: songInfo.videoDetails.title,
  52. url: songInfo.videoDetails.video_url,
  53. };
  54.  
  55. if (!queue.has(message.guild.id)) {
  56. const queueContruct = {
  57. textChannel: message.channel,
  58. voiceChannel: voiceChannel,
  59. connection: null,
  60. songs: [],
  61. volume: 5,
  62. playing: true,
  63. };
  64.  
  65. queue.set(message.guild.id, queueContruct);
  66. queueContruct.songs.push(song);
  67.  
  68. try {
  69. const connection = await voiceChannel.join();
  70. queueContruct.connection = connection;
  71. play(message.guild, queueContruct.songs[0]);
  72. } catch (error) {
  73. console.error(`Je n'ai pas pu rejoindre le canal vocal : ${error}`);
  74. queue.delete(message.guild.id);
  75. return message.channel.send(`Je n'ai pas pu rejoindre le canal vocal : ${error}`);
  76. }
  77. } else {
  78. queue.get(message.guild.id).songs.push(song);
  79. return message.channel.send(`${song.title} a été ajouté à la file d'attente !`);
  80. }
  81. }
  82.  
  83. function skip(message) {
  84. if (!message.member.voice.channel) {
  85. return message.reply('vous devez être dans un canal vocal pour utiliser cette commande !');
  86. }
  87. if (!queue.has(message.guild.id)) {
  88. return message.reply('il n\'y a pas de musique dans la file d\'attente !');
  89. }
  90. queue.get(message.guild.id).connection.dispatcher.end();
  91. }
  92.  
  93. function stop(message) {
  94. if (!message.member.voice.channel) {
  95. return message.reply('vous devez être dans un canal vocal pour utiliser cette commande !');
  96. }
  97. if (!queue.has(message.guild.id)) {
  98. return message.reply('il n\'y a pas de musique dans la file d\'attente !');
  99. }
  100. queue.get(message.guild.id).songs = [];
  101. queue.get(message.guild.id).connection.dispatcher.end();
  102. }
  103.  
  104. function play(guild, song) {
  105. const serverQueue = queue.get(guild.id);
  106. if (!song) {
  107. serverQueue.voiceChannel.leave();
  108. queue.delete(guild.id);
  109. return;
  110. }
  111.  
  112. const dispatcher = serverQueue.connection
  113. .play(ytdl(song.url))
  114. .on('finish', () => {
  115. serverQueue.songs.shift();
  116. play(guild, serverQueue.songs[0]);
  117. })
  118. .on('error', error => console.error(error));
  119. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  120.  
  121. serverQueue.textChannel.send(`Lecture en cours : **${song.title}**`);
  122. }
  123.  
  124. client.login(token);
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement