Discord.js say command



我正在制作一个有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);
}
});

say.js文件:

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

我的期望:用户:说堆栈溢出很酷机器人:栈溢出很酷

输出:

用户: $say stack overflow is cool

机器人:

堆栈

溢出

问题是你的"args"参数是字符串数组。当你用send函数发送它时,数组的每个元素将被发送,它们之间将有一个新的行。

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

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

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

👋

最新更新