简体   繁体   English

如何阻止 discord 机器人回复自己或其他机器人? (discord.js)

[英]how can i stop a discord bot from replying to itself or other bots? (discord.js)

i'm a newbie in this whole coding thing.我是整个编码领域的新手。 i started working on my first discord bot a couple of days ago, you know, for me and my friends to mess around with.几天前,我开始研究我的第一个 discord 机器人,你知道,我和我的朋友们可以玩弄。 now, let's say that i want this bot to detect words in a message and reply every time someone mentions that word, no matter in which part of the message.现在,假设我希望这个机器人检测消息中的单词并在每次有人提到该单词时回复,无论消息的哪个部分。 i was able to do this, but now there's a problem.我能够做到这一点,但现在有一个问题。 let's say that the word I'm looking for is "hello".假设我要找的词是“你好”。 if someone says "oh hello", "hello there", a message with the word hello, the bot will reply "hello" back.如果有人说“哦,你好”,“你好”,一条带有“你好”的消息,机器人会回复“你好”。 but the bot will also detect the hello in its own message and reply to itself over and over until i shut it down.但机器人也会在自己的消息中检测到你好,并一遍又一遍地回复自己,直到我将其关闭。 here's the code:这是代码:

bot.on("message", message => {
    const hello = ["hello"];
    if( hello.some(word => message.content.includes(word)) ) {
        message.channel.send("Hello!");
}} ) 

so, i can't figure out how to make the bot not see that "hello" in its own message, or any bot's message if that's easier, but be able to analyze the "hello" from a user, so that it isn't stuck in an infinite loop of replying to itself.所以,我不知道如何让机器人在自己的消息中看不到“你好”,或者如果这更容易的话,也看不到任何机器人的消息,但能够分析来自用户的“你好”,所以它不是t 陷入了回复自己的无限循环。 how can i do this??我怎样才能做到这一点?? thank you in advance (:先感谢您 (:

If want to prevent the bot from replying to itself:如果想阻止机器人回复自己:

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

You can also prevent a bot from replying to another bot:您还可以阻止机器人回复另一个机器人:

if (message.author.bot)
   return;

Just check if the message author is a bot using message.author.bot只需使用message.author.bot检查消息作者是否是机器人

bot.on("message", message => {
    if (message.author.bot) return
    const hello = ["hello"];
    if( hello.some(word => message.content.includes(word)) ) {
        message.channel.send("Hello!");
}
})

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

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