Advertisement
ItzDerock

Discord.js Music Bot

Jun 12th, 2019
14,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. const Discord = require("discord.js");
  2.  
  3. const bot = new Discord.Client();
  4.  
  5. const config = require("./config.json");
  6. const ytdl = require("ytdl-core");
  7.  
  8. const prefix = config.prefix;
  9.  
  10. var queue = new Map();
  11.  
  12. bot.on("ready", () => {
  13. console.log(`I am ready! I am in ${bot.guilds.size} guilds`);
  14.  
  15. bot.user.setActivity(`Hello!`);
  16. });
  17.  
  18. bot.on("message", async message => {
  19. if(message.author.bot) return;
  20. if(message.content.indexOf(prefix) !== 0) return;
  21.  
  22. const args = message.content.slice(prefix.length).trim().split(/ +/g);
  23. const command = args.shift().toLowerCase();
  24.  
  25. const serverQueue = queue.get(message.guild.id);
  26.  
  27. if(command === 'hello') {
  28. message.reply('Hello!');
  29. }
  30.  
  31. if(command === 'ping') {
  32. const msg = await message.channel.send("Pinging...");
  33. msg.edit(`Pong! Latency is ${msg.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(bot.ping)}ms`);
  34. }
  35.  
  36. if(command === 'kick') {
  37. if(!message.member.hasPermission("ADMINISTRATOR")) return message.reply('Sorry you do not have permission!');
  38. let member = message.mentions.members.first() || message.guild.members.get(args[0]);
  39. if(!member) return message.reply("Please mention a valid user");
  40. if(!member.kickable) return message.channel.send("Sorry I cannot kick that person! Do they have a higher role?");
  41.  
  42. let reason = args.slice(1).join(' ');
  43. if(!reason) reason = "No reason provided";
  44.  
  45. await member.kick(reason)
  46. .catch(e => message.reply(`Sorry I couldn't kick them! Error: ${e}`));
  47. message.reply(`:white_check_mark: User kicked!`);
  48. }
  49.  
  50. if(command === 'ban') {
  51. if(!message.member.hasPermission("ADMINISTRATOR")) return message.reply('Sorry you do not have permission!');
  52. let member = message.mentions.members.first() || message.guild.members.get(args[0]);
  53. if(!member) return message.reply("Please mention a valid user");
  54. if(!member.bannable) return message.channel.send("Sorry I cannot ban that person! Do they have a higher role?");
  55.  
  56. let reason = args.slice(1).join(' ');
  57. if(!reason) reason = "No reason provided";
  58.  
  59. await member.ban(reason)
  60. .catch(e => message.reply(`Sorry I couldn't ban them! Error: ${e}`));
  61. message.reply(`:white_check_mark: User banned!`);
  62. }
  63.  
  64. if(command === 'play') {
  65. // !play url
  66.  
  67. play(message, serverQueue);
  68. }
  69.  
  70. });
  71.  
  72. async function play(message, serverQueue) {
  73. const args = message.content.split(" ");
  74.  
  75. const voiceChannel = message.member.voiceChannel;
  76. if(!voiceChannel) return message.reply("You must be in a voice channel!");
  77. const permission = voiceChannel.permissionsFor(message.client.user);
  78. if(!permission.has('CONNECT') || !permission.has("SPEAK")) {
  79. return message.channel.send("I need permission to join and speak in your voice channel!")
  80. }
  81.  
  82. const songInfo = await ytdl.getInfo(args[1]);
  83. const song = {
  84. title: songInfo.title,
  85. url: songInfo.video_url,
  86. };
  87.  
  88. if(!serverQueue) {
  89. const queueConstruct = {
  90. textChannel: message.channel,
  91. voiceChannel: voiceChannel,
  92. connection: null,
  93. songs: [],
  94. volume: 5,
  95. playing: true,
  96. };
  97. queue.set(message.guild.id, queueConstruct);
  98.  
  99. queueConstruct.songs.push(song);
  100.  
  101. try{
  102. var connection = await voiceChannel.join();
  103. queueConstruct.connection = connection;
  104. playSong(message.guild, queueConstruct.songs[0]);
  105. } catch (err) {
  106. console.log(err);
  107. queue.delete(message.guild.id)
  108. return message.channel.send("There was an error playing! " + err);
  109. }
  110. } else {
  111. serverQueue.songs.push(song);
  112. return message.channel.send(`${song.title} has been added to the queue!`);
  113. }
  114. }
  115.  
  116. function playSong(guild, song) {
  117. const serverQueue = queue.get(guild.id);
  118.  
  119. if(!song) {
  120. serverQueue.voiceChannel.leave();
  121. queue.delete(guild.id);
  122. return;
  123. }
  124.  
  125. const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  126. .on('end', () => {
  127. serverQueue.songs.shift();
  128. playSong(guild, serverQueue.songs[0]);
  129. })
  130. .on('error', error => {
  131. console.log(error);
  132. })
  133. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  134. }
  135.  
  136.  
  137. bot.login(config.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement