Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Discord = require('discord.js');
- const client = new Discord.Client();
- const ytdl = require('ytdl-core');
- const streamOptions = {
- seek: 0,
- volume: 1
- };
- const token = "NjQ5NjU0NjEzMDYxOTI2OTIy.Xd_96A.6jk95gWJiiA7SY2Z0dD9NeXQ73Q";
- client.login(token);
- client.on('ready', () => {
- console.log("Logged is as ", client.user.username + "#" + client.user.discriminator)
- });
- var musicUrls = [];
- client.on('message', async message =>{
- if(message.author.bot)
- return;
- if(message.content.toLowerCase().startsWith("!play"))
- {
- let args = message.content.split(" ");
- let url = args[1];
- let voiceChannel = message.guild.channels.find(channel => channel.id === '649641310986436608');
- if(ytdl.validateURL(url))
- {
- console.log("Valid URL!");
- var flag = musicUrls.some(element => element === url);
- if(flag)
- {
- musicUrls.push(url);
- if(voiceChannel != null)
- {
- console.log("Connection exists.");
- const embed = new Discord.RichEmbed();
- embed.setAuthor(client.user.username, client.user.displayAvatarURL);
- embed.setDescription("You've successfully added to queue!");
- message.channel.send(embed);
- }
- else {
- try {
- const voiceConnection = await voiceChannel.join();
- await playSong(message.channel, voiceConnection, voiceChannel);
- }
- catch(ex)
- {
- console.log(ex);
- }
- }
- }
- }
- }
- });
- async function playSong(messageChannel, voiceConnection, voiceChannel)
- {
- const stream = ytdl(musicUrls[0], { filter : 'audioonly'});
- const dispatcher = voiceConnection.playStream(stream, streamOptions);
- dispatcher.on('end', () =>{
- musicUrls.shift();
- if (musicUrls.length == 0)
- voiceChannel.leave();
- else
- {
- setTimeout(() =>{
- playSong(messageChannel, voiceConnection, voiceChannel);
- }, 5000);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement