python - 如何列出discord.py 中的所有角色

标签 python discord discord.py

我需要列出所有角色以向所有角色添加权限,如何列出所有角色?

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/

相关文章:

javascript - 预期收到 SlashCommandChannelOption 构建器,但未定义

JavaScript fetch() Discord 文件上传导致 HTTP 400 "cannot send empty message, 50006"错误

node.js - Discord.js 消息附件图片

python - Discord.py 的 "on_message"可以将 't work after I implemented "schedule"写入其中

python - 将计数添加到 Plotly 箱线图中

python - Music21:从 MIDI 文件中获取每个乐器的所有音符

python - 将 PDF 转换为文本 : remove word breaks

php - 在windows下从php执行python脚本

python - discord.py 重写 : TypeError: cogs must derive from Cog

python - 如何向具有不同设置的用户发送不同的消息?不和谐.py