Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm new at bot creating so sry if quesrion is stupid

Bot showing incorrect number of servers(showing 0) when starting 1 code but when starting 2 code it shows correctly, how to change 1 code that it will show correctly help 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 ') )

Workin code but it isn't updating 5 sec if someone need help with updating check another my questions

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
168 views
Welcome To Ask or Share your Answers For Others

1 Answer

The python interpreter will only run over

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

once, as it is defined as a global variable. This means that len(client.guilds) will always be the same number. And since the interpreter runs over this, before on_ready() was called, the value of this is 0. To fix this, put the declaration of status into your changeStatus() function.

@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) ) )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...