Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ytdl = require('ytdl-core');
- module.exports = {
- name: 'music',
- description: 'Command fpr music related stuffs.',
- gulidOnly: true,
- execute(client, message, args) {
- const queue = new Map();
- const serverQueue = queue.get(message.guild.id);
- const type = args[0];
- if(type == 'play'){
- const voiceChannel = message.member.voiceChannel;
- if (!voiceChannel) return message.channel.send('You need to be in a voice channel to play music!');
- const permissions = voiceChannel.permissionsFor(message.client.user);
- if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) {
- return message.channel.send('I am not allwd to do dat hooman :( (Insuffect Permissions)');
- }
- const songInfo = ytdl.getInfo(args[1]);
- const song = {
- title: songInfo.title,
- url: songInfo.video_url,
- };
- if (!serverQueue) {
- // Creating the contract for our queue
- const queueContruct = {
- textChannel: message.channel,
- voiceChannel: voiceChannel,
- connection: null,
- songs: [],
- volume: 5,
- playing: true,
- };
- // Setting the queue using our contract
- queue.set(message.guild.id, queueContruct);
- // Pushing the song to our songs array
- queueContruct.songs.push(song);
- try {
- // Here we try to join the voicechat and save our connection into our object.
- var connection = voiceChannel.join();
- queueContruct.connection = connection;
- // Calling the play function to start a song
- play(message.guild, queueContruct.songs[0]);
- } catch (err) {
- // Printing the error message if the bot fails to join the voicechat
- console.log(err);
- queue.delete(message.guild.id);
- return message.channel.send(err);
- }
- } else {
- serverQueue.songs.push(song);
- console.log(serverQueue.songs);
- return message.channel.send(`${song.title} has been added to the queue!`);
- }
- } else if (type == 'skip') {
- if (!message.member.voiceChannel) return message.channel.send('You have to be in a voice channel to stop the music!');
- if (!serverQueue) return message.channel.send('There is no song that I could skip!');
- serverQueue.connection.dispatcher.end();
- } else if (type == 'stop') {
- if (!message.member.voiceChannel) return message.channel.send('You have to be in a voice channel to stop the music!');
- serverQueue.songs = [];
- serverQueue.connection.dispatcher.end();
- }
- function play(guild, song) {
- const serverQueue = queue.get(guild.id);
- if (!song) {
- serverQueue.voiceChannel.leave();
- queue.delete(guild.id);
- return;
- }
- const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
- .on('end', () => {
- console.log('Music ended!');
- serverQueue.songs.shift();
- play(guild, serverQueue.songs[0]);
- })
- .on('error', error => {
- console.error(error);
- });
- dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement