Discord.js说命令

我正在开发一个有 say 命令的不和谐机器人。但我的结果与我的预期不同。

这是我的代码:

Discord = require('discord.js');
client = new Discord.Client();
prefix = '$';
fs = require('fs');
.commands = new Discord.Collection();
commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Bot is online!');
});

client.on('message', message => {
    if(!message.content.startsWith(prefix) || message.author.bot) return;
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'say') {
        client.commands.get('say').execute(message, args, Discord);
    }
});

说.js文件:

module.exports = {
    name: 'say',
    description: 'The bot says thing.',
    execute(message, args, Discord) {
        message.channel.send(args);
    }
}

我的期望:

用户: $say 堆栈溢出很酷
Bot:堆栈溢出很酷

输出:

用户:$say 堆栈溢出很酷

机器人

溢出

凉爽的

回答

这里的问题是你的“args”参数是一个字符串数组。当您使用 send 函数发送它时,数组的每个元素将在它们之间以新行发送。

如果您查看文档,您可以看到 send() 需要一个String Resolvable,并且一个数组是一个有效的 String Resolvable 值,但它具有特殊的行为。

在发送之前尝试在参数上使用join 方法。下面是一个例子:

message.channel.send(args.join(' '));

  • "If you look the documentation, you can see that send() wants a String Resolvable" When citing from an online source, please add a link to it and also add quotes from the actual text when possible (currently there is only a link to a screenshot on a third-party website).

以上是Discord.js说命令的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>