简体   繁体   中英

How do i list all roles in discord.py

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?

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM