如何在discord.py中列出所有角色?

问题描述 投票:0回答:1

我需要列出所有的角色,为他们添加一个权限,我如何列出所有的角色?

for member in server.members:
    for role in member.roles:
        print(role.id)

我在reddit上看到这段代码,但它没有打印任何东西,那么我如何列出所有的角色?

python discord discord.py
1个回答
1
投票

这取决于你在哪里做的,但是在命令中会是这样的。

@bot.command()
async def roles(ctx):
    print(", ".join([str(r.id) for r in ctx.guild.roles]))

如果你需要获取公会的信息(如果你使用的是事件,而必要的obj又无法获取),你可以使用... ..:

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.")

引用:

© www.soinside.com 2019 - 2024. All rights reserved.