Discord.JS 机器人 - 如何在命令名称中包含空格? [英] Discord.JS bot - how can I include spaces in my command names?

查看:20
本文介绍了Discord.JS 机器人 - 如何在命令名称中包含空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I include spaces in my command names, those commands won't run. I'm using a command handler and in my index.js file, we have:

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 文件中,我们有:

and in my command.js file, we have:

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

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

When I change command name to command-name or to commandname it works! But when it's command name, my bot doesn't output any response... not even an error message. Where am I going wrong? Is there some simple solution or workaround that I'm overlooking? Thanks in advance.

推荐答案

这是因为你 shift() 你的参数在一行

This is because you shift() your arguments in the line

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

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

This takes the first argument and returns it, any following arguments will not be included. To take the first 2 argument use

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

command 更改为以下内容以考虑任何其他参数.

Change command to the following to account for any other arguments.

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

这篇关于Discord.JS 机器人 - 如何在命令名称中包含空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆