【问题标题】:My Discord bot is messaging continuously even though I programmed it to do it once我的 Discord 机器人正在持续发送消息,即使我将它编程为只发送一次
【发布时间】:2021-08-19 07:20:11
【问题描述】:

我正在创建一个不和谐机器人并正在对其进行测试。当我说 Hi 或 Yo 时,它会给我发送一个表情符号。 它工作正常,但问题是它是不断地发送表情符号。

这是我的代码:

console.log('Beep Beep! ????');

const Discord = require('discord.js');
const client = new Discord.Client();  // Client is the object that connects with the discord API itself to run the bot
client.login('API');
client.on('ready', readyDiscord);

function readyDiscord(){
    console.log('????');
}

client.on('message', gotMessage);

function gotMessage(msg){
    if (msg.channel.id == '849147593392914453' && msg.content === 'Hi' || 'Yo'){
         // msg.reply('????????');
         msg.channel.send('????????');
    }
}

【问题讨论】:

    标签: node.js discord bots


    【解决方案1】:

    好像你在这篇文章中添加了API token。 API 令牌应始终保密。

    其次,如果您只尝试发送一次消息,则可以使用a closure。我在本地测试了附加的 sn-p,它工作正常。消息只会发送一次。

    function gotMessage(msg){
        let isReplied = false;
    
        function reply() {
        if (!isReplied && (msg.content === 'Hi' || 'Yo')){
             console.log('??');
             isReplied = true
            }
        }
        return reply
    }
    

    【讨论】:

    • 是的,我忘记了令牌
    【解决方案2】:

    您不必关注我的,因为似乎答案已经在这里,但这是我要做的。

    另外,不要向任何人展示您的令牌。令牌是您的机器人的密钥,因此我建议您在 Discord 开发者门户中重置您的机器人令牌。

    无论如何,这就是我要做的:

    const Discord = require('discord.js');
    const client = new Discord.Client();
    client.login('TOKEN_HERE');
    client.once('ready', () => {
          console.log('I am ready to help others! ?');
    });
    
    const channelID = '849147593392914453';
    
    client.on('message', message => {
    if (message.channel.id == channelID && message.content.toLowerCase() === 'hi' || 'yo' || 'hey'){
             message.reply('Hey! ??');
        }
    });
    

    【讨论】:

    • 是的,我忘记了令牌
    猜你喜欢
    • 2021-04-19
    • 2019-09-12
    • 2021-08-19
    • 2023-03-22
    • 2021-01-08
    • 2022-01-23
    • 2021-05-23
    • 2021-11-24
    • 2021-05-14
    相关资源
    最近更新 更多