如何在discord.py中列出所有角色 [英] How do i list all roles in discord.py

查看:60
本文介绍了如何在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)

我在reddit上看到了这段代码,但是没有打印任何内容,所以我该如何列出所有角色?

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]))

如果您需要获取公会(如果您使用的是事件,并且必需的obj无法使用),则可以使用:

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]))

后期

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


参考:

这篇关于如何在discord.py中列出所有角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆