Norberto A.
Norberto A.

Reputation: 388

How to get user's roles, Discord, Python

I've been improving and improving my bot. Now, I've figured out how to make the bot purge messages, but a purge message, shouldn't be allowed to any member, only to people with certain role.

I've done this code:

if author.roles.has("BotModerator"):

I've looked for it, on this website(https://anidiotsguide.gitbooks.io/discord-js-bot-guide/information/understanding_roles.html), but seems that they're coding with Java, how can I do it in Python?

By the way, the error shown in the output is 'list' object has no atribute 'has'

Upvotes: 6

Views: 35838

Answers (2)

Wright
Wright

Reputation: 3424

The roles a User has is a list of role objects, you'll need to access the name property of the role. You can use list comprehension.

if "botmoderator" in [y.name.lower() for y in author.roles]:

But of course, this limits that command to only people with the specific role, BotModerators. It can't be used in another server unless they aswell have a role called BotModerator. I recommend you using the id instead. If you don't know how, tag the role then put a backslash in front of the name then send. Should show you the role's id. Then you can use that instead.

if "role_id" in [y.id for y in author.roles]:

Upvotes: 10

perfect5th
perfect5th

Reputation: 2062

It looks like author.roles is a python list. You may be able to check for the presence of your "BotModerator" role like so:

if "BotModerator" in author.roles:

Haven't tried it myself, so can't verify currently.

Alternatively, if each role is an object, you may need something like:

role_names = [role.name for role in author.roles]
if "BotModerator" in role_names

If each role is a dict, try the same as above, but with either role['name'] or role.get('name') instead of role.name.

Upvotes: 4

Related Questions