Reputation:
I need to list all roles to add to all of them a permission, how do i list all roles?
for member in server.members:
for role in member.roles:
print(role.id)
I saw this code on reddit but it dosent print anything, so how do i list all roles?
Upvotes: 2
Views: 12384
Reputation: 1
Hey still wondering on this post, how would I be able to sort that in order? Top -> Bottom of the discord roles, as for me it goes from the lowest role to the highest role, maybe i'm missing something or not understanding but would love some assistance!
After-edit:
@client.hybrid_command()
async def roles(ctx: commands.Context):
embed = discord.Embed(title=f"Roles [{len(ctx.guild.roles)}]", description="\n ".join([str(r.mention) for r in sorted(ctx.guild.roles, reverse=True)]))
await ctx.send(embed=embed)
Found out that this works well we can just reverse the ctx.guild.roles and how it posts and it's perfect, if y'all would like to use it!
Upvotes: 0
Reputation: 6944
It depends where you're doing it, but in a command it would look like:
@bot.command()
async def roles(ctx):
print(", ".join([str(r.id) for r in ctx.guild.roles]))
And if you need to get the guild (if you're using an event and the necessary obj isn't avialable), you can use:
guild = bot.get_guild(ID_HERE)
print(", ".join([str(r.id) for r in guild.roles]))
Post-edit:
@bot.command()
async def rmvroles(ctx):
role_ids = [112233445566778899, 224466881133557799 # etc...
for role in [r for r in ctx.guild.roles if r.id in role_ids]:
try:
await role.delete()
except:
await ctx.send(f"Couldn't delete {role.name} ({role.id}).")
await ctx.send("Deleted roles.")
References:
Upvotes: 3