簡體   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 旨在返回 2,因為機器人位於 2 個服務器中,但它返回 0 或空列表。 它工作得很好,沒有錯誤,但它仍然返回 0

您的問題是您在機器人實際登錄之前設置了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