简体   繁体   中英

Discord.js bot looping using || or operator

I am having an issue when I use the or opperator ||the bot will send 5 messages at once, and continue to loop. Without using the or operator it works fine. It will also accept some letters like c, or h to start the loop, which is odd.

This is the code it will print out onto the discord.

Hello @thehiddencheese! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact @thehiddencheese.
Hello @thehiddencheese's test bot! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact @thehiddencheese.
Hello @thehiddencheese's test bot! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact @thehiddencheese.
Hello @thehiddencheese's test bot! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact @thehiddencheese.
Hello @thehiddencheese's test bot! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact @thehiddencheese.

Here is the code

client.on('message', (message) => {
  let targetMember = message.member.user;
  if (message.content === '!help' || '!command') {
    message.channel.send(
      `Hello ${targetMember}! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact <@248030367666274304>.`
    );
  }
});

The statement if (message.content === '!help' || '!command') { has two blocks: message.content === '!help' and '!command' . The || or operator says: "either of those things should be true".

So it ignores the first block because the second block will always be truthy .

What you're trying to do is this:

client.on('message', (message) => {
  let targetMember = message.member.user;
  if (message.content === '!help' || message.content === '!command') {
    message.channel.send(
      `Hello ${targetMember}! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact <@248030367666274304>.`
    );
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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