Advertisement
W1ngzy

Discord Music Bot by W1ngzy

Apr 23rd, 2020
10,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //if missing dependencies or any other error run the following
  2. //make sure node.js is installed
  3.  
  4. //npm install discord.js ffmpeg fluent-ffmpeg @discordjs/opus ytdl-core ytsr axios --save
  5.  
  6.  
  7. const Discord = require("discord.js")
  8. const ytsr = require('ytsr');
  9. const ytdl = require("ytdl-core");
  10. const token = "BOT-Token"
  11. const client = new Discord.Client();
  12. const queue = new Map();
  13. //const axios = require("axios")
  14. let prefix = "!"
  15. let dispatcher = null;
  16. let storedVolume = 0.25; // sets default volume to 25%
  17.  
  18.  
  19. //set status after init complete
  20. client.on("ready", () => {
  21.   console.log("Ready");
  22.   client.user.setStatus("ONLINE")
  23.   client.user.setPresence({ activity: { name: '!help' }, status: 'available' })
  24. });
  25.  
  26. //notify upon conn lost
  27. client.once("reconnecting", () => {
  28.   console.log("Reconnecting!");
  29. });
  30.  
  31. //notify upon disconnect
  32. client.once("disconnect", () => {
  33.   console.log("Disconnect!");
  34. });
  35.  
  36.  
  37. //respond whenever a message is received with prefix
  38. client.on("message", async message => {
  39.   if (message.author.bot) return;
  40.   if (!message.content.startsWith(prefix)) return;
  41.  
  42.   const serverQueue = queue.get(message.guild.id);
  43.  
  44.  
  45.   //execute specific funtion based on input command
  46.   if (message.content.startsWith(`${prefix}play`)) execute(message, serverQueue);
  47.     else if (message.content.startsWith(`${prefix}skip`)) skip(message, serverQueue);
  48.     else if (message.content.startsWith(`${prefix}stop`)) stop(message, serverQueue);
  49.     else if (message.content.startsWith(`${prefix}volume`)) volume(message, serverQueue);
  50.     else if (message.content.startsWith(`${prefix}help`)) help(message, serverQueue);
  51.     else if (message.content.startsWith(`${prefix}join`)) join(message, serverQueue);
  52.     else if (message.content.startsWith(`${prefix}leave`)) leave(message, serverQueue);
  53.     else if (message.content.startsWith(`${prefix}member`)) member(message, serverQueue);
  54.     else if (message.content.startsWith(`${prefix}resume`)) resume(message, serverQueue);
  55.     else
  56.     message.channel.send("You need to enter a valid command!");
  57.  
  58. });
  59.  
  60. async function execute(message, serverQueue) {
  61.  
  62.   const msg = message.content.substr(message.content.indexOf(" "));
  63.  
  64.   //test what condiiotn to test for
  65.  
  66.   /*console.log(s);
  67.   console.log("spotify link = " + (s.search("spotify") > -1));
  68.   console.log("Getlink = " + (s.search("http") === -1))
  69.   console.log("is Link = " + ((s.search("spotify") === -1) && !(s.search("http") === -1)));*/
  70.  
  71.   if(msg.search("spotify") > -1)
  72.     return message.channel.send("Spotify not supported yet");
  73.   if(msg.search("http") === -1){
  74.     //search youtube for url
  75.     var options = {
  76.       limit: 1,
  77.     }
  78.     const res = await ytsr(msg,options);
  79.     result = await res.items[0].link;
  80.     }
  81.   if((msg.search("spotify") === -1) && !(msg.search("http") === -1)) {
  82.     result = msg;
  83.   }
  84.  
  85.  
  86.   const voiceChannel = message.member.voice.channel;
  87.   if (!voiceChannel)
  88.     return message.channel.send(
  89.       "You need to be in a voice channel to play music!"
  90.     );
  91.   const permissions = voiceChannel.permissionsFor(message.client.user);
  92.   if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
  93.     return message.channel.send(
  94.       "I need the permissions to join and speak in your voice channel!"
  95.     );
  96.   }
  97.  
  98.   const songInfo = await ytdl.getInfo(result);
  99.   const song = {
  100.     title: songInfo.title,
  101.     url: songInfo.video_url
  102.   };
  103.  
  104.   if (!serverQueue) {
  105.     const queueContruct = {
  106.       textChannel: message.channel,
  107.       voiceChannel: voiceChannel,
  108.       connection: null,
  109.       songs: [],
  110.       volume: 0.25,
  111.       playing: true
  112.     };
  113.  
  114.     queue.set(message.guild.id, queueContruct);
  115.  
  116.     queueContruct.songs.push(song);
  117.  
  118.     try {
  119.       var connection = await voiceChannel.join();
  120.       queueContruct.connection = connection;
  121.       play(message.guild, queueContruct.songs[0]);
  122.     } catch (err) {
  123.       console.log(err);
  124.       queue.delete(message.guild.id);
  125.       return message.channel.send(err);
  126.     }
  127.   } else {
  128.     serverQueue.songs.push(song);
  129.     return message.channel.send(`${song.title} has been added to the queue!`);
  130.   }
  131. }
  132.  
  133.  
  134. function dhm (t,user,members) {
  135.  
  136.   // Thank you to https://stackoverflow.com/users/166491/mic
  137.   // for creating this code
  138.  
  139.   let cd = 24 * 60 * 60 * 1000;
  140.   let ch = 60 * 60 * 1000;
  141.   let d = Math.floor(t / cd);
  142.   let h = Math.floor( (t - d * cd) / ch);
  143.   let m = Math.round( (t - d * cd - h * ch) / 60000);
  144.      
  145.  
  146.   if( m === 60 ){
  147.     h++;
  148.     m = 0;
  149.   }
  150.   if( h === 24 ){
  151.     d++;
  152.     h = 0;
  153.   }
  154.   return (user + " has been a member of this server for: " + d + " Days | " + h + " Hours | and " + m + " Minutes with a total of " + members + " members");
  155.  
  156. }
  157.  
  158. function member(message) {
  159.   let age = Date.now() - Date.parse(message.member.joinedAt);
  160.   let user = message.author.username;
  161.   let members = message.member.guild.memberCount;
  162.  
  163.   return message.channel.send(dhm(age,user,members));
  164. }
  165.  
  166. function skip(message, serverQueue) {
  167.   if (!message.member.voice.channel)
  168.     return message.channel.send(
  169.       "You have to be in a voice channel to stop the music!"
  170.     );
  171.   if (!serverQueue)
  172.     return message.channel.send("There is no song that I could skip!");
  173.   serverQueue.connection.dispatcher.end();
  174. }
  175.  
  176. function stop(message, serverQueue) {
  177.   if (!message.member.voice.channel)
  178.     return message.channel.send(
  179.       "You have to be in a voice channel to stop the music!"
  180.     );
  181.   message.channel.send("Audio playback paused!");
  182.   serverQueue.connection.dispatcher.pause();
  183. }
  184.  
  185. function resume(message, serverQueue) {
  186.   if (!message.member.voice.channel)
  187.     return message.channel.send(
  188.       "You have to be in a voice channel to stop the music!"
  189.     );
  190.   message.channel.send("Audio playback resumed!");
  191.   serverQueue.connection.dispatcher.resume();
  192. }
  193.  
  194.  
  195. function join (message, serverQueue) {
  196.   if(!message.member.voice.channel) return message.channel.send("You have to be in a voice channel for me to join");
  197.   message.member.voice.channel.join();
  198.   return message.channel.send("I have joined your voice channel");
  199. }
  200.  
  201. function leave (message, serverQueue) {
  202.   if(!message.member.voice.channel) return message.channel.send("You have to be in a voice channel for me to leave");
  203.   if(!serverQueue){
  204.     message.member.voice.channel.leave();
  205.     return message.channel.send("I have left your voice channel");
  206.   } else {
  207.   serverQueue.connection.dispatcher.end();
  208.   return message.channel.send("I have left your voice channel");
  209.   }
  210. }
  211.  
  212. function play(guild, song) {
  213.   if(storedVolume > 1){
  214.     storedVolume = 0.25;
  215.   }
  216.   const serverQueue = queue.get(guild.id);
  217.   if (!song) {
  218.     serverQueue.voiceChannel.leave();
  219.     queue.delete(guild.id);
  220.     return;
  221.   }
  222.  
  223.   dispatcher = serverQueue.connection
  224.     .play(ytdl(song.url), {volumeEditable: true})
  225.     .on("finish", () => {
  226.       serverQueue.songs.shift();
  227.       play(guild, serverQueue.songs[0]);
  228.     })
  229.     .on("error", error => console.error(error));
  230.   dispatcher.setVolume(storedVolume);
  231.   serverQueue.textChannel.send(`Started playing: **${song.title}**`);
  232. }
  233.  
  234.  
  235. function volume(message, serverQueue) {
  236.     const args = message.content.split(" ");
  237.     let v = (args[1]/100);
  238.  
  239.     if(!dispatcher){
  240.       storedVolume = v;
  241.       message.channel.send(`Changed volume to: **${storedVolume*100}%**`);
  242.     }else {
  243.       if(v>1){
  244.         v = 1;
  245.         dispatcher.setVolume(v);
  246.         serverQueue.textChannel.send(`Changed volume to: **${v*100}%**`);
  247.       } else if (v < 0) {
  248.           v = 0;
  249.           dispatcher.setVolume(v);
  250.           serverQueue.textChannel.send(`Changed volume to: **${v*100}%**`);
  251.       } else {
  252.           dispatcher.setVolume(v);
  253.           serverQueue.textChannel.send(`Changed volume to: **${v*100}%**`);
  254.       }
  255.     }
  256. }
  257.  
  258. function help(message, serverQueue) {
  259.     let help = `Available Commands:
  260.  
  261.     1. !join - Let me join your current channel.
  262.     2. !leave - I will leave your channel.
  263.     3. !play - plays either a youtube link or searchers for a song manually.
  264.     4. !skip - skips current song.
  265.     5. !stop - stop all music and kicks bot.
  266.     6. !volume - !volume <number> - change volume of bot.
  267.     7. !member - See how long you have been a member of this server.
  268.     8. !help - display this help screen.`
  269.  
  270.     message.author.send(help);
  271.     return;
  272. }
  273.  
  274. client.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement