Advertisement
JohnDog3112

Music Bot

Sep 6th, 2018
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. const Discord = require("discord.js");
  2. const client = new Discord.Client();
  3. const config = require("./config.json");
  4. const ytdl = require("ytdl-core");
  5. const queue = new Map();
  6. var datas;
  7. const fs = require("fs");
  8. const mutes = new Map();
  9. /*if (!fs.existsSync('./serverConfigs.json')) {
  10. const serverConfigConstruct = {};
  11. fs.writeFileSync('./serverConfigs.json', JSON.stringify(serverConfigConstruct) , (err) => {
  12. console.log(err);
  13. });
  14. */
  15. //}
  16. //fs.readFileSync('./serverConfigs.json', (err, data) => {
  17. // if (err) throw err;
  18. // datas = data;
  19. //});
  20. //const guildSettings = JSON.parse(datas);
  21.  
  22. client.on('ready', async () => {
  23. console.log("Ready");
  24.  
  25.  
  26. });
  27. client.on("guildCreate", guild => {
  28. console.log("Joined a new guild: " + guild.name);
  29. //const guildConstruct = {
  30. // volume: 5,
  31. // prefix: "!?!"
  32. // };
  33. // guildSettings.set(guild.id, guildConstruct);
  34. let chan = 0;
  35. var parentch;
  36. guild.channels.forEach((channel) => {
  37. if (channel.type == "text" && channel.name == "bots") {
  38. chan = 1;
  39. } else {
  40. parentch = channel.parent;
  41. }
  42. });
  43. if (chan == 0 && parentch != undefined) {
  44. guild.createChannel("bots", "text");
  45. }
  46.  
  47. });
  48. client.on("guildDelete", guild => {
  49. console.log("Left a guild: " + guild.name);
  50. // guildSettings.delete(guild.id);
  51. })
  52. client.on('message', async msg => {
  53. if (msg.author.bot) {
  54. return undefined;
  55. }
  56. if (msg.channel.name == "bots") {
  57. if (!msg.content.startsWith(config.prefix)) return undefined;
  58. msg.delete();
  59. let args = msg.content.split(' ');
  60. const command = args[0].slice(config.prefix.length);
  61. args = args.slice(1);
  62. var serverQueue = queue.get(msg.guild.id);
  63. if (command == "play") {
  64. const voiceChannel = msg.member.voiceChannel;
  65. if (!voiceChannel) return msg.channel.send('I\'m sorry, but you need to be in a voice channel to play music!');
  66. const permissions = voiceChannel.permissionsFor(msg.client.user);
  67. if (!permissions.has('CONNECT')){
  68. return msg.channel.send("I don't have permission to join your voice channel.");
  69. }
  70. if (!permissions.has('SPEAK')){
  71. return msg.channel.send("I don't have permission to speak in your voice channel.")
  72. }
  73. const songInfo = await ytdl.getInfo(args[0]);
  74. const song = {
  75. title: songInfo.title,
  76. url: songInfo.video_url,
  77. id: msg.author
  78. };
  79.  
  80. if (!serverQueue) {
  81. const queueConstruct = {
  82. textChannel: msg.channel,
  83. voiceChannel: voiceChannel,
  84. connection: null,
  85. songs: [],
  86. volume: 2,
  87. playing: true,
  88. loop: false
  89. };
  90. queue.set(msg.guild.id, queueConstruct);
  91. queueConstruct.songs.push(song);
  92.  
  93.  
  94. try {
  95. var connection = await voiceChannel.join();
  96. queueConstruct.connection = connection;
  97. play(msg.guild, queueConstruct.songs[0]);
  98. return msg.channel.send(`✅ **${song.title}** has been added to the queue!`);
  99. } catch(error) {
  100. voiceChannel.leave();
  101. queue.delete(msg.guild);
  102. console.error(error);
  103. return msg.channel.send(error);
  104. }
  105.  
  106. } else {
  107. serverQueue.songs.push(song);
  108. console.log(serverQueue.songs);
  109. return msg.channel.send(`✅ **${song.title}** has been added to the queue!`);
  110.  
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. } else if (command == "mute") {
  119.  
  120.  
  121. }else if (command == "stop") {
  122. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  123. if (!serverQueue) return msg.channel.send('There is nothing playing that I could stop for you.');
  124. if (serverQueue.songs[0].id.username != msg.author.username) return msg.channel.send('no');
  125. serverQueue.songs = [];
  126. serverQueue.connection.dispatcher.end('Stop command has been used!');
  127. return undefined;
  128. } else if (command == "volume") {
  129. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  130. if (!serverQueue) return msg.channel.send('There is nothing playing.');
  131. if (!args[0]) return msg.channel.send(`The current volume is: **${serverQueue.volume}**`);
  132. serverQueue.volume = args[0];
  133. serverQueue.connection.dispatcher.setVolumeLogarithmic(args[0] / 5);
  134. return msg.channel.send(`I set the volume to: **${args[0]}**`);
  135. } else if (command == "loop") {
  136. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  137. if (!serverQueue) return msg.channel.send('There is nothing playing.');
  138. if (!serverQueue.loop == false) return msg.channel.send('The queue is already looping!');
  139. serverQueue.loop = true;
  140. return msg.channel.send('Queue is now repeating');
  141. } else if (command == "unloop") {
  142. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  143. if (!serverQueue)return msg.channel.send('There is nothing playing.');
  144. if (!serverQueue.loop == true) return msg.channel.send('The Queue isnI\'t looping!');
  145. serverQueue.loop = false;
  146. return msg.channel.send('The Queus is no longer repeating');
  147. } else if (command === "skip") {
  148. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  149. if (!serverQueue) return msg.channel.send('There is nothing playing that I could skip for you.');
  150. serverQueue.connection.dispatcher.end('Skip command has been used!');
  151. return undefined;
  152. } else if (command == "pause") {
  153. if (serverQueue && serverQueue.playing) {
  154. serverQueue.playing = false;
  155. serverQueue.connection.dispatcher.pause();
  156. return msg.channel.send('⏸ Paused the music for you!');
  157. }
  158. return msg.channel.send('There is nothing playing.');
  159. } else if (command == "resume"){
  160. if (serverQueue && !serverQueue.playing) {
  161. serverQueue.playing = true;
  162. serverQueue.connection.dispatcher.resume();
  163. return msg.channel.send('▶ Resumed the music for you!');
  164. }
  165. return msg.channel.send('There is nothing playing.');
  166. } else if (command === 'queue') {
  167. if (!serverQueue) return msg.channel.send('There is nothing playing.');
  168. return msg.channel.send(`
  169. __**Song queue:**__
  170. ${serverQueue.songs.map(song => `**-** ${song.title}`).join('\n')}
  171. **Now playing:** ${serverQueue.songs[0].title}
  172. `);
  173. } else if (command == 'test') {
  174. console.log(msg.member.id);
  175. } else {
  176. return console.log("not a command");
  177. }
  178. }
  179. });
  180.  
  181. function play(guild, song) {
  182. const serverQueue = queue.get(guild.id);
  183. if (!song) {
  184. serverQueue.voiceChannel.leave();
  185. queue.delete(guild.id);
  186. return;
  187. }
  188. const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  189. .on('end', () => {
  190. console.log('song ended');
  191. if (serverQueue.loop == true){
  192. serverQueue.songs.push(serverQueue.songs[0]);
  193. }
  194. serverQueue.songs.shift();
  195. play(guild, serverQueue.songs[0])
  196.  
  197. })
  198. .on('error', error => {
  199. console.error(error);
  200. });
  201. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  202. }
  203. client.login(config.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement