简体   繁体   English

ServerCount 变量返回 0 而不是 2

[英]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. serverCount 旨在返回 2,因为机器人位于 2 个服务器中,但它返回 0 或空列表。 It works completely fine with no errors but it still returns 0它工作得很好,没有错误,但它仍然返回 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的值。因此len(client.guilds)的值为 0。要解决此问题,您需要在on_ready事件中设置该值。

serverCount = ""

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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