Discord.JS 命令处理程序不工作或更新
Discord.JS Command Handler not working or updating
我正在尝试向我的机器人添加一个命令处理程序,但是,只有一个命令在工作,而且无论我如何更改命令,它都不会更新。这是我的代码。
它位于包下方和准备就绪代码上方的顶部。
fs.readdir('./commands/', (_err, files) => {
files.forEach((file) => {
if (!file.endsWith('.js')) return;
bot.commands = new Discord.Collection()
let command = require(`./commands/${file}`); // access all details though this variable
bot.commands.set(command.name, command);
console.log(` Command loaded: ${command.name}`);
});
});
这是在资源命令中
const Discord = require("discord.js")
const fs = require("fs")
module.exports = {
name: 'resources',
// any other details you might like, such as:
description: 'Shows resources',
execute(bot, message, args, prefix) {
// the actual function
let resourceText = fs.readFileSync('txtfiles/resources.txt', 'utf8')
message.suppressEmbeds(true)
message.channel.send(resourceText)
}
}
resources 命令工作正常,但 ship 命令不工作。
const Discord = require("discord.js")
const fs = require("fs")
module.exports = {
name: 'ship',
// any other details you might like, such as:
description: 'ships people',
execute(bot, message, args, prefix) {
// the actual function
message.channel.send("please work")
}
}
这是在“on messageCreate”中
if(cmd===`${prefix}resources`){bot.commands.get('resources').execute(bot, message, args, prefix);}
if(cmd===`${prefix}ship`){bot.commands.get('ship').execute(bot, message, args, prefix);}
我试过把这些放在包裹下面
delete require.cache['./index.js'];
delete require.cache[require.resolve('./index.js')]
但是,我收到此错误消息
Unhandled promise rejection: TypeError: Cannot read properties of undefined (reading 'execute')
即使我们删除文件中的所有代码,resources 命令也能正常工作,而 ship 命令根本不起作用。
因此,与其为每个命令放置一个 if (cmd ===
块,不如尝试使用一个动态的块,它不需要您在每次添加新命令或删除时更新 messageCreate
部分一.
messageCreate 部分:
bot.on('messageCreate', async message => {
if (message.author.bot) return
if (!message.content.startsWith(config.prefix)) return
const args = message.content.slice(config.prefix.length).trim().split(/ +/)
const command = args.shift().toLowerCase()
const cmd = bot.commands.get(command)
if (!cmd) {
console.log("Unknown command")
return
} else {
cmd.run(bot, message, args)
}
})
然后你必须更新你现有的命令,它确实可以匹配这种风格,但只要像这样制作任何新命令即可:
module.exports = {
name: 'Command Name',
description: 'Command description',
run: async (bot, message, args) => {
// the actual function
}
}
所以你的 ships
命令看起来像这样
module.exports = {
name: 'ships',
description: 'ships people',
run: async (bot, message, args) => {
message.channel.send({
content: 'Please work'
})
}
}
我会将您的命令集合更改为此
bot.commands = new Discord.Collection()
const commandList = []
const commands = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for (const file of commands) {
const command = require(`./commands/${file}`)
commandList.push(`${file.split('.'[0])}`)
bot.commands.set(command.name, command)
}
console.log(` Commands loaded: ${commandList}`)
我正在尝试向我的机器人添加一个命令处理程序,但是,只有一个命令在工作,而且无论我如何更改命令,它都不会更新。这是我的代码。
它位于包下方和准备就绪代码上方的顶部。
fs.readdir('./commands/', (_err, files) => {
files.forEach((file) => {
if (!file.endsWith('.js')) return;
bot.commands = new Discord.Collection()
let command = require(`./commands/${file}`); // access all details though this variable
bot.commands.set(command.name, command);
console.log(` Command loaded: ${command.name}`);
});
});
这是在资源命令中
const Discord = require("discord.js")
const fs = require("fs")
module.exports = {
name: 'resources',
// any other details you might like, such as:
description: 'Shows resources',
execute(bot, message, args, prefix) {
// the actual function
let resourceText = fs.readFileSync('txtfiles/resources.txt', 'utf8')
message.suppressEmbeds(true)
message.channel.send(resourceText)
}
}
resources 命令工作正常,但 ship 命令不工作。
const Discord = require("discord.js")
const fs = require("fs")
module.exports = {
name: 'ship',
// any other details you might like, such as:
description: 'ships people',
execute(bot, message, args, prefix) {
// the actual function
message.channel.send("please work")
}
}
这是在“on messageCreate”中
if(cmd===`${prefix}resources`){bot.commands.get('resources').execute(bot, message, args, prefix);}
if(cmd===`${prefix}ship`){bot.commands.get('ship').execute(bot, message, args, prefix);}
我试过把这些放在包裹下面
delete require.cache['./index.js'];
delete require.cache[require.resolve('./index.js')]
但是,我收到此错误消息
Unhandled promise rejection: TypeError: Cannot read properties of undefined (reading 'execute')
即使我们删除文件中的所有代码,resources 命令也能正常工作,而 ship 命令根本不起作用。
因此,与其为每个命令放置一个 if (cmd ===
块,不如尝试使用一个动态的块,它不需要您在每次添加新命令或删除时更新 messageCreate
部分一.
messageCreate 部分:
bot.on('messageCreate', async message => {
if (message.author.bot) return
if (!message.content.startsWith(config.prefix)) return
const args = message.content.slice(config.prefix.length).trim().split(/ +/)
const command = args.shift().toLowerCase()
const cmd = bot.commands.get(command)
if (!cmd) {
console.log("Unknown command")
return
} else {
cmd.run(bot, message, args)
}
})
然后你必须更新你现有的命令,它确实可以匹配这种风格,但只要像这样制作任何新命令即可:
module.exports = {
name: 'Command Name',
description: 'Command description',
run: async (bot, message, args) => {
// the actual function
}
}
所以你的 ships
命令看起来像这样
module.exports = {
name: 'ships',
description: 'ships people',
run: async (bot, message, args) => {
message.channel.send({
content: 'Please work'
})
}
}
我会将您的命令集合更改为此
bot.commands = new Discord.Collection()
const commandList = []
const commands = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for (const file of commands) {
const command = require(`./commands/${file}`)
commandList.push(`${file.split('.'[0])}`)
bot.commands.set(command.name, command)
}
console.log(` Commands loaded: ${commandList}`)