crodev
crodev

Reputation: 1491

How to check if message was sent by certain user discord.py

I am using discord.py to make my discord bot and when somebody types a message I want to check if the user is lets say foo#3645 and then do something if it is not then do something else

if(messageAuthor == "foo#3645):
    # do something
else:
    # do something else

I tried this:

if(ctx.message.author == "foo#3465"):
    # do something

but that is not working for some reason...

If you need more info please comment.

Upvotes: 1

Views: 30754

Answers (3)

MCO
MCO

Reputation: 1257

if you want a reliable check, check for user ids:

if author.id == 170733454822341405:
   #do something

Upvotes: 7

apc518
apc518

Reputation: 303

Checking for a user ID is your best bet. According to the docs, User.id is an integer, not a string, so the accepted answer may not work. message.author is of type User.

This is what worked for me:

if message.author.id = 170733454822341405:
    #do something

To get someone's user ID manually, enable developer mode in discord (user settings -> appearance -> advanced), then just right click on a user anywhere and click copy ID.

Upvotes: 1

Patrick Haugh
Patrick Haugh

Reputation: 60984

The str representation of a User (including Members) will be their username (not their server-specific nickname), and the discriminator (used to tell people with the same username apart).

if str(ctx.message.author) == "foo#3465":
    ...

That said, you should be checking against ids, as it is possible for a person to change their Discord username. ids are strings in the async branch of and integers in

Upvotes: 5

Related Questions