From 1c1dbcb1e914fc4670bc2d7ba7a41e0192b4a0b8 Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Fri, 24 Apr 2020 16:45:24 +0200 Subject: [PATCH] Clarify code --- commands/clear-queue.js | 19 +++++------- commands/now-playing.js | 21 ++++++------- commands/pause.js | 15 +++++----- commands/play.js | 23 +++++++-------- commands/queue.js | 20 ++++++------- commands/resume.js | 23 +++++++-------- commands/set-volume.js | 24 +++++++-------- commands/skip.js | 21 ++++++------- commands/stop.js | 21 ++++++------- index.js | 65 ++++++++++++++++++++--------------------- 10 files changed, 114 insertions(+), 138 deletions(-) diff --git a/commands/clear-queue.js b/commands/clear-queue.js index 5522bac..6eb9e4b 100644 --- a/commands/clear-queue.js +++ b/commands/clear-queue.js @@ -1,21 +1,18 @@ -const Discord = require("discord.js") -const fs = require("fs") -const emotes = require ("../config/emojis.json") +const emotes = require ("../config/emojis.json"); -module.exports.run = async (client, message, args) => { +module.exports.run = async (client, message) => { - if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`) + if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`); - if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`) + if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`); client.player.clearQueue(message.guild.id); message.channel.send(`**Queue cleared ${emotes.success}**`); - -} +}; module.exports.config = { - name: "clear-queue", - aliases: [] -} + name: "clear-queue", + aliases: [] +}; diff --git a/commands/now-playing.js b/commands/now-playing.js index 4abe01b..f7e3f81 100644 --- a/commands/now-playing.js +++ b/commands/now-playing.js @@ -1,21 +1,18 @@ -const Discord = require("discord.js") -const fs = require("fs") -const emotes = require ("../config/emojis.json") +const emotes = require ("../config/emojis.json"); -module.exports.run = async (client, message, args) => { +module.exports.run = async (client, message) => { - if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`) + if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`); - if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`) + if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`); - let song = await client.player.nowPlaying(message.guild.id); + const song = await client.player.nowPlaying(message.guild.id); message.channel.send(`**Currently playing ${song.name} ${emotes.music}**`); - -} +}; module.exports.config = { - name: "now-playing", - aliases: [] -} + name: "now-playing", + aliases: [] +}; diff --git a/commands/pause.js b/commands/pause.js index d375fdc..c47c072 100644 --- a/commands/pause.js +++ b/commands/pause.js @@ -2,18 +2,17 @@ const Discord = require("discord.js") const fs = require("fs") const emotes = require ("../config/emojis.json") -module.exports.run = async (client, message, args) => { +module.exports.run = async (client, message) => { - if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`) - - let song = await client.player.pause(message.guild.id); + if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`); + + if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`); + + const song = await client.player.pause(message.guild.id); - if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`) - message.channel.send(`**Song ${song.name} paused ${emotes.success}**`); - -} +}; module.exports.config = { name: "pause", diff --git a/commands/play.js b/commands/play.js index f9bc34d..59431d2 100644 --- a/commands/play.js +++ b/commands/play.js @@ -1,28 +1,27 @@ -const Discord = require("discord.js") -const fs = require("fs") -const emotes = require ("../config/emojis.json") +const emotes = require ("../config/emojis.json"); module.exports.run = async (client, message, args) => { - if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`) + if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`); - if (!args[0]) return message.channel.send(`**Please enter a music ${emotes.error}**`) + if (!args[0]) return message.channel.send(`**Please enter a music ${emotes.error}**`); - let aSongIsAlreadyPlaying = client.player.isPlaying(message.guild.id); + const aSongIsAlreadyPlaying = client.player.isPlaying(message.guild.id); + // If there's already a song playing if(aSongIsAlreadyPlaying){ // Add the song to the queue - let song = await client.player.addToQueue(message.guild.id, args.join(" ")); + const song = await client.player.addToQueue(message.guild.id, args.join(" ")); message.channel.send(`**Song ${song.name} added to queue ${emotes.music}**`); } else { // Else, play the song - let song = await client.player.play(message.member.voice.channel, args.join(" ")); + const song = await client.player.play(message.member.voice.channel, args.join(" ")); message.channel.send(`**Currently playing ${song.name} ${emotes.music}**`); } -} +}; module.exports.config = { - name: "play", - aliases: [] -} \ No newline at end of file + name: "play", + aliases: [] +}; \ No newline at end of file diff --git a/commands/queue.js b/commands/queue.js index b3c5af8..36d1309 100644 --- a/commands/queue.js +++ b/commands/queue.js @@ -1,12 +1,10 @@ -const Discord = require("discord.js") -const fs = require("fs") -const emotes = require ("../config/emojis.json") +const emotes = require ("../config/emojis.json"); -module.exports.run = async (client, message, args) => { +module.exports.run = async (client, message) => { - if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`) - - let queue = client.player.getQueue(message.guild.id); + if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`); + + const queue = client.player.getQueue(message.guild.id); if(!queue) return message.channel.send(`**No songs currently playing ${emotes.error}**`); @@ -14,10 +12,10 @@ module.exports.run = async (client, message, args) => { return `${i === 0 ? 'Current' : `#${i+1}`} - ${song.name} | ${song.author}` }).join('\n'))); -} +}; module.exports.config = { - name: "queue", - aliases: [] -} + name: "queue", + aliases: [] +}; diff --git a/commands/resume.js b/commands/resume.js index 15a71c8..0564d29 100644 --- a/commands/resume.js +++ b/commands/resume.js @@ -1,21 +1,18 @@ -const Discord = require("discord.js") -const fs = require("fs") -const emotes = require ("../config/emojis.json") +const emotes = require ("../config/emojis.json"); -module.exports.run = async (client, message, args) => { +module.exports.run = async (client, message) => { - if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`) - - let song = await client.player.resume(message.guild.id); + if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`); + + const song = await client.player.resume(message.guild.id); if(!song) return message.channel.send(`**No songs currently playing ${emotes.error}**`); - + message.channel.send(`**Song ${song.name} resumed ${emotes.success}**`); - -} +}; module.exports.config = { - name: "resume", - aliases: [] -} \ No newline at end of file + name: "resume", + aliases: [] +}; \ No newline at end of file diff --git a/commands/set-volume.js b/commands/set-volume.js index 60a7367..e257d55 100644 --- a/commands/set-volume.js +++ b/commands/set-volume.js @@ -1,23 +1,21 @@ -const Discord = require("discord.js") -const fs = require("fs") -const emotes = require ("../config/emojis.json") +const emotes = require ("../config/emojis.json"); module.exports.run = async (client, message, args) => { - if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`) + if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`); + + if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`); + if(!args[0]) return message.channel.send(`**Please enter a number ${emotes.error}**`); + if(isNaN(args[0])) return message.channel.send(`**Please enter a valid number ${emotes.error}**`); - if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`) - if (!args[0]) return message.channel.send(`**Please enter a number ${emotes.error}**`) - if (isNaN(args[0])) return message.channel.send(`**Please enter a valid number ${emotes.error}**`) - client.player.setVolume(message.guild.id, parseInt(args.join(" "))); - + message.channel.send("**Volume set to** `" + args.join(" ") + "`" + `** ** ${emotes.success}`); -} +}; module.exports.config = { - name: "set-volume", - aliases: [] -} + name: "set-volume", + aliases: [] +}; diff --git a/commands/skip.js b/commands/skip.js index 01f4aa6..b1dd767 100644 --- a/commands/skip.js +++ b/commands/skip.js @@ -1,21 +1,18 @@ -const Discord = require("discord.js") -const fs = require("fs") -const emotes = require ("../config/emojis.json") +const emotes = require ("../config/emojis.json"); -module.exports.run = async (client, message, args) => { +module.exports.run = async (client, message) => { - if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`) + if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`); - if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`) + if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`); - let song = await client.player.skip(message.guild.id); + const song = await client.player.skip(message.guild.id); message.channel.send(`**Song ${song.name} skipped ${emotes.success}**`); - -} +}; module.exports.config = { - name: "skip", - aliases: [] -} + name: "skip", + aliases: [] +}; diff --git a/commands/stop.js b/commands/stop.js index 29a66b3..8474e45 100644 --- a/commands/stop.js +++ b/commands/stop.js @@ -1,21 +1,18 @@ -const Discord = require("discord.js") -const fs = require("fs") -const emotes = require ("../config/emojis.json") +const emotes = require ("../config/emojis.json"); -module.exports.run = async (client, message, args) => { +module.exports.run = async (client, message) => { - if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`) + if(!message.member.voice.channel) return message.channel.send(`**You're not in a voice channel ${emotes.error}**`); - if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`) + if(!client.player.isPlaying(message.guild.id)) return message.channel.send(`**No music playing on this server ${emotes.error}**`); client.player.stop(message.guild.id); - + message.channel.send(`**Music stopped ${emotes.success}**`); - -} +}; module.exports.config = { - name: "stop", - aliases: [] -} + name: "stop", + aliases: [] +}; diff --git a/index.js b/index.js index 6a9aaf5..050fcf5 100644 --- a/index.js +++ b/index.js @@ -1,62 +1,59 @@ -const Discord = require("discord.js") -const fs = require("fs") -const client = new Discord.Client() +const Discord = require("discord.js"); +const fs = require("fs"); +const client = new Discord.Client(); -const settings = require ("./config/bot.json") //The bot connects using the configuration file +const settings = require ("./config/bot.json") // The bot connects using the configuration file -const emotes = require ("./config/emojis.json") //The bot finds emojis +const { Player } = require("discord-player"); // Create a new Player (Youtube API key is your Youtube Data v3 key) -const { Player } = require("discord-player"); //Create a new Player (Youtube API key is your Youtube Data v3 key) - -const player = new Player(client, settings.youtube_api); //To easily access the player +const player = new Player(client, settings.youtube_api); // To easily access the player client.player = player; client.on("ready", () => { - console.log("The bot is ready to play music"); //If the bot is ready it sends a message in the console + console.log("The bot is ready to play music"); // If the bot is ready it sends a message in the console }); client.login(settings.token_bot); //The bot connects thanks to the token -client.on('message', async message => { +client.on('message', async (message) => { - let prefix = settings.prefix + const prefix = settings.prefix; - let messageArray = message.content.split(" ") - let cmd = messageArray[0].toLowerCase(); - let args = messageArray.slice(1); - + const messageArray = message.content.split(" "); + const cmd = messageArray[0].toLowerCase(); + const args = messageArray.slice(1); if(!message.content.startsWith(prefix)) return; - let commandfile = client.commands.get(cmd.slice(prefix.length)) || client.commands.get(client.aliases.get(cmd.slice(prefix.length))) + const commandfile = client.commands.get(cmd.slice(prefix.length)) || client.commands.get(client.aliases.get(cmd.slice(prefix.length))) if(commandfile) commandfile.run(client,message,args) +}); - -}) - - client.commands = new Discord.Collection(); - client.aliases = new Discord.Collection(); +client.commands = new Discord.Collection(); +client.aliases = new Discord.Collection(); - fs.readdir("./commands/", (err, files) => { - - let jsfile = files.filter(f => f.split(".").pop() === "js") +fs.readdir("./commands/", (err, files) => { + + const jsfiles = files.filter(f => f.split(".").pop() === "js") - if(jsfile.length <= 0) { - return console.log("Couldn't Find Commands !"); + if(jsfiles.length <= 0) { + return console.log("Couldn't find any commands!"); } - jsfile.forEach((f, i) => { + jsfiles.forEach((file) => { - console.log(`Run command ${f}`); + console.log(`Loading command ${file}`); - let pull = require(`./commands/${f}`); + const command = require(`./commands/${file}`); + + client.commands.set(command.config.name, command); + command.config.aliases.forEach(alias => { + client.aliases.set(alias, command.config.name); + }); - client.commands.set(pull.config.name, pull); - pull.config.aliases.forEach(alias => { - client.aliases.set(alias, pull.config.name) - }); -})}); + +});