简体   繁体   中英

ServerCount variable returning 0 instead of 2

intents = discord.Intents(messages = True, guilds = True, reactions = True, members = True, presences = True)
client = commands.Bot(command_prefix = get_prefix, intents = intents)
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()

serverCount is meant to return 2 because the bot is in 2 servers but instead it returns 0 or an empty list. It works completely fine with no errors but it still returns 0

Your problem is that you're setting the value of serverCount before the bot has actually logged in. Therefore the value of len(client.guilds) is 0. To fix this, you'll need to set the value in the on_ready event.

serverCount = ""

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

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