我需要列出所有角色以向所有角色添加权限,如何列出所有角色?
for member in server.members:
for role in member.roles:
print(role.id)
我在 reddit 上看到了这段代码,但它没有打印任何内容,那么我如何列出所有角色?
最佳答案
这取决于您在哪里执行此操作,但在命令中它看起来像:
@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]))
后期编辑:
@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.")
引用文献:
关于python - 如何列出discord.py 中的所有角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62246057/