简体   繁体   English

Discord.JS 命令处理程序不工作或更新

[英]Discord.JS Command Handler not working or updating

I am trying to add a command handler to my bot, however, only one command is working and no matter how I change the command, it does not update.我正在尝试向我的机器人添加一个命令处理程序,但是,只有一个命令在工作,无论我如何更改命令,它都不会更新。 Here is my code.这是我的代码。

This is at the top underneath the packages and above the on ready code.它位于包下方和就绪代码上方的顶部。

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}`);
 });
});

This is in the resources command这是在资源命令中

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)
    
 }
 }

The resources command works just fine, but the ship command does not. 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")
    
 }
 }

And this is in the "on messageCreate" thing这是在“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);}

I have tried putting these underneath the packages我试过把这些放在包裹下面

delete require.cache['./index.js'];
delete require.cache[require.resolve('./index.js')]

However, I am getting this error message Unhandled promise rejection: TypeError: Cannot read properties of undefined (reading 'execute')但是,我收到此错误消息Unhandled promise rejection: TypeError: Cannot read properties of undefined (reading 'execute')

The resources command works just fine, even if we delete all the code in the file, and the ship command does not work at all. resources 命令工作得很好,即使我们删除了文件中的所有代码,而 ship 命令根本不起作用。

So rather than put an if (cmd === block for every command, try using a dynamic one that won't require you to update your messageCreate section, every time you add a new command or delete one.因此,与其为每个命令放置一个if (cmd ===块,不如尝试使用一个动态的,它不需要您在每次添加新命令或删除命令时更新您的messageCreate部分。

messageCreate section:消息创建部分:

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)
    }
})

Then you'll have to update your existing command that does work to match this style but just make any new ones like this:然后你必须更新你现有的命令,它确实可以匹配这种风格,但只需像这样制作任何新命令:

module.exports = {
    name: 'Command Name',
    description: 'Command description',
    run: async (bot, message, args) => {
        // the actual function
    }
}

So your ships command would look like this所以你的ships命令看起来像这样

module.exports = {
    name: 'ships',
    description: 'ships people',
    run: async (bot, message, args) => {
        message.channel.send({
            content: 'Please work'
        })
    }
}

I'd change your command collection to this我会将您的命令集合更改为此

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}`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM