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 token = 'YOUR TOKEN GORES HERE';
- const ytdl = require('ytdl-core');
- const streamOptions = {
- seek: 0,
- volume: 1
- }
- client.login(token);
- client.on('ready', () => {
- console.log('I am ready!');
- });
- 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)
- {
- if(voiceChannel.connection)
- {
- console.log("Connection exists.");
- const embed = new Discord.RichEmbed();
- embed.setAuthor(client.user.username, client.user.displayAvatarURL);
- embed.setDescription("You've successfully added to the 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);
- }, 1000);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement