How to make a Discord bot respond to a particular user's messages

I’m working on a Discord bot and I want it to react whenever a specific user types anything in the chat. Here’s what I’ve tried so far:

const targetUserId = '1234567890'

bot.on('messageCreate', (msg) => {
  if (msg.author.id === targetUserId) {
    msg.reply('I see you!')
  }
})

This code is supposed to make the bot reply ‘I see you!’ when the user with the ID ‘1234567890’ sends a message. I’ve changed the actual user ID for privacy reasons.

Is this the right way to do it? The bot doesn’t seem to be responding. Am I missing something? Also, is there a way to make the bot’s responses more varied so it’s not always the same message?

Your approach is on the right track, but there might be a few reasons why it’s not working. First, double-check that you’ve correctly copied the user ID. It’s easy to mistype those long numbers.

If that’s not the issue, make sure your bot has the necessary permissions in the Discord server. It needs to be able to read messages and send responses in the channels where you want it to work.

For varied responses, you could use an array of messages and pick one randomly. Something like:

const responses = ['I see you!', 'Hello there!', 'Fancy meeting you here!'];
const randomResponse = responses[Math.floor(Math.random() * responses.length)];
msg.reply(randomResponse);

This will add some variety to your bot’s replies. Hope this helps!

Your code looks correct, but there could be a few reasons why it’s not responding. Make sure your bot is properly connected and has the necessary permissions in the server. Also, verify that the user ID is correct and that the bot is in the same channels as the target user.

For more varied responses, you could create an array of messages and randomly select one each time. This would make the bot’s interactions more dynamic and engaging. Additionally, consider adding a cooldown mechanism to prevent the bot from responding too frequently to the same user, which could be annoying.

If you’re still having issues, try logging the incoming messages to see if the event is being triggered at all. This can help pinpoint where the problem might be occurring in your code.

hey mate, ur code seems ok but check if the bot is online and has perms. also, confirm the targetID. try randomizing replies from an array for varition. hope it helps!