1

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.

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
crodev
  • 1,323
  • 6
  • 22
  • 39
  • 2
    `ctx.message.author` doesn't contain the name, I believe you're looking for `ctx.message.author.name` (see https://discordpy.readthedocs.io/en/rewrite/api.html?highlight=author#discord.Member). Though you're better off using the ID as MCO answered. – flau Sep 06 '18 at 15:07

4 Answers4

7

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

if author.id == 170733454822341405:
   #do something
MCO
  • 1,187
  • 1
  • 11
  • 20
5

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

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
2

It appears to me that you are attempting to compare the instance of an "author" to the name and discriminator. What you need to do is either: 1) find the member with name "foo#3465" and compare THAT to the ctx.message.author or "messageAuthor"

member = discord.utils.get(message.guild.members, name='Foo')
if member == messageAuthor:
   #do something
else:
   #do something else

or alternatively you could: 2) compare messageAuthor.name to "foo" or str(messageAuthor) to "foo#3465"

Thunderwood
  • 525
  • 2
  • 9
1

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.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
apc518
  • 193
  • 1
  • 4
  • 12