简体   繁体   English

我的不和谐机器人一次发送多条消息,而不是一条

[英]My discord bot is sending multiple messages at once instead of just one

I am new to Javascript and Discord Bots.我是 Javascript 和 Discord Bots 的新手。 I am trying to make a simple bot that just copies what a user says and then sends that exact message back.我正在尝试制作一个简单的机器人,它只复制用户所说的内容,然后将准确的消息发回。 I am having difficulties though because for some reason the bot sends multiple messages at once instead of a singular message.但我遇到了困难,因为出于某种原因,机器人一次发送多条消息而不是一条消息。

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

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
    colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
   token: auth.token,
   autorun: true
});
bot.on('ready', function (evt) {
    logger.info('Connected');
    logger.info('Logged in as: ');
    logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {

    if (message) {
        var messageArray = message.substring(0).split(' ');
        var recentMessage = messageArray[0];
       
        messageArray = messageArray.splice(1);
        switch(recentMessage) {
            default:
                bot.sendMessage({
                    to:channelID,
                    message: recentMessage
                })
                break;
         }
     }
});

Thank you谢谢

If you look at the discord ping example , you can access the contents of the message with message.content .如果您查看discord ping 示例,您可以使用message.content访问消息的内容。 I'm not sure, but you code looks like it is breaking the message object apart which might be why you're getting multiple messages back.我不确定,但您的代码看起来像是将message对象分开,这可能就是您收到多条消息的原因。

You might try changing your code to be:您可以尝试将代码更改为:

bot.on('message', function (message) {
   // Send message back on the same channel
   message.channel.send(message.content);
});

or to reply或回复

bot.on('message', function (message) {
   // Reply to message with the same message
   message.reply(message.content);
});

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

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