【问题标题】:Cannot get role to change colours无法获得更改颜色的角色
【发布时间】:2018-07-10 06:57:13
【问题描述】:

我有一段代码,它接受一个指定的角色和角色服务器并将其放入一个列表中,列表中的任何角色都应该改变颜色,但是......它不会。

代码:

@client.command(pass_context=True,description="Linusifies a role")
@has_permissions(administrator = True)
async def rgb(ctx, role : discord.Role):
    server = ctx.message.author.server
    if (("{}, {}").format(server, role)) in rgb_list:
        msg = "{}'s rgb is turned off :disappointed_relieved:".format(role)
        rgb_list.remove(("{}, {}").format(server, role))
        await client.send_message(ctx.message.channel, msg)
        print (server)

    else:
        msg = "{}'s rgb has been turned on :christmas_tree:".format(role)
        rgb_list.append(("{}, {}").format(server, role))
        await client.send_message(ctx.message.channel, msg)
        print (server)

在我的@client.event 中:

   async def rgb_():
    await client.wait_until_ready()
    while not client.is_closed:
        print (rgb_list)
        await client.edit_role(rgb_list,colour=discord.Colour(0x1abc9c))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0x11806a))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0x2ecc71))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0x1f8b4c))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0x3498db))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0x206694))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0x9b59b6))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0x71368a))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0xe91e63))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0xad1457))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0xf1c40f))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0xc27c0e))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0xe67e22))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0xa84300))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0xe74c3c))
        await asyncio.sleep(2)
        await client.edit_role(rgb_list,colour=discord.Colour(0x992d22))
        await asyncio.sleep(2)

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    阅读Client.edit_role 的文档。 edit_role 采用 ServerRole,而不是字符串列表。您应该将您的角色列表存储为listRoles。然后你可以遍历那个 lsit 来改变颜色。

    @client.command(pass_context=True,description="Linusifies a role")
    @has_permissions(administrator = True)
    async def rgb(ctx, role : discord.Role):
        server = ctx.message.author.server
        if role in rgb_list:
            msg = "{}'s rgb is turned off :disappointed_relieved:".format(role.name)
            rgb_list.remove(role)
            await client.send_message(ctx.message.channel, msg)
            print (server)
    
        else:
            msg = "{}'s rgb has been turned on :christmas_tree:".format(role.name)
            rgb_list.append(role)
            await client.send_message(ctx.message.channel, msg)
            print (server)
    
    colours = [discord.Colour(0x1abc9c), discord.Colour(0x11806a)]
    
    @client.event
    async def on_ready():
        while not client.is_closed:
            for colour in colours:  # make a list of Colours
                for role in rgb_list:
                    await client.edit_role(role.server, role, colour=colour)
                await asyncio.sleep(2)
    

    【讨论】:

    • 喜欢这个? async def rgb_(): await client.wait_until_ready() while not client.is_closed: for colour in colours: [discord.Colour(0x1abc9c), discord.Colour(0x11806a), discord.Colour(0x2ecc71)] for role in rgb_list: await client.edit_role(role.server, role, colour=colour) await asyncio.sleep(2)
    • 代码应该按原样工作。我不想制作完整的 colours 列表,因此您必须将其扩展为您想要的所有颜色。
    • 还是不变色
    • 您的机器人无权管理目标服务器上的角色。这不是你可以编码的东西,你必须给你的机器人这些权限。
    • 是的,现在修复了,但它仍然不想改变颜色(在你给我的两个之间交替)
    猜你喜欢
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2022-01-25
    • 2017-08-24
    • 2021-06-21
    • 1970-01-01
    相关资源
    最近更新 更多