thehiddencheese
thehiddencheese

Reputation: 93

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>.`
    );
  }
});

Upvotes: 0

Views: 162

Answers (1)

Dominik
Dominik

Reputation: 6313

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>.`
    );
  }
});

Upvotes: 4

Related Questions