【问题标题】:JavaScript Command Handler Not Running Additional Commands?JavaScript 命令处理程序没有运行其他命令?
【发布时间】:2021-06-06 22:42:16
【问题描述】:

我的 discord 机器人有一个命令处理程序。它可以很好地加载命令模块(并且控制台日志也表明了这一点),但是我无法让它执行第一个命令模块之外的命令。代码如下:

const Discord = require("discord.js");
const client = new Discord.Client();
client.commands = new Discord.Collection();

const prefix = "f$";

const fs = require("fs");
try {
    const files = fs.readdirSync("./commands/");
    let jsFiles = files.filter(filename => filename.split(".").pop() === "js");
    for (const filename of jsFiles) {
        let command = require(`./commands/${filename}`);
        if (!command) return console.error(`Command file '${filename}' doesn't export an object.`);
        client.commands.set(command.name, command);
        console.log(`Loaded command: ${command.name}`);
    }
} catch (err) {
    console.error(`Error loading command: '${err}'`);
}
console.log("Finished loading\n");


client.on("ready", () => {
    console.log("Client ready!");
});

client.on("message", async message => {
    if (message.author.bot) return;
    if (!message.content.startsWith(prefix)) return;
    let args = message.content.slice(prefix.length).trim().split(/ +/g);
    //console.log(args);
    let cmdName = args.shift().toLowerCase();
    for (const command of client.commands.values()) {
        console.log(command.name);
        console.log(cmdName);
        console.log(command === cmdName);
        if (command.name !== cmdName /*&& !command.aliases.includes(cmdName)*/) return;
        try {
            await command.executor(message, args, client);
        } catch (err) {
            console.error(`Error executing command ${command.name}: '$```{err.message}'`);
        }
    }

});

client.login(TOKEN).catch(err => console.log(`Failed to log in: ${err}`));

在每个命令模块中你都有这个位:

module.exports = {
    name: "command",
    description: "description of command",
    aliases: [],
    executor: async function (message, args, client) {

(我还没有为此做别名,所以别名行要么是空的,要么被删除。)

【问题讨论】:

    标签: javascript node.js discord.js bots


    【解决方案1】:

    你在模板文字中错误地引用了你的变量,这意味着你的很多代码被忽略了,这可能是问题的全部,它应该看起来像这样:

    console.error(`Error executing command ${command.name}: ${err.message}`);
    

    有关模板文字的指导,请使用https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals。如果您想使用 ``` 但忽略它们,请使用以下代码:

    console.error(`Error executing command ${command.name}: \`\`\` ${err.message} \`\`\`  `);
    

    此外,在您的执行功能上,您没有右大括号,因此应该是:

    module.exports = {
        name: "command",
        description: "description of command",
        aliases: [],
        executor: async function (message, args, client) {
    // code to be executed
    }
    

    【讨论】:

    • 我没有完全关注你。您引用了 console.error 行,我明白您对这些行编写不正确的意思。但是,我没有看到代码中出现同样错误的位置。我错过了什么?
    • 你还没有在 module.exports 上关闭你的花括号
    • 是的,它在实际代码中是封闭的。没有错误,其他命令只是不会触发,(但它们已成功加载。)看起来这行有问题:``` if (command.name !== cmdName /*&& !command. aliases.includes(cmdName)*/) 返回; ``` 第一个command.name 定义为command1,如果是command2 被调用,它会命中返回,因为command.name 和cmdName 不匹配。 (这是正确的。)但是,它不会以 command.name 作为 command2 重新启动检查,而 command2 将实际执行。 (控制台日志确实显示了这一点。)
    猜你喜欢
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 2021-11-24
    相关资源
    最近更新 更多