如何使用公会。有背景任务吗?不和谐

2024-04-24 17:22:50 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用公会。有背景任务吗?discord.py重写 例如:

async def create_role():
    guild = ctx.guild
    roles = ctx.guild.roles
    if game != roles:
        color = "%06x" % random.randint(0, 0xFFFFFF)
        await guild.create_role(name="role name", colour=discord.Colour(color))

bot.loop.create_task(create_role())

Tags: namepygameasyncifdefcreaterole
1条回答
网友
1楼 · 发布于 2024-04-24 17:22:50

不太清楚您试图通过此后台任务完成什么,但这里有一种在后台任务中创建角色的方法

项目/假设:

  1. 您必须等到客户机准备就绪后,才能使用公会和角色
  2. 一般来说,后台任务在循环中运行,直到结束—我只是在其中插入了一个中断,所以它是一次性操作
  3. 不确定你们从哪里得到这个游戏,所以我只是把“test01”作为要创建的角色
  4. 我不确定你的随机着色过程,所以我只加了蓝色
  5. 你需要知道你的帮会ID才能设置帮会对象
  6. 你可以从公会获得角色列表

代码:

async def create_role():
    await client.wait_until_ready()
    while not client.is_closed():
        guild = client.get_guild(<your guild id here>)
        role_list = guild.roles
        game = 'test01'
        if game not in role_list:
            color = "%06x" % random.randint(0, 0xFFFFFF)
            await guild.create_role(name=game, colour=discord.Colour.blue())
        break

具有新角色的图像:

enter image description here

相关问题 更多 >