Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. var discord = require("discord.js"); // discord library
  2. var ytdl = require('ytdl-core');
  3. var youtube = require('./youtube.js'); // performs youtube API requests
  4.  
  5. var bot = new discord.Client();
  6. var ytAudioQueue = [];
  7. var dispatcher = null;
  8.  
  9. bot.on('ready', function () {
  10. console.log('I am ready');
  11. });
  12.  
  13. bot.on('message', function (message) {
  14. var messageParts = message.content.split(' ');
  15.  
  16. var command = messageParts[0].toLowerCase();
  17. var parameters = messageParts.splice(1, messageParts.length);
  18.  
  19. console.log("command: " + command);
  20. console.log("parameters: " + parameters);
  21.  
  22. switch (command) {
  23. case "hi":
  24. message.reply("Hey there!");
  25. break;
  26. case "*help":
  27. HelpCommand(message);
  28. break;
  29. case "*join":
  30. message.reply("Attempting to join channel: " + parameters[0]);
  31. JoinCommand(parameters[0]);
  32. break;
  33. case "*play":
  34. PlayCommand(parameters.join(" "), message);
  35. break;
  36. case "*playqueue":
  37. PlayQueueCommand(message);
  38. break;
  39. //case "*leave":
  40. //LeaveCommand(voiceChannel);
  41. //break;
  42.  
  43. }
  44. });
  45.  
  46. /* COMMAND HANDLERS */
  47. //function LeaveCommand(voiceChannel) {
  48. //var voiceChannel = GetChannelByName(channelName);
  49. //if (message.content === "*leave") {
  50.  
  51. //voiceChannel.leave();
  52. //console.log("Bye!");
  53. //}
  54.  
  55. //}
  56. /// lists out all of the bot commands
  57. function HelpCommand(originalMessage) {
  58. originalMessage.reply("*join <channel-to-join> - Connects to bot to a channel by channel name");
  59. originalMessage.reply("*play <YouTube search term> - Plays audio from YouTube based on the search term");
  60. originalMessage.reply("*playqueue - Lists the audio remaining in the play queue");
  61. }
  62.  
  63. /// plays audio based on results from youtube search
  64. function PlayCommand(searchTerm) {
  65.  
  66. // if not connected to a voice channel then connect to first one
  67. if (bot.voiceConnections.array().length == 0) {
  68. var defaultVoiceChannel = bot.channels.find(val => val.type === 'voice').name;
  69. JoinCommand(defaultVoiceChannel);
  70. }
  71.  
  72. // search youtube using the given search search term and perform callback action if video is found
  73. youtube.search(searchTerm, QueueYtAudioStream);
  74. }
  75.  
  76. /// lists out all music queued to play
  77. function PlayQueueCommand(message) {
  78. var queueString = "";
  79.  
  80. for(var x = 0; x < ytAudioQueue.length; x++) {
  81. queueString += ytAudioQueue[x].videoName + ", ";
  82. }
  83.  
  84. queueString = queueString.substring(0, queueString.length - 2);
  85. message.reply(queueString);
  86. }
  87.  
  88. /// joins the bot to the specified voice channel
  89. function JoinCommand(channelName) {
  90. var voiceChannel = GetChannelByName(channelName);
  91.  
  92. if (voiceChannel) {
  93. voiceChannel.join();
  94. console.log("Joined " + voiceChannel.name);
  95. }
  96.  
  97. return voiceChannel;
  98. }
  99.  
  100.  
  101. /* END COMMAND HANDLERS */
  102. /*----------------------------------------------------------------------*/
  103. /* HELPER METHODS */
  104.  
  105. /// returns the channel that matches the name provided
  106. function GetChannelByName(name) {
  107. var channel = bot.channels.find(val => val.name === name);
  108. return channel;
  109. }
  110.  
  111.  
  112. /// Queues result of Youtube search into stream
  113. function QueueYtAudioStream(videoId, videoName) {
  114. var streamUrl = `${youtube.watchVideoUrl}${videoId}`;
  115.  
  116. if (!ytAudioQueue.length) {
  117. ytAudioQueue.push(
  118. {
  119. 'streamUrl': streamUrl,
  120. 'videoName': videoName
  121. }
  122. );
  123.  
  124. console.log("Queued audio " + videoName);
  125. PlayStream(ytAudioQueue[0].streamUrl);
  126. }
  127. else {
  128. ytAudioQueue.push(
  129. {
  130. 'streamUrl': streamUrl,
  131. 'videoName': videoName
  132. }
  133. );
  134.  
  135. console.log("Queued audio " + videoName);
  136. }
  137.  
  138. }
  139.  
  140. /// Plays a given stream
  141. function PlayStream(streamUrl) {
  142.  
  143. const streamOptions = {seek: 0, volume: 1};
  144.  
  145. if (streamUrl) {
  146. const stream = ytdl(streamUrl, {filter: 'audioonly'});
  147.  
  148. if (dispatcher == null) {
  149.  
  150. var voiceConnection = bot.voiceConnections.first();
  151. //console.log(voiceConnection);
  152.  
  153. if (voiceConnection) {
  154.  
  155. console.log("Now Playing " + ytAudioQueue[0].videoName);
  156. dispatcher = bot.voiceConnections.first().playStream(stream, streamOptions);
  157.  
  158. dispatcher.on('end', () => {
  159. PlayNextStreamInQueue();
  160. });
  161.  
  162. dispatcher.on('error', (err) => {
  163. console.log(err);
  164. });
  165. }
  166. }
  167. else {
  168. dispatcher = bot.voiceConnections.first().playStream(stream, streamOptions);
  169. }
  170. }
  171. }
  172.  
  173. /// Plays the next stream in the queue
  174. function PlayNextStreamInQueue() {
  175.  
  176. ytAudioQueue.splice(0, 1);
  177.  
  178. // if there are streams remaining in the queue then try to play
  179. if (ytAudioQueue.length != 0) {
  180. console.log("Now Playing " + ytAudioQueue[0].videoName);
  181. PlayStream(ytAudioQueue[0].streamUrl);
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement