我尝试使用这段代码:
status = cycle(['.apuva', f'{str(len(client.guilds))} palvelimella!']
@client.event
async def on_ready():
change_status.start()
print('Botti on käynnissä.')
async def change_status():
await client.change_presence(activity=discord.Game(next(status)))
但是,即使我的机器人在 3 个服务器上,这也显示状态为“0 个服务器”。如何在discord.py的重写版本中解决这个问题?
答案 0 :(得分:1)
我是这样写的,所以机器人的状态会变成“正在玩x台服务器!”:
@client.event
async def on_ready():
print('Bot is ready.')
await client.change_presence(activity=discord.Game(name=f"{len(client.guilds)} servers!"))
答案 1 :(得分:1)
之所以说 0 个服务器是因为 f 字符串在您声明它们的同一行上进行评估。由于机器人尚未运行,len(client.guilds)
在您的字符串中计算为 0,因此它与分配 status = cycle(['.apuva', '0 palvelimella!'])
相同。
您可以在字符串中保留占位符,而不是使用 f 字符串,然后在循环中时对其进行格式化。
status = itertools.cycle(['In {n} servers', ...])
...
@tasks.loop(minutes=5)
async def change_status():
name = next(status)
name = name.format(n=len(client.guilds))
# Note that if the placeholder '{n}' does not exist in the string,
# no error will occur and the string remains the same
await client.change_presence(...)
答案 2 :(得分:0)
import asyncio
@client.event
async def on_ready():
print("Bot is ready!")
while not client.is_closed():
await client.change_presence(activity=discord.Game(name=f"{len(client.guilds)} servers!")
await asyncio.sleep(10)
await client.change_presence(activity=discord.Game(name="Status 2")
await asyncio.sleep(10)