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,14 +29,11 @@ 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
@ -87,41 +88,67 @@ client.on("messageCreate", (message) => {
if (!realCommand) {
return
}
// Removing command from the args
const args = parsed.filter(value => {return value != command})
// Parsing
args.forEach((arg, index) => {
if (arg.startsWith("<@")) {
// User, lets get the id
const id = arg.slice(2, arg.length - 1)
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})
const member = message.guild.members.cache.find(member => member.user.id == id)
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
}
})
if (member) {
args[index] = member
return message.reply(`Missing arguments ${missing}`)
}
var wrongArg = false
// Parsing
args.forEach((arg, index) => {
if (arg.startsWith("<@")) {
// User, lets get the id
const id = arg.slice(2, arg.length - 1)
const member = message.guild.members.cache.find(member => member.user.id == id)
if (member) {
args[index] = member
}
} else if (arg.startsWith("<#")) {
// Channel, lets get the id
const id = arg.slice(2, arg.length - 1)
const channel = message.guild.channels.cache.find(channel => channel.id == id)
if (channel) {
args[index] = channel
}
}
} else if (arg.startsWith("<#")) {
// Channel, lets get the id
const id = arg.slice(2, arg.length - 1)
const channel = message.guild.channels.cache.find(channel => channel.id == id)
if (channel) {
args[index] = channel
// 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
}
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)
//...args passes it as separate args rather than an Array
}
})
// 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",