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 config = require("./config.json");
- const ytdl = require("ytdl-core");
- const queue = new Map();
- var datas;
- const fs = require("fs");
- const mutes = new Map();
- /*if (!fs.existsSync('./serverConfigs.json')) {
- const serverConfigConstruct = {};
- fs.writeFileSync('./serverConfigs.json', JSON.stringify(serverConfigConstruct) , (err) => {
- console.log(err);
- });
- */
- //}
- //fs.readFileSync('./serverConfigs.json', (err, data) => {
- // if (err) throw err;
- // datas = data;
- //});
- //const guildSettings = JSON.parse(datas);
- client.on('ready', async () => {
- console.log("Ready");
- });
- client.on("guildCreate", guild => {
- console.log("Joined a new guild: " + guild.name);
- //const guildConstruct = {
- // volume: 5,
- // prefix: "!?!"
- // };
- // guildSettings.set(guild.id, guildConstruct);
- let chan = 0;
- var parentch;
- guild.channels.forEach((channel) => {
- if (channel.type == "text" && channel.name == "bots") {
- chan = 1;
- } else {
- parentch = channel.parent;
- }
- });
- if (chan == 0 && parentch != undefined) {
- guild.createChannel("bots", "text");
- }
- });
- client.on("guildDelete", guild => {
- console.log("Left a guild: " + guild.name);
- // guildSettings.delete(guild.id);
- })
- client.on('message', async msg => {
- if (msg.author.bot) {
- return undefined;
- }
- if (msg.channel.name == "bots") {
- if (!msg.content.startsWith(config.prefix)) return undefined;
- msg.delete();
- let args = msg.content.split(' ');
- const command = args[0].slice(config.prefix.length);
- args = args.slice(1);
- var serverQueue = queue.get(msg.guild.id);
- if (command == "play") {
- const voiceChannel = msg.member.voiceChannel;
- if (!voiceChannel) return msg.channel.send('I\'m sorry, but 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 don't have permission to join your voice channel.");
- }
- if (!permissions.has('SPEAK')){
- return msg.channel.send("I don't have permission to speak in your voice channel.")
- }
- const songInfo = await ytdl.getInfo(args[0]);
- const song = {
- title: songInfo.title,
- url: songInfo.video_url,
- id: msg.author
- };
- if (!serverQueue) {
- const queueConstruct = {
- textChannel: msg.channel,
- voiceChannel: voiceChannel,
- connection: null,
- songs: [],
- volume: 2,
- playing: true,
- loop: false
- };
- 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]);
- return msg.channel.send(`✅ **${song.title}** has been added to the queue!`);
- } catch(error) {
- voiceChannel.leave();
- queue.delete(msg.guild);
- console.error(error);
- return msg.channel.send(error);
- }
- } else {
- serverQueue.songs.push(song);
- console.log(serverQueue.songs);
- return msg.channel.send(`✅ **${song.title}** has been added to the queue!`);
- }
- } else if (command == "mute") {
- }else if (command == "stop") {
- if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
- if (!serverQueue) return msg.channel.send('There is nothing playing that I could stop for you.');
- if (serverQueue.songs[0].id.username != msg.author.username) return msg.channel.send('no');
- serverQueue.songs = [];
- serverQueue.connection.dispatcher.end('Stop command has been used!');
- return undefined;
- } else if (command == "volume") {
- if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
- if (!serverQueue) return msg.channel.send('There is nothing playing.');
- if (!args[0]) return msg.channel.send(`The current volume is: **${serverQueue.volume}**`);
- serverQueue.volume = args[0];
- serverQueue.connection.dispatcher.setVolumeLogarithmic(args[0] / 5);
- return msg.channel.send(`I set the volume to: **${args[0]}**`);
- } else if (command == "loop") {
- if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
- if (!serverQueue) return msg.channel.send('There is nothing playing.');
- if (!serverQueue.loop == false) return msg.channel.send('The queue is already looping!');
- serverQueue.loop = true;
- return msg.channel.send('Queue is now repeating');
- } else if (command == "unloop") {
- if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
- if (!serverQueue)return msg.channel.send('There is nothing playing.');
- if (!serverQueue.loop == true) return msg.channel.send('The Queue isnI\'t looping!');
- serverQueue.loop = false;
- return msg.channel.send('The Queus is no longer repeating');
- } else if (command === "skip") {
- if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
- if (!serverQueue) return msg.channel.send('There is nothing playing that I could skip for you.');
- serverQueue.connection.dispatcher.end('Skip command has been used!');
- return undefined;
- } else if (command == "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 (command == "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 (command === '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 (command == 'test') {
- console.log(msg.member.id);
- } else {
- return console.log("not a command");
- }
- }
- });
- 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('song ended');
- if (serverQueue.loop == true){
- serverQueue.songs.push(serverQueue.songs[0]);
- }
- serverQueue.songs.shift();
- play(guild, serverQueue.songs[0])
- })
- .on('error', error => {
- console.error(error);
- });
- dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
- }
- client.login(config.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement