Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //if missing dependencies or any other error run the following
- //make sure node.js is installed
- //npm install discord.js ffmpeg fluent-ffmpeg @discordjs/opus ytdl-core ytsr axios --save
- const Discord = require("discord.js")
- const ytsr = require('ytsr');
- const ytdl = require("ytdl-core");
- const token = "BOT-Token"
- const client = new Discord.Client();
- const queue = new Map();
- //const axios = require("axios")
- let prefix = "!"
- let dispatcher = null;
- let storedVolume = 0.25; // sets default volume to 25%
- //set status after init complete
- client.on("ready", () => {
- console.log("Ready");
- client.user.setStatus("ONLINE")
- client.user.setPresence({ activity: { name: '!help' }, status: 'available' })
- });
- //notify upon conn lost
- client.once("reconnecting", () => {
- console.log("Reconnecting!");
- });
- //notify upon disconnect
- client.once("disconnect", () => {
- console.log("Disconnect!");
- });
- //respond whenever a message is received with prefix
- client.on("message", async message => {
- if (message.author.bot) return;
- if (!message.content.startsWith(prefix)) return;
- const serverQueue = queue.get(message.guild.id);
- //execute specific funtion based on input command
- if (message.content.startsWith(`${prefix}play`)) execute(message, serverQueue);
- else if (message.content.startsWith(`${prefix}skip`)) skip(message, serverQueue);
- else if (message.content.startsWith(`${prefix}stop`)) stop(message, serverQueue);
- else if (message.content.startsWith(`${prefix}volume`)) volume(message, serverQueue);
- else if (message.content.startsWith(`${prefix}help`)) help(message, serverQueue);
- else if (message.content.startsWith(`${prefix}join`)) join(message, serverQueue);
- else if (message.content.startsWith(`${prefix}leave`)) leave(message, serverQueue);
- else if (message.content.startsWith(`${prefix}member`)) member(message, serverQueue);
- else if (message.content.startsWith(`${prefix}resume`)) resume(message, serverQueue);
- else
- message.channel.send("You need to enter a valid command!");
- });
- async function execute(message, serverQueue) {
- const msg = message.content.substr(message.content.indexOf(" "));
- //test what condiiotn to test for
- /*console.log(s);
- console.log("spotify link = " + (s.search("spotify") > -1));
- console.log("Getlink = " + (s.search("http") === -1))
- console.log("is Link = " + ((s.search("spotify") === -1) && !(s.search("http") === -1)));*/
- if(msg.search("spotify") > -1)
- return message.channel.send("Spotify not supported yet");
- if(msg.search("http") === -1){
- //search youtube for url
- var options = {
- limit: 1,
- }
- const res = await ytsr(msg,options);
- result = await res.items[0].link;
- }
- if((msg.search("spotify") === -1) && !(msg.search("http") === -1)) {
- result = msg;
- }
- const voiceChannel = message.member.voice.channel;
- 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 need the permissions to join and speak in your voice channel!"
- );
- }
- const songInfo = await ytdl.getInfo(result);
- const song = {
- title: songInfo.title,
- url: songInfo.video_url
- };
- if (!serverQueue) {
- const queueContruct = {
- textChannel: message.channel,
- voiceChannel: voiceChannel,
- connection: null,
- songs: [],
- volume: 0.25,
- playing: true
- };
- queue.set(message.guild.id, queueContruct);
- queueContruct.songs.push(song);
- try {
- var connection = await voiceChannel.join();
- queueContruct.connection = connection;
- play(message.guild, queueContruct.songs[0]);
- } catch (err) {
- console.log(err);
- queue.delete(message.guild.id);
- return message.channel.send(err);
- }
- } else {
- serverQueue.songs.push(song);
- return message.channel.send(`${song.title} has been added to the queue!`);
- }
- }
- function dhm (t,user,members) {
- // Thank you to https://stackoverflow.com/users/166491/mic
- // for creating this code
- let cd = 24 * 60 * 60 * 1000;
- let ch = 60 * 60 * 1000;
- let d = Math.floor(t / cd);
- let h = Math.floor( (t - d * cd) / ch);
- let m = Math.round( (t - d * cd - h * ch) / 60000);
- if( m === 60 ){
- h++;
- m = 0;
- }
- if( h === 24 ){
- d++;
- h = 0;
- }
- return (user + " has been a member of this server for: " + d + " Days | " + h + " Hours | and " + m + " Minutes with a total of " + members + " members");
- }
- function member(message) {
- let age = Date.now() - Date.parse(message.member.joinedAt);
- let user = message.author.username;
- let members = message.member.guild.memberCount;
- return message.channel.send(dhm(age,user,members));
- }
- function skip(message, serverQueue) {
- if (!message.member.voice.channel)
- 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();
- }
- function stop(message, serverQueue) {
- if (!message.member.voice.channel)
- return message.channel.send(
- "You have to be in a voice channel to stop the music!"
- );
- message.channel.send("Audio playback paused!");
- serverQueue.connection.dispatcher.pause();
- }
- function resume(message, serverQueue) {
- if (!message.member.voice.channel)
- return message.channel.send(
- "You have to be in a voice channel to stop the music!"
- );
- message.channel.send("Audio playback resumed!");
- serverQueue.connection.dispatcher.resume();
- }
- function join (message, serverQueue) {
- if(!message.member.voice.channel) return message.channel.send("You have to be in a voice channel for me to join");
- message.member.voice.channel.join();
- return message.channel.send("I have joined your voice channel");
- }
- function leave (message, serverQueue) {
- if(!message.member.voice.channel) return message.channel.send("You have to be in a voice channel for me to leave");
- if(!serverQueue){
- message.member.voice.channel.leave();
- return message.channel.send("I have left your voice channel");
- } else {
- serverQueue.connection.dispatcher.end();
- return message.channel.send("I have left your voice channel");
- }
- }
- function play(guild, song) {
- if(storedVolume > 1){
- storedVolume = 0.25;
- }
- const serverQueue = queue.get(guild.id);
- if (!song) {
- serverQueue.voiceChannel.leave();
- queue.delete(guild.id);
- return;
- }
- dispatcher = serverQueue.connection
- .play(ytdl(song.url), {volumeEditable: true})
- .on("finish", () => {
- serverQueue.songs.shift();
- play(guild, serverQueue.songs[0]);
- })
- .on("error", error => console.error(error));
- dispatcher.setVolume(storedVolume);
- serverQueue.textChannel.send(`Started playing: **${song.title}**`);
- }
- function volume(message, serverQueue) {
- const args = message.content.split(" ");
- let v = (args[1]/100);
- if(!dispatcher){
- storedVolume = v;
- message.channel.send(`Changed volume to: **${storedVolume*100}%**`);
- }else {
- if(v>1){
- v = 1;
- dispatcher.setVolume(v);
- serverQueue.textChannel.send(`Changed volume to: **${v*100}%**`);
- } else if (v < 0) {
- v = 0;
- dispatcher.setVolume(v);
- serverQueue.textChannel.send(`Changed volume to: **${v*100}%**`);
- } else {
- dispatcher.setVolume(v);
- serverQueue.textChannel.send(`Changed volume to: **${v*100}%**`);
- }
- }
- }
- function help(message, serverQueue) {
- let help = `Available Commands:
- 1. !join - Let me join your current channel.
- 2. !leave - I will leave your channel.
- 3. !play - plays either a youtube link or searchers for a song manually.
- 4. !skip - skips current song.
- 5. !stop - stop all music and kicks bot.
- 6. !volume - !volume <number> - change volume of bot.
- 7. !member - See how long you have been a member of this server.
- 8. !help - display this help screen.`
- message.author.send(help);
- return;
- }
- client.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement