Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { Client, Util } = require(`discord.js`);
- const { TOKEN, PREFIX, GOOGLE_API_KEY} = require(`./config`);
- const ytdl = require(`ytdl-core`);
- const YouTube = require(`simple-youtube-api`)
- var loop = false;
- const client = new Client({ disableEveryone: true });
- const youtube = new YouTube(GOOGLE_API_KEY);
- const queue = new Map();
- client.on(`warn`, console.warn);
- client.on(`error`, console.error);
- client.on(`ready`, () => {
- console.log("Active!");
- client.user.setActivity(`.help`);
- })
- client.on(`disconnect`, () => console.log("I just disconnected, reconnecting now!"))
- client.on(`reconnecting`, () => console.log("I am reconnecting now!"));
- client.on(`message`, async msg => { // eslist disable line
- if (msg.author.bot) return undefined;
- if (!msg.content.startsWith(PREFIX)) return undefined;
- const args = msg.content.split(" ");
- const searchString = args.slice(1).join(" ");
- const url = args[1] ? args[1].replace(/<(.+)>/g, `$1`) : ``;
- const serverQueue = queue.get(msg.guild.id);
- if (msg.content.startsWith(`${PREFIX}play`)) {
- const voiceChannel = msg.member.voiceChannel;
- if (!voiceChannel) return msg.channel.send(`**You need to be in a voice channel to play music!**`);
- const permissions = voiceChannel.permissionsFor(msg.client.user);
- if(!permissions.has(`CONNECT`)) {
- return msg.channel.send(`**I cant connect to the voice channel, make sure I have permissions!**`);
- }
- if (!permissions.has(`SPEAK`)) {
- return msg.channel.send(`**I cant speak in the voice channel, make sure I have permissions!**`);
- }
- if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
- const playlist = await youtube.getPlaylist(url);
- const videos = await playlist.getVideos();
- for (const video of Object.values(videos)) {
- const video2 = await youtube.getVideoByID(video.id); // eslint disable line no await in loop
- await handleVideo(video2, msg, voiceChannel, true); // eslist disable line no await in loop
- }
- return msg.channel.send(`Playlist: **${playlist.title}** has been added to the queue!`);
- } else {
- try {
- var video = await youtube.getVideo(url);
- } catch (error) {
- try {
- var videos = await youtube.searchVideos(searchString, 1);
- var video = await youtube.getVideoByID(videos[0].id);
- } catch (err) {
- console.error(err);
- return msg.channel.send("**I could not find any search results!**");
- }
- }
- return handleVideo(video, msg, voiceChannel);
- }
- } else if (msg.content.startsWith(`${PREFIX}skip`)) {
- if (!msg.member.voiceChannel) return msg.channel.send("**Your not in a voice channel!**");
- if(!serverQueue) return msg.channel.send("**There is nothing playing that I can skip!**");
- msg.channel.send("**Skipping song for you now!**");
- serverQueue.connection.dispatcher.end("Skip command has been used!");
- return undefined;
- } else if (msg.content.startsWith(`${PREFIX}stop`)) {
- if (!msg.member.voiceChannel) return msg.channel.send("**Your not in a voice channel!**");
- if(!serverQueue) return msg.channel.send("**There is nothing playing right now, I can not stop!**");
- serverQueue.songs = [];
- serverQueue.connection.dispatcher.end("Stop command has been used!");
- return msg.channel.send("**Stopping music for you now!**");
- } else if (msg.content.startsWith(`${PREFIX}volume`)) {
- if(!serverQueue) return msg.channel.send("**There is nothing playing!**");
- if (!msg.member.voiceChannel) return msg.channel.send("**Your not in a voice channel!**");
- if(!args[1]) return msg.channel.send(`**The current volume is: ${serverQueue.volume}**`);
- serverQueue.volume = args[1];
- serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 5);
- return msg.channel.send(`**I set the volume to: ${args[1]}**`);
- } else if (msg.content.startsWith(`${PREFIX}np`)) {
- if(!serverQueue) return msg.channel.send("**There is nothing playing!**");
- return msg.channel.send(`**Now playing: ${serverQueue.songs[0].title}**`);
- } else if (msg.content.startsWith(`${PREFIX}queue`)) {
- if(!serverQueue) return msg.channel.send("**There is nothing playing!**");
- return msg.channel.send(`
- __**Song Queue:**__
- ${serverQueue.songs.map(song => `**-** ${song.title}`).join(`\n`)}
- **Now playing:** ${serverQueue.songs[0].title}
- `);
- } else if (msg.content.startsWith(`${PREFIX}pause`)) {
- if (serverQueue && serverQueue.playing) {
- serverQueue.playing = false;
- serverQueue.connection.dispatcher.pause();
- return msg.channel.send("**Paused the music for you!**");
- }
- return msg.channel.send("**There is nothing playing!**");
- } else if (msg.content.startsWith(`${PREFIX}resume`)) {
- if(serverQueue && !serverQueue.playing) {
- serverQueue.playing = true;
- serverQueue.connection.dispatcher.resume();
- return msg.channel.send("**Resumed the music for you!**");
- }
- return msg.channel.send("**There is nothing playing!**");
- } else if(msg.content.startsWith(`${PREFIX}help`)) {
- msg.channel.send(`These are the commands for the music bot!
- **.skip** will skip the current song and play the next one in the queue
- **.stop** will make the bot stop playing music and leave the voice channel
- **.np** will show the title of the song currently playing
- **.volume** will show the current volume the music it at
- **.volume *(insert number)*** will change that to the volume level
- **.pause** will pause the music bot where the song is
- **.resume** will play the song where it was last paused
- **.queue** will show you the queue list of songs that are going to be played
- **.play *(yotube url)*** this will play that song that pasted
- **.play *(song name)*** will play the song that you put the name of
- **.loop** will play the song on repeat until .loop is sent again
- `);
- }else if (msg.content.startsWith(`${PREFIX}loop`)) {
- loop = !loop;
- if (loop === false) {
- msg.channel.send("**The music has now disabled loop!**");
- } if (loop === true) {
- msg.channel.send("**The music is now enabled to loop!**");
- }}
- return undefined;
- });
- async function handleVideo(video, msg, voiceChannel, playlist = false) {
- const serverQueue = queue.get(msg.guild.id);
- console.log(video);
- const song = {
- id: video.id,
- title: Util.escapeMarkdown(video.title),
- url: `https://www.youtube.com/watch?v=${video.id}`
- };
- if(!serverQueue) {
- const queueConstruct = {
- textChannel: msg.channel,
- voiceChannel: voiceChannel,
- connection: null,
- songs: [],
- volume: 5,
- playing: true,
- };
- queue.set(msg.guild.id, queueConstruct);
- queueConstruct.songs.push(song);
- try {
- var connection = await voiceChannel.join();
- queueConstruct.connection = connection;
- play(msg.guild, queueConstruct.songs[0]);
- } catch (error) {
- console.error(`I could not join the voice channel: ${error}`);
- queue.delete(msg.guild.id);
- return msg.channel.send(`I could not join the voice channel: ${error}`);
- }
- } else {
- serverQueue.songs.push(song);
- console.log(serverQueue.songs);
- if (playlist) return undefined;
- else return msg.channel.send(`**${song.title}** has been added to the queue!`);
- }
- return undefined;
- }
- function play(guild, song) {
- const serverQueue = queue.get(guild.id);
- if(!song) {
- serverQueue.voiceChannel.leave();
- queue.delete(guild.id);
- }
- client.on(`message`, msg => {
- if (msg.content.startsWith(`${PREFIX}clearqueue`)) {
- serverQueue.voiceChannel.leave();
- queue.delete(guild.id);
- return;
- }
- })
- console.log(serverQueue.songs);
- const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
- .on(`end`, reason => {
- if(reason === "Stream is not generating quickly enough!") console.log("Song ended!");
- if (loop === false) {
- serverQueue.songs.shift();
- }
- play(guild, serverQueue.songs[0]);
- })
- .on(`error`, error => console.error(error));
- dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
- if (loop === false) {
- serverQueue.textChannel.send(`**Now playing: ${song.title}**`);
- }
- }
- client.login(TOKEN)
- ERROR CODE
- node .
- I just disconnected, reconnecting now!
- (node:15392) UnhandledPromiseRejectionWarning: Error: Incorrect login details were provided.
- at WebSocketConnection.<anonymous> (C:\Users\maxhu\Desktop\Discord Bot\node_modules\discord.js\src\client\ClientManager.js:48:41)
- at Object.onceWrapper (events.js:313:26)
- at WebSocketConnection.emit (events.js:223:5)
- at WebSocketConnection.onClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:390:10)
- at WebSocket.onClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\event-target.js:124:16)
- at WebSocket.emit (events.js:223:5)
- at WebSocket.emitClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\websocket.js:191:10)
- at TLSSocket.socketOnClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\websocket.js:850:15)
- at TLSSocket.emit (events.js:228:7)
- at net.js:664:12
- (node:15392) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was
- not handled with .catch(). (rejection id: 2)
- (node:15392) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.PS C:\Users\maxhu\Desktop\Discord Bot> ^C
- PS C:\Users\maxhu\Desktop\Discord Bot> node .
- I just disconnected, reconnecting now!
- (node:17272) UnhandledPromiseRejectionWarning: Error: Incorrect login details were provided.
- at WebSocketConnection.<anonymous> (C:\Users\maxhu\Desktop\Discord Bot\node_modules\discord.js\src\client\ClientManager.js:48:41)
- at Object.onceWrapper (events.js:313:26)
- at WebSocketConnection.emit (events.js:223:5)
- PS C:\Users\maxhu\Desktop\Discord Bot> node .
- I just disconnected, reconnecting now!
- (node:3508) UnhandledPromiseRejectionWarning: Error: Incorrect login details were provided.
- at WebSocketConnection.<anonymous> (C:\Users\maxhu\Desktop\Discord Bot\node_modules\discord.js\src\client\ClientManager.js:48:41)
- at Object.onceWrapper (events.js:313:26)
- at WebSocketConnection.emit (events.js:223:5)
- at WebSocketConnection.onClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:390:10)
- at WebSocket.onClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\event-target.js:124:16)
- at WebSocket.emit (events.js:223:5)
- at WebSocket.emitClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\websocket.js:191:10)
- at TLSSocket.socketOnClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\websocket.js:850:15)
- at TLSSocket.emit (events.js:228:7)
- at net.js:664:12
- (node:3508) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
- (node:3508) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
- PS C:\Users\maxhu\Desktop\Discord Bot>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement