SHARE
TWEET

fix

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