From dbc45d4a426ba791b65543997dd4171753d35db3 Mon Sep 17 00:00:00 2001 From: entar Date: Wed, 21 May 2025 12:21:04 +0700 Subject: [PATCH] Better argument type handing --- commands/base.js | 11 ++++++++++- commands/extra.js | 2 +- index.js | 21 +++++++++++++-------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/commands/base.js b/commands/base.js index 752af12..d39786e 100644 --- a/commands/base.js +++ b/commands/base.js @@ -11,7 +11,7 @@ module.exports = [ arguments: ["any"], description: "Check how fast bot will reply!" }, - + { name: "help", run: (ctx, args) => { @@ -24,6 +24,15 @@ module.exports = [ if (command.hide) { return // Go to next command if this one shouldnt show up } + + const arguments = command.arguments.slice() + + arguments.forEach((argument, index) => { + if (typeof argument == "object") { + arguments[index] = argument.prototype.name + } + }) + const field = {name: command.name, value: `Description: ${command.description || "None"}\nArguments: ${command.arguments || "None"}`} embed.addFields(field) }) diff --git a/commands/extra.js b/commands/extra.js index 24a6b33..a1dd51c 100644 --- a/commands/extra.js +++ b/commands/extra.js @@ -5,7 +5,7 @@ module.exports = [ { name: "helptest", description: "Kill every1", - arguments: [GuildMember.name, GuildChannel.name], + arguments: [GuildMember, GuildChannel], run: (ctx, member, channel) => { if (!member || !(member instanceof GuildMember) ) { return ctx.reply("Member invalid.") diff --git a/index.js b/index.js index d52b766..cf87b02 100644 --- a/index.js +++ b/index.js @@ -79,6 +79,15 @@ client.on("messageCreate", (message) => { // Getting command name const command = parsed[0] + // Actual command + const realCommand = getCommand(command) + + + // Only continue if command exists + if (!realCommand) { + return + } + // Removing command from the args const args = parsed.filter(value => {return value != command}) @@ -103,15 +112,11 @@ client.on("messageCreate", (message) => { args[index] = channel } } - }) - - // Actual command - const realCommand = getCommand(command) - // Only continue if command exists - if (!realCommand) { - return - } + if (typeof args[index] != typeof realCommand.arguments[index] || !(args[index] instanceof realCommand.arguments[index])) { + return message.reply(`Argument ${index} of wrong type`) + } + }) // Run command realCommand.run(message, ...args)