I made a simple code that should reply when someone mentions the bot.
client.on("messageCreate", (message) => {
if (message.content === "<@935849545789763585>" || '<@!935849545789763585>') {
message.channel.send('Hi');
}
});
But I have a problem because the bot is replying multiple times.
CodePudding user response:
I see you're trying to check if the message content is <@935849545789763585>
(mentioning your bot). (You should use MessageMentions by the way.)
The issue is with your if condition.
message.content === "<@935849545789763585>" || '<@!935849545789763585>'
When you mention your bot, the first condition passes, as it should. When the first condition fails, the OR
operator will check if the second condition is true, in this case, '<@!935849545789763585>'
will ALWAYS be true since it is not an empty string and is not comparing anything.
Your if statement is basically evaluated like this:
message.content === "<@935849545789763585>" || true
The solution is to just remove the second condition.
client.on("messageCreate", (message) => {
if (message.content === "<@935849545789763585>") {
message.channel.send("Hi");
}
});
CodePudding user response:
It's because if (message.content === "<@935849545789763585>" || '<@!935849545789763585>')
always returns true
. It doesn't matter what the value of message.content
is because '<@!935849545789763585>'
is a truthy value. Check out the snippet below:
let message = { content: false }
if (message.content === "<@935849545789763585>" || "<@!935849545789763585>") {
console.log(true)
}
As your if statement is incorrect, you send a new message every single time there is an incoming message.
You could check the mentions
instead:
if (message.mentions.members.has(client.user.id)) {
message.channel.send('Hi');
}
Or:
if (message.mentions.members.has('3584954578976358')) {
message.channel.send('Hi');
}