You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.6 KiB

  1. const ytdl = require("ytdl-core");
  2. module.exports = {
  3. name: "play",
  4. description: "Play a song in your channel!",
  5. roles: ['732550362199752764', '732345527143759943'],
  6. async execute(message) {
  7. try {
  8. const args = message.content.split(" ");
  9. const queue = message.client.queue;
  10. const serverQueue = message.client.queue.get(message.guild.id);
  11. const voiceChannel = message.member.voice.channel;
  12. if (!voiceChannel)
  13. return message.channel.send(
  14. "You need to be in a voice channel to play music!"
  15. );
  16. const permissions = voiceChannel.permissionsFor(message.client.user);
  17. if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
  18. return message.channel.send(
  19. "I need the permissions to join and speak in your voice channel!"
  20. );
  21. }
  22. const songInfo = await ytdl.getInfo(args[1]);
  23. const song = {
  24. title: songInfo.videoDetails.title,
  25. url: songInfo.videoDetails.video_url
  26. };
  27. if (!serverQueue) {
  28. const queueContruct = {
  29. textChannel: message.channel,
  30. voiceChannel: voiceChannel,
  31. connection: null,
  32. songs: [],
  33. volume: 5,
  34. playing: true
  35. };
  36. queue.set(message.guild.id, queueContruct);
  37. queueContruct.songs.push(song);
  38. try {
  39. var connection = await voiceChannel.join();
  40. queueContruct.connection = connection;
  41. this.play(message, queueContruct.songs[0]);
  42. } catch (err) {
  43. console.log(err);
  44. queue.delete(message.guild.id);
  45. return message.channel.send(err);
  46. }
  47. } else {
  48. serverQueue.songs.push(song);
  49. return message.channel.send(
  50. `${song.title} has been added to the queue!`
  51. );
  52. }
  53. } catch (error) {
  54. console.log(error);
  55. message.channel.send(error.message);
  56. }
  57. },
  58. play(message, song) {
  59. const queue = message.client.queue;
  60. const guild = message.guild;
  61. const serverQueue = queue.get(message.guild.id);
  62. if (!song) {
  63. serverQueue.voiceChannel.leave();
  64. queue.delete(guild.id);
  65. return;
  66. }
  67. const dispatcher = serverQueue.connection
  68. .play(ytdl(song.url, { filter: 'audioonly', highWaterMark: 1024 * 1024 * 10 }))
  69. .on("finish", () => {
  70. serverQueue.songs.shift();
  71. this.play(message, serverQueue.songs[0]);
  72. })
  73. .on("error", error => console.error(error));
  74. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  75. serverQueue.textChannel.send(`Start playing: **${song.title}**`);
  76. }
  77. };