discord.js bot replies to itself

前端 未结 5 1060
情话喂你
情话喂你 2020-12-19 01:38

I am currently coding my first discord bot, it can already play YouTube music.

if (message.content.includes(\"         


        
相关标签:
5条回答
  • 2020-12-19 01:50

    Use this in the on message event:

    if (message.author.bot) return;
    

    for more info: https://anidiotsguide.gitbooks.io/discord-js-bot-guide/coding-guides/a-basic-command-handler.html

    0 讨论(0)
  • 2020-12-19 01:53

    You may use this code which avoids doing anything if the author is a bot:

    if(message.author.bot) return;
    
    0 讨论(0)
  • 2020-12-19 02:03

    The reason why your bot replies to yourself is because where you put:

    if (message.content.includes("Good Job") || 
        message.content.includes("good job"))
    

    It is basically checking the chat if a piece of text includes the words "Good Job" or "good job". As your bot sends:

    message.channel.sendMessage("Good Job everyone :smirk:");
    

    as an answer, it creates a loop because that message includes the words "Good Job", basically running the code again and again and again.

    Of course, the easiest way of fixing this is for you to change the answer it gives to not include the words Good Job, but there is better solution to make sure it doesn't happen for all of the commands you might make.

    As @Jörmungandr said, under the message event include:

    if (message.author.bot) return;
    

    to make sure it doesn't happen. It essentially checks if the author of the message is a bot and if it is, it ignores it.

    discord.js

    0 讨论(0)
  • 2020-12-19 02:05

    You can simply just check if the user sending the message is a bot. For example:

    if (!msg.author.bot) {
        <Your code to execute if the user is not a bot.>
    } 
    

    Hope this was helpful, thank you!

    0 讨论(0)
  • 2020-12-19 02:14
    // In message event
    if(message.author.id === client.user.id) return;
    
    // I would recommend a variable like this for splits on words
    // const args = message.content.trim().split(/\s+/g); 
    // You could also .slice() off a prefix if you have one
    
    if(/good job/i.test(message.content)) {
      message.channel.send('Good job everyone :smirk:'); // sendMessage is deprecated, use send instead
    }
    
    0 讨论(0)
提交回复
热议问题