Even better argument type handling

This commit is contained in:
entar 2025-05-21 14:21:43 +07:00
parent e80dd4a6e9
commit 28d38422fd
3 changed files with 56 additions and 31 deletions

View File

@ -29,15 +29,12 @@ module.exports = [
if (arguments) {
arguments.forEach((argument, index) => {
console.log(typeof argument)
if (typeof argument == "object" || typeof argument == "function") {
arguments[index] = argument.name
}
})
}
console.log(arguments)
const field = {name: command.name, value: `Description: ${command.description || "None"}\nArguments: ${arguments || "None"}`}
embed.addFields(field)
})

View File

@ -1,6 +1,7 @@
// Base requires
const { Client, GatewayIntentBits, Events } = require("discord.js")
const { Client, GatewayIntentBits, Events, ApplicationCommandPermissionType } = require("discord.js")
const fs = require("fs")
const { type } = require("os")
// TOKEN is stored in the .env file (node --env-file=.env index.js)
const token = process.env.TOKEN
@ -88,9 +89,27 @@ client.on("messageCreate", (message) => {
return
}
if (!realCommand.arguments) {
// No arguments so no need to parse them
// Run command with no args
realCommand.run(message)
} else {
// Removing command from the args
const args = parsed.filter(value => {return value != command})
if (args.length < realCommand.arguments.length) {
const missing = realCommand.arguments.slice(realCommand.arguments.length - args.length)
missing.forEach((argument, index) => {
if (typeof argument == "object" || typeof argument == "function") {
missing[index] = argument.name
}
})
return message.reply(`Missing arguments ${missing}`)
}
var wrongArg = false
// Parsing
args.forEach((arg, index) => {
if (arg.startsWith("<@")) {
@ -113,15 +132,23 @@ client.on("messageCreate", (message) => {
}
}
if (typeof args[index] != typeof realCommand.arguments[index] || !(args[index] instanceof realCommand.arguments[index])) {
return message.reply(`Argument ${index} of wrong type`)
// Checks if the type is wrong
if (typeof args[index] != typeof realCommand.arguments[index] && !(args[index] instanceof realCommand.arguments[index])) {
wrongArg = true // So it cant continue
return message.reply(`Argument ${index} supposed to be ${realCommand.arguments[index].name}`)
}
})
if (wrongArg) {
return
}
// Run command
realCommand.run(message, ...args)
//...args passes it as separate args rather than an Array
})
}
}
)
// After getting the command list we can export it
module.exports.commands = commands

3
package-lock.json generated
View File

@ -10,7 +10,8 @@
"license": "MIT",
"dependencies": {
"discord.js": "^14.19.3"
}
},
"devDependencies": {}
},
"node_modules/@discordjs/builders": {
"version": "1.11.2",