【问题标题】:Discord.JS bot - how can I include spaces in my command names?Discord.JS 机器人 - 如何在命令名称中包含空格?
【发布时间】:2021-07-26 05:44:09
【问题描述】:

当我在命令名称中包含空格时,这些命令将不会运行。我正在使用命令处理程序,在我的 index.js 文件中,我们有:

client.on('message', msg => {
    if (!msg.content.startsWith(prefix) || msg.author.bot) return;
    const args = msg.content.slice(prefix.length).trim().split(/ +/g);
    const commandName = args.shift().toLowerCase();
    const command = client.commands.get(commandName)
    || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
    if (!command) return;
    try {
    command.execute(msg, args);
    } catch (error) {
    console.error(error);
    msg.reply("I'm encountering an error trying to execute that command. Please try again.");
    }
    });

在我的command.js 文件中,我们有:

module.exports = {
    name: 'command name',
    execute(msg, args) {
    msg.channel.send("command");
    },
};

当我将command name 更改为command-namecommandname 时,它可以工作!但是当它是command name 时,我的机器人没有输出任何响应......甚至没有错误消息。我哪里错了?是否有一些我忽略的简单解决方案或解决方法?提前致谢。

【问题讨论】:

    标签: discord discord.js bots


    【解决方案1】:

    这是因为你shift()你的论点在行

    const commandName = args.shift().toLowerCase();
    

    这需要第一个参数并返回它,任何后面的参数都不会包括在内。 要采用前 2 个参数,请使用

    const commandName = args.splice(0, 2);
    

    command 更改为以下内容以解释任何其他参数。

    const command = client.commands.get(commandName.join(' ').toLowerCase())
        || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName.join(' ').toLowerCase()));
    

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 2020-02-22
      • 2021-04-18
      • 2021-06-02
      • 1970-01-01
      • 2013-04-05
      • 2021-06-02
      • 2020-12-05
      • 2020-11-09
      相关资源
      最近更新 更多