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) { if (arguments) {
arguments.forEach((argument, index) => { arguments.forEach((argument, index) => {
console.log(typeof argument)
if (typeof argument == "object" || typeof argument == "function") { if (typeof argument == "object" || typeof argument == "function") {
arguments[index] = argument.name arguments[index] = argument.name
} }
}) })
} }
console.log(arguments)
const field = {name: command.name, value: `Description: ${command.description || "None"}\nArguments: ${arguments || "None"}`} const field = {name: command.name, value: `Description: ${command.description || "None"}\nArguments: ${arguments || "None"}`}
embed.addFields(field) embed.addFields(field)
}) })

View File

@ -1,6 +1,7 @@
// Base requires // Base requires
const { Client, GatewayIntentBits, Events } = require("discord.js") const { Client, GatewayIntentBits, Events, ApplicationCommandPermissionType } = require("discord.js")
const fs = require("fs") const fs = require("fs")
const { type } = require("os")
// TOKEN is stored in the .env file (node --env-file=.env index.js) // TOKEN is stored in the .env file (node --env-file=.env index.js)
const token = process.env.TOKEN const token = process.env.TOKEN
@ -88,9 +89,27 @@ client.on("messageCreate", (message) => {
return 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 // Removing command from the args
const args = parsed.filter(value => {return value != command}) 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 // Parsing
args.forEach((arg, index) => { args.forEach((arg, index) => {
if (arg.startsWith("<@")) { 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])) { // Checks if the type is wrong
return message.reply(`Argument ${index} of wrong type`) 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 // Run command
realCommand.run(message, ...args) realCommand.run(message, ...args)
//...args passes it as separate args rather than an Array //...args passes it as separate args rather than an Array
}) }
}
)
// After getting the command list we can export it // After getting the command list we can export it
module.exports.commands = commands module.exports.commands = commands

3
package-lock.json generated
View File

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