Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. //index.js
  2. let client = {
  3. config: require("./config.json"),
  4. getYouTubeID: require('get-youtube-id'),
  5. fetchYoutubeInfo: require('youtube-info'),
  6. ytdl: require('ytdl-core'),
  7. queue: [],
  8. isPlaying: false,
  9. dispatcher: null,
  10. voiceChannel: null,
  11. skipReq: 0,
  12. skippers: {},
  13. playMusic: async function (id, message) {
  14. message.member.voiceChannel.join().then(function (connection) {
  15. stream = client.ytdl("https://www.youtube.com/watch?v=" + id, {
  16. filter: 'audioonly'
  17. });
  18. client.skipReq = 0;
  19. client.skippers = [];
  20.  
  21. dispatcher = connection.playStream(stream);
  22. });
  23. },
  24. getID: async function (str, cb) {
  25. if (client.isYoutube(str)) {
  26. cb(client.getYouTubeID(str));
  27. } else {
  28. client.search_video(str, function (id) {
  29. cb(id);
  30. });
  31. }
  32. },
  33. add_to_queue: async function (strID) {
  34. if (client.isYoutube(strID)) {
  35. client.queue.push(client.getID(client.getYouTubeID(strID)));
  36. } else {
  37. client.queue.push(strID);
  38. }
  39. },
  40. search_video: async function (query, callback) {
  41. request("https://www.googleapis.com/youtube/v3/search?part=id&type=video&q=" + encodeURIComponent(query) + "&key=" + client.config.yt_api_key, function(error, response, body) {
  42. var json = JSON.parse(body);
  43. callback(jsonf.items[0].id.videoId);
  44. });
  45. },
  46. isYoutube: async function (str) {
  47. return str.toLowerCase().indexOf('youtube.com') > -1;
  48. }
  49. }
  50.  
  51. //play commnd
  52.  
  53. if (client.queue.length > 0 || client.isPlaying) {
  54. client.getID(args, function (id) {
  55. client.add_to_queue(id);
  56. client.fetchYoutubeInfo(id, function (err, videoInfo) {
  57. if (err) throw new Error(err);
  58. message.channel.send(`**${videoInfo.title}** has been added to queue!`);
  59. });
  60. });
  61. } else {
  62. client.isPlaying = true;
  63. client.getID(args, function (id) {
  64. client.queue.push('placeholder');
  65. client.playMusic(id, message);
  66. client.fetchYoutubeInfo(id, function (err, videoInfo) {
  67. if (err) throw new Error(err);
  68. message.channel.send(`Now playing **${videoInfo.title}**`);
  69. });
  70. });
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement