How to make a Discord bot respond differently to specific users?

I’m working on a Discord bot that replies with random messages when users type a ‘hello’ command. Now I want to make it give special responses to certain users based on their User ID. Here’s what I tried:

@bot.command()
async def greet(ctx):
    if ctx.author.id == 123456789:
        replies = ['Go away', 'Not you again', 'What now?']
        await ctx.send(random.choice(replies))
    elif ctx.author.id == 987654321:
        replies = ['Oh, it's you', 'Hi there']
        await ctx.send(random.choice(replies))
    else:
        regular_replies = ['Hello!', 'Hi', 'Hey there']
        await ctx.send(random.choice(regular_replies))

But it’s not working. The bot doesn’t respond differently to specific users. What am I doing wrong? How can I fix this to make the bot recognize and respond uniquely to certain User IDs?

yo, have u tried using a switch statement instead? it might be cleaner. also, make sure ur bot has the right permissions in the server. sometimes that can mess things up. if all else fails, try adding some debug prints to see whats goin on behind the scenes

I’ve implemented something similar in my Discord bot, and I can share what worked for me. Instead of hardcoding the user IDs in the function, I found it more flexible to use a dictionary to store user-specific responses. Here’s an approach that might help:

user_responses = {
    123456789: ['Go away', 'Not you again', 'What now?'],
    987654321: ['Oh, it's you', 'Hi there'],
}

@bot.command()
async def greet(ctx):
    user_id = ctx.author.id
    if user_id in user_responses:
        await ctx.send(random.choice(user_responses[user_id]))
    else:
        regular_replies = ['Hello!', 'Hi', 'Hey there']
        await ctx.send(random.choice(regular_replies))

This method makes it easier to add or modify user-specific responses without changing the function itself. Also, make sure you’ve enabled the necessary intents in your bot setup, as mentioned by SurfingWave. If you’re still having issues, try printing the user ID to ensure it’s being captured correctly.

Your code looks structurally correct, but the issue might be with how you’re obtaining the User IDs. Double-check that the IDs you’re using (123456789 and 987654321) are actually correct for the users you’re targeting. You can verify this by printing ctx.author.id in your command function.

Also, ensure your bot has the necessary intents enabled, particularly the ‘members’ intent. Without it, the bot might not be able to access user information properly.

If those checks don’t solve it, try using str(ctx.author.id) instead of just ctx.author.id in your comparisons. Sometimes, ID comparisons can be finicky with type matching.

Lastly, for better maintainability, consider using a dictionary to store user-specific responses. It’ll make adding or modifying responses easier in the future.