如何使用公会。有后台任务? discord.py
How to use guild. with background tasks? discord.py
如何使用公会。有后台任务? 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())
不确定您要通过此后台任务完成什么,但这里有一种在后台任务中创建角色的方法。
Items/assumptions:
- 您必须等到客户端准备就绪,公会和角色才可用。
- 一般来说,后台任务是 运行 循环直到关闭 - 我只是在其中插入了一个中断,所以它是一次性操作。
- 不确定您从哪里获得游戏,所以我只是输入 "test01" 作为要创建的角色。
- 不确定你的随机颜色过程,所以我只放了蓝色。
- 您需要知道您的公会ID才能设置公会对象
- 你得到公会的角色列表
代码:
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
具有新角色的图像:
如何使用公会。有后台任务? 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())
不确定您要通过此后台任务完成什么,但这里有一种在后台任务中创建角色的方法。
Items/assumptions:
- 您必须等到客户端准备就绪,公会和角色才可用。
- 一般来说,后台任务是 运行 循环直到关闭 - 我只是在其中插入了一个中断,所以它是一次性操作。
- 不确定您从哪里获得游戏,所以我只是输入 "test01" 作为要创建的角色。
- 不确定你的随机颜色过程,所以我只放了蓝色。
- 您需要知道您的公会ID才能设置公会对象
- 你得到公会的角色列表
代码:
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
具有新角色的图像: