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) { 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
@ -87,41 +88,67 @@ client.on("messageCreate", (message) => {
if (!realCommand) { if (!realCommand) {
return return
} }
// Removing command from the args
const args = parsed.filter(value => {return value != command})
// Parsing if (!realCommand.arguments) {
args.forEach((arg, index) => { // No arguments so no need to parse them
if (arg.startsWith("<@")) { // Run command with no args
// User, lets get the id realCommand.run(message)
const id = arg.slice(2, arg.length - 1) } 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) { return message.reply(`Missing arguments ${missing}`)
args[index] = member }
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) // Checks if the type is wrong
if (typeof args[index] != typeof realCommand.arguments[index] && !(args[index] instanceof realCommand.arguments[index])) {
if (channel) { wrongArg = true // So it cant continue
args[index] = channel 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])) { // Run command
return message.reply(`Argument ${index} of wrong type`) 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 // 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",