Haha-Yes/commands/fun/music.js

42 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-09-07 22:48:43 +02:00
const { Command } = require('discord.js-commando');
const ytdl = require('ytdl-core');
module.exports = class MusicCommand extends Command {
constructor(client) {
super(client, {
name: 'music',
2018-09-17 22:00:50 +02:00
aliases: ['play'],
2018-09-07 22:48:43 +02:00
group: 'fun',
memberName: 'music',
2018-09-09 21:02:12 +02:00
description: 'Play youtube link in vocal',
guildOnly: true,
2018-09-07 22:48:43 +02:00
args: [
{
key: 'ytblink',
2018-09-17 22:02:27 +02:00
prompt: 'Wich Youtube link would you like to play? (default: despacito)\nTo stop the bot just do "haha music stop"',
2018-09-07 22:48:43 +02:00
type: 'string',
2018-09-17 22:00:50 +02:00
default: 'https://www.youtube.com/watch?v=kJQP7kiw5Fk'
2018-09-07 22:48:43 +02:00
}
]
});
}
2018-09-18 14:16:54 +02:00
async run(message, { ytblink, repeat }) {
2018-09-07 22:48:43 +02:00
const { voiceChannel } = message.member;
2018-09-08 23:09:33 +02:00
2018-09-11 15:57:32 +02:00
// If not in voice channel ask user to join
2018-09-08 22:30:50 +02:00
if (!voiceChannel) {
return message.reply('please join a voice channel first!');
2018-09-08 23:09:33 +02:00
} else
2018-09-11 15:57:32 +02:00
// If user say "stop" make the bot leave voice channel
2018-09-08 22:30:50 +02:00
if (ytblink == 'stop') {
voiceChannel.leave()
2018-09-18 14:16:54 +02:00
} else
2018-09-08 22:30:50 +02:00
voiceChannel.join().then(connection => {
const stream = ytdl(ytblink, { filter: 'audioonly' });
const dispatcher = connection.playStream(stream);
2018-09-11 15:57:32 +02:00
// End at then end of the audio stream
2018-09-08 22:30:50 +02:00
dispatcher.on('end', () => voiceChannel.leave());
});
2018-09-07 22:48:43 +02:00
}
2018-09-08 23:09:33 +02:00
}