0

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>.`
    );
  }
});
thehiddencheese
  • 83
  • 1
  • 11
  • 1
    Replace `if (message.content === '!help' || '!command') {` with `if (message.content === '!help' || message.content === '!command') {` – Dominik Dec 03 '20 at 04:23
  • This fixed it, I'm curious why that is though? Edit: Just seen your post – thehiddencheese Dec 03 '20 at 04:25
  • 1
    I added an answer – Dominik Dec 03 '20 at 04:25
  • Does this answer your question? [Javascript: The prettiest way to compare one value against multiple values](https://stackoverflow.com/questions/9121395/javascript-the-prettiest-way-to-compare-one-value-against-multiple-values) – Sebastian Simon Dec 03 '20 at 17:33

1 Answers1

4

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>.`
    );
  }
});
Dominik
  • 6,078
  • 8
  • 37
  • 61