【问题标题】:editing roles with edit_role discord.py使用 edit_role discord.py 编辑角色
【发布时间】:2020-08-06 06:31:49
【问题描述】:

我的脚本中有以下行:

await client.edit_role(server='547874634978789398', role='' ,colour=0x008000)    

但是我不明白 discord.py 对 role= 的期望是什么参数。谁能指出我正确的方向以更好地理解这个参数?

【问题讨论】:

  • Read the documentationrole 是您要编辑的角色
  • @PatrickHaugh 你能给我一个例子吗?
  • 您对要编辑的角色了解多少?姓名、身份证等?
  • @PatrickHaugh 我拥有所有这些,我不明白该进入哪个角色=''

标签: python-3.x discord.py


【解决方案1】:

首先,您应该使用Server 类而不是服务器的ID。有 get_server() 函数返回具有给定服务器 ID 的 Server 对象。

server = client.get_server('547874634978789398')

然后,您可以通过server.roles 访问属于服务器的所有角色。是Role 对象的list。因此,如果您有角色名称并想将角色编辑到该名称中,请尝试。

for role in server.roles:
    if role.name == 'role_name':
        # What you want to do.
        await client.edit_role(server=server, role=role, colour=0x0080000)
        break

还有server.role_hierarchy 属性,它按层次结构的顺序返回角色。它包含与server.roles 相同的元素,但它是排序后的版本。

【讨论】:

    【解决方案2】:
    role = discord.utils.get(ctx.guild.roles, name="Name")
    # This will get the role you want to edit
    await role.edit(color=0x008000, reason="The reason")
    # This will edit the role with the desired color
    

    有关这方面的更多信息,请参阅文档:discord.Role.edit | discord.utils.get

    【讨论】:

    • 这是更新的重写,只是想我会发送更新,因为我也在寻找答案。
    猜你喜欢
    • 2021-04-15
    • 2021-06-22
    • 2021-09-30
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2014-07-14
    • 2021-05-05
    • 1970-01-01
    相关资源
    最近更新 更多