How to make Discord bot send different messages based on specific user IDs

I’m working on a Discord bot that responds to a greeting command. Currently, it picks a random reply from a list when users type the command.

I want the bot to give unique responses when specific users run the command. I attempted to check the user ID, but I’m encountering an error that says ‘message is not defined’.

Here’s the code I used:

@mybot.command()
async def greet():
    if message.author.id == ('123456789012345678'):
        responses_a = ('go away', 'not you again', 'leave me alone', 'Hello there', 'oh no its you', 'hmm...', 'please leave', 'WHAT NOW', 'HEYYYYYYYYYY', 'i dont like you')
        await mybot.say(random.choice(responses_a))
    elif message.author.id == ('987654321098765432'):
        responses_b = ('sarah...', 'hello sarah.')
        await mybot.say(random.choice(responses_b))

I believe the issue is that ‘message’ isn’t available in this context. How can I get the user ID of the person who sent the command?

you need ctx in your function, like async def greet(ctx):. use ctx.author.id for getting the user who triggered the command. also, user IDs shouldnt be in parentheses, just compare them directly.

You’re trying to access message but it’s not a parameter in your command function. Discord.py commands need ctx (context) as the first parameter - that’s how you get info about who ran the command. So your function should be async def greet(ctx): and use ctx.author.id instead of message.author.id. Also, drop the parentheses around user ID strings when comparing. Just use if ctx.author.id == '123456789012345678': or convert the ID to an integer. I made this same mistake when I started with discord.py - took me forever to figure out why I couldn’t access the message object.

Commands work differently than event handlers in discord.py. When you use @mybot.command(), the function automatically gets a context object as its first parameter. This context has all the information about who sent the command. Your function should be async def greet(ctx): and then use ctx.author.id to retrieve the user ID. Additionally, compare user IDs as integers, not as strings with parentheses. Modify your comparison to if ctx.author.id == 123456789012345678: for cleaner code. I experienced the same confusion when transitioning from message events to commands; the parameter handling differs significantly.