Discord机器人程序显示错误的服务器数量(显示0)



我是机器人创建的新手,所以如果quesrion是愚蠢的

Bot在启动1个代码时显示错误的服务器数量(显示0(,但在启动2个代码时它显示正确,如何更改它将正确显示的1个代码有助于pls

status = cycle( [f' on { len(client.guilds) } servers', '~help'] )
@tasks.loop( seconds = 5 )
async def changeStatus():
await client.change_presence( activity = discord.Activity( type = discord.ActivityType.playing, name = next(status) ) )

@client.event
async def on_ready():
print( 'bot connected' )
changeStatus.start()
---------------------------------------------------------------------------------------------------------
@client.event
async def on_ready():
print( 'bot connected')
await client.change_presence( activity = discord.Game( f' on { len(client.guilds) } servers | ~help ') )

正在处理代码,但如果有人需要帮助更新,它不会更新5秒,请检查另一个我的问题

async def changeStatus():
status = cycle( [f' on { len(client.guilds) } servers', '~help'] )
await client.change_presence( activity = discord.Activity( type = discord.ActivityType.playing, name = next(status) ) )
@client.event
async def on_ready():
print( 'bot connected' )
changeStatus.start()

python解释器只能在上运行

status = cycle( [f' on { len(client.guilds) } servers', '~help'] ) 

一次,因为它被定义为全局变量。这意味着len(client.guilds)将始终是相同的数字。由于解释器在调用on_ready()之前运行,因此它的值为0。要解决此问题,请将status的声明放入changeStatus()函数中。

@tasks.loop( seconds = 5 )
async def changeStatus():
status = cycle( [f' on { len(client.guilds) } servers', '~help'] )
await client.change_presence( activity = discord.Activity( type = discord.ActivityType.playing, name = next(status) ) )

你不能这么快改变它。这被认为是API滥用,反对TOS我认为。试着做一个更合理的间隔,比如3分钟。

最新更新