From c51a4fa97612e2b956706a6cac3279ff19ea68ee Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Tue, 9 Mar 2021 18:48:46 +0100 Subject: [PATCH] Pass objects on command.execute Pass objects instead of using a switch statement. Now I don't have to restart the bot whenever I add a new command that needs specific arguments other than 'message'. --- commands/.example | 2 +- commands/botinfo.js | 2 +- commands/emoji.js | 2 +- commands/help.js | 2 +- commands/pfp.js | 2 +- commands/reload.js | 2 +- commands/say.js | 2 +- commands/serverinfo.js | 2 +- commands/uptime.js | 2 +- commands/userinfo.js | 2 +- server.js | 20 +------------------- 11 files changed, 11 insertions(+), 29 deletions(-) diff --git a/commands/.example b/commands/.example index 5151bd1..7a30081 100644 --- a/commands/.example +++ b/commands/.example @@ -1,7 +1,7 @@ module.exports = { name: 'example name', description: 'example description', - execute(message) { + execute({message}) { //code } }; \ No newline at end of file diff --git a/commands/botinfo.js b/commands/botinfo.js index 1836443..63d0f7f 100644 --- a/commands/botinfo.js +++ b/commands/botinfo.js @@ -7,7 +7,7 @@ const creationJoinDates = require("../util/creationJoinDates") module.exports = { name: 'botinfo', description: 'Shows information about the bot', - execute(message, client) { + execute({message, client}) { let createJoin = creationJoinDates(client.user) const embed = new Discord.MessageEmbed() .setColor(15780145) diff --git a/commands/emoji.js b/commands/emoji.js index d555a36..c9d0df2 100644 --- a/commands/emoji.js +++ b/commands/emoji.js @@ -1,7 +1,7 @@ module.exports = { name: 'e', description: 'Returns emoji url', - execute(message, args) { + execute({message, args}) { let emoji = args.join(` `); if (!emoji) { diff --git a/commands/help.js b/commands/help.js index 4f2033e..7d1f97a 100644 --- a/commands/help.js +++ b/commands/help.js @@ -6,7 +6,7 @@ const {prefix} = require('../config.json'); module.exports = { name: 'help', description: 'List all available commands.', - execute(message, args) { + execute({message, args}) { var commands = " " const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); diff --git a/commands/pfp.js b/commands/pfp.js index 6a57d51..d694bad 100644 --- a/commands/pfp.js +++ b/commands/pfp.js @@ -3,7 +3,7 @@ const parseMention = require("../util/parseMention.js") module.exports = { name: 'pfp', description: 'Returns profile picture', - execute(message, args) { + execute({message, args}) { let info; if (!args[0]) { info = message.author.id; diff --git a/commands/reload.js b/commands/reload.js index afc3ba1..203c94e 100644 --- a/commands/reload.js +++ b/commands/reload.js @@ -4,7 +4,7 @@ module.exports = { name: 'reload', description: 'Reloads modules.', admin: true, - execute(message, client) { + execute({message, client}) { let reloadCommands = require("../util/reloadCommands.js") let beforeSize = client.commands.size; diff --git a/commands/say.js b/commands/say.js index b4d987a..d1c7390 100644 --- a/commands/say.js +++ b/commands/say.js @@ -1,7 +1,7 @@ module.exports = { name: 'say', description: 'Repeats arguments', - execute(message, args) { + execute({message, args}) { if(args.length == 0) message.channel.send("Can't send empty message"); diff --git a/commands/serverinfo.js b/commands/serverinfo.js index 6dca94a..d28a0f2 100644 --- a/commands/serverinfo.js +++ b/commands/serverinfo.js @@ -3,7 +3,7 @@ const Discord = require('discord.js') module.exports = { name: 'serverinfo', description: 'Displays information about the server', - execute(message) { + execute({message}) { console.log(message.guild.emojis.cache) const embed = new Discord.MessageEmbed() diff --git a/commands/uptime.js b/commands/uptime.js index ed338fd..1dde784 100644 --- a/commands/uptime.js +++ b/commands/uptime.js @@ -3,7 +3,7 @@ const parseMS = require('parse-ms') module.exports = { name: 'uptime', description: 'Returns uptime', - execute(message, client) { + execute({message, client}) { let days = ""; let hours = ""; diff --git a/commands/userinfo.js b/commands/userinfo.js index 3dd1492..0c43a82 100644 --- a/commands/userinfo.js +++ b/commands/userinfo.js @@ -5,7 +5,7 @@ const creationJoinDates = require("../util/creationJoinDates") module.exports = { name: 'userinfo', description: 'Displays information about the user', - execute(message, args) { + execute({message, args}) { let info; let nickname = ""; if (!args[0]) { diff --git a/server.js b/server.js index 5786639..9bb8420 100644 --- a/server.js +++ b/server.js @@ -34,25 +34,7 @@ client.on('message', async message => { if (!message.content.startsWith(prefix)) return; try { - - switch (commandName) { - case "ban": - case "botinfo": - case "reload": - case "uptime": - command.execute(message, client, args); - break; - case "say": - case "e": - case "help": - case "userinfo": - case "katti": - case "pfp": - command.execute(message, args); - break; - default: - command.execute(message) - } + command.execute({message:message, args:args, client: client}) } catch (error) { console.log(`${error}\n-------`) }