Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { Command } = require('klasa');
- const yt = require('ytdl-core');
- const search = require('youtube-search');
- module.exports = class extends Command {
- constructor(...args) {
- super(...args, {
- name: 'play',
- enabled: true,
- runIn: ['text'],
- cooldown: 0,
- aliases: [],
- permLevel: 0,
- botPerms: ['CONNECT', 'SPEAK', 'SEND_MESSAGES', 'EMBED_LINKS'],
- requiredSettings: [],
- description: 'Plays the audio of a YouTube video.',
- quotedStringSupport: false,
- usage: '<search:string>',
- usageDelim: undefined
- });
- }
- async run(msg, [searchString]) {
- if(!msg.member.voiceChannel)return msg.channel.send('You must be in a voice channel to use music commands.').catch(console.error);
- const options = {
- maxResults: 1,
- type: 'video',
- key: 'AIzaSyBp0nhmScKPAIy7BfS4pp4SbmdvB5lZn04'
- };
- search(searchString, options, (err, results) => {
- if(err) return console.log(err);
- if(results.length === 0)return message.channel.send('No results found.').catch(console.error);
- if(!this.client.music[msg.guild.id])this.client.music[msg.guild.id] = {queue: [], loop: false};
- let video = results[0];
- video.requester = msg.author.tag;
- if(!this.client.music[msg.guild.id].nowPlaying){
- playVideo(this.client, msg, video);
- } else {
- this.client.music[msg.guild.id].queue.push(video);
- }
- const embed = new this.client.methods.Embed();
- embed.setTitle(video.title);
- embed.setURL(video.link);
- embed.setDescription(video.description);
- embed.addField('Channel:', video.channelTitle);
- embed.setThumbnail(video.thumbnails.default.url);
- embed.setFooter('Requested by ' + video.requester);
- embed.setColor('RANDOM');
- msg.channel.send(embed).catch(console.error);
- });
- }
- playVideo = async function(client, msg, video) {
- const connection = await msg.member.voiceChannel.join().catch(console.error);
- client.music[msg.guild.id].dispatcher = connection.playStream(yt(video.link, { audioonly: true }), { passes: 1 });
- client.music[msg.guild.id].nowPlaying = video;
- client.music[msg.guild.id].dispatcher.on('end', () => {
- if(client.music[msg.guild.id].loop)client.music[msg.guild.id].queue.push(client.music[msg.guild.id].nowPlaying);
- if(client.music[msg.guild.id].queue.length > 0)return playVideo(client, msg, client.music[msg.guild.id].queue.shift());
- connection.disconnect();
- delete client.music[msg.guild.id];
- });
- }
- async init() {
- if(!this.client.music)this.client.music = {};
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement