首页 > 解决方案 > Discord.py - 从服务器复制角色(名称和颜色)

问题描述

我正在尝试创建服务器的副本,就像我在服务器中 > 复制一样,它将创建一个具有相同名称但添加“备份”的新服务器,然后它复制所有通道、角色和权限。

我设法使复制渠道(和类别)工作,如果可以的话,我计划稍后再做。目前,我正在尝试复制角色(名称和颜色)

@SelfBot.command()
async def copy(ctx): # b'\xfc'
    await ctx.message.delete()
    await SelgBot.create_guild(f'backup-{ctx.guild.name}')
    await asyncio.sleep(4)
    for g in SelfBot.guilds:
        if f'backup-{ctx.guild.name}' in g.name:
            for c in g.channels:
                await c.delete()
            for cate in ctx.guild.categories:
                x = await g.create_category(f"{cate.name}")
                for chann in cate.channels:
                    if isinstance(chann, discord.VoiceChannel):
                        await x.create_voice_channel(f"{chann}")
                    if isinstance(chann, discord.TextChannel):
                        await x.create_text_channel(f"{chann}")
            print(ctx.guild.roles)
            for role in ctx.guild.roles:
                await g.create_role(name='role', color=RandomColor())

它目前创建具有随机颜色和名称的角色role,有没有人做过类似的事情,可以告诉我如何获取原始服务器每个角色的名称和颜色?

标签: pythonpython-3.xdiscorddiscord.pydiscord.py-rewrite

解决方案


for role in ctx.guild.roles:
                name = role.name
                print(name)
                color = role.colour
                print(color)
                perms = role.permissions
                print(perms)
                await g.create_role(name=name, permissions=perms, colour=color)

您可以通过role.name/获得名称和颜色role.colour:) 希望这会有所帮助


推荐阅读