简体   繁体   English

Discord.js 说命令

[英]Discord.js say command

I'm working on a discord bot that has a say command.我正在开发一个 discord 机器人,它有一个说命令。 But my results are different to what I expect.但是我的结果与我的预期不同。

Here is my code:这是我的代码:

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 file: say.js 文件:

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

My expectations: 我的期望:

User: $say stack overflow is cool Bot: stack overflow is cool用户: $say 堆栈溢出很酷Bot:堆栈溢出很酷

The output: output:

User : $say stack overflow is cool用户:$say 堆栈溢出很酷

Bot :机器人

stack

overflow溢出

is

cool凉爽的

The problem here is that your "args" argument is an array of strings.这里的问题是您的“args”参数是一个字符串数组。 When you send it with the send function, each element of the array will be send with a new line between them.当您使用发送 function 发送它时,数组的每个元素都将在它们之间用一个新行发送。

If you look the documentation, you can see that send() wants a String Resolvable , and an array IS a valid String Resolvable value, but it comes with a special behavior.如果您查看文档,您可以看到 send() 需要一个String Resolvable ,并且一个数组是一个有效的 String Resolvable 值,但它具有特殊的行为。

Try using the join method on your argument before sending it.在发送参数之前尝试在参数上使用join 方法 Here's an example:这是一个例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM