【问题标题】:How to make a bot not respond to itself discord.js [duplicate]如何使机器人不响应自身 discord.js [重复]
【发布时间】:2022-01-24 19:55:17
【问题描述】:

所以我知道让机器人不响应自己的方法是放在if (message.author.id === client.user.id) return;if(message.author.bot) return; 但我不知道我应该把它放在哪里。我的代码是这样的:

const {Client, Intents} = require('discord.js'); 
const dotenv = require('dotenv')

dotenv.config();

const client = new Client(
    {
        intents:[
             Intents.FLAGS.GUILDS,
             Intents.FLAGS.GUILD_MESSAGES,
        ]
    }
);

client.on('ready', ()=>{
    console.log('bot working ngl')
})

//greetings
const random_greeting = () =>{
    return Math.floor(Math.random() * 6);
  }
  
  client.on('messageCreate', msg=>{
    let greeting = ['Hi', 'Yo', 'Ohayo', 'Hello', '????????', 'Ok...',]
    if (msg.content === 'hi', 'yo'){
      msg.reply(greeting[random_greeting()])
    }
  })

  client.on('message', message => {
    if (message.author.id === client.user.id) return;
  })
client.login(process.env.TOKEN)

正如您看到的,当有人打招呼时,机器人会通过说出列表中 6 个单词中的 1 个来打招呼,但问题是,每当机器人用该列表词回复某人时,就会继续向这些词发送垃圾邮件。什么代码可以阻止它自己回复,我应该把它放在哪里

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    很简单。在您的 messageCreate 事件中,只需将其添加为事件的第一行:

    if(msg.author == client.user) return;

    【讨论】:

    • 改了好几遍,记错了。
    【解决方案2】:

    只需添加到您的 messageCreate 事件:

      if (message.author.bot) return;
    

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 2021-11-21
      • 2020-08-09
      • 2021-01-06
      • 2021-08-30
      • 2018-02-19
      • 2020-03-28
      • 2021-01-07
      • 2020-10-11
      相关资源
      最近更新 更多