polandball
polandball

Reputation: 21

Discord.py - List the user's roles

I want my bot to show me the list of the users roles. If I use user.roles, I get this problem:

enter image description here

Is there a way to let the bot list the roles clean, just like this bot does?

enter image description here

Upvotes: 1

Views: 3174

Answers (1)

Diggy.
Diggy.

Reputation: 6944

You're having a list of roles returned. This allows you to iterate over it, picking each individual attribute of a role.

For example, the attribute in the second image you've chosen was .mention, and the general idea of the code looks like this (adapt it for your own command):

@bot.command()
async def roles(...):
    rolelist = [r.mention for r in user.roles if r != ctx.guild.default_role]
    roles = ", ".join(rolelist)

The list comprehension is getting every role that the user has, excluding @everyone.

roles is the variable that contains the "beautified" list of roles. And to get the number of roles, just do len(rolelist).


References:

Upvotes: 4

Related Questions