简体   繁体   中英

Returning 0 instead of 2?

serverCount = str(len(client.guilds))
status = cycle([f'Verified in {serverCount} servers!', ';help'])

@tasks.loop(seconds=15)
async def change_status():
    await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name=next(status)))

@client.event
async def on_ready():
    print('Galactia is prepared for lift off!')
    change_status.start()

My variable serverCount returns 0 instead of 2 (what it is meant to return)

I will give you a better type of code for this.

Here it is:

@tasks.loop(seconds=20)
async def status_refresh():
    await phb.wait_until_ready()
    botactivity = discord.Activity(type=discord.ActivityType.playing, name=f"Verified in {len(client.guilds)} server(s)! | ;help")
    await phb.change_presence(status=discord.Status.dnd, activity=botactivity)
status_refresh.start()

Explanation:

  • phb.wait_until_ready() for the loop to start only when the bot is online.

That's actually all you need as you know all other stuff.


This will work the way you want.

Thanks:D

To get the number of servers your bot is in, you need to do a loop through all servers it is in. So I would do something like

servers = for guild in client.guilds
s = len(servers)

Then you can apply this to your status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM