Discord bot 不工作且不改变状态

时间:2021-05-08 12:42:28

标签: python discord discord.py

起初我的 discord bot 不起作用,在这段代码中是错误的,我找不到它,其次是它没有改变状态,当你只输入 2 个文本时它正在改变但是当试图把client.guilds不要

@tasks.loop( seconds = 12 )
async def changeStatus():
    iterationPosition = 0
    status = [f' on { len(client.guilds) } servers', '~help']
    await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name=status[iterationPosition]))
    iterationPosition = 0 if (iterationPosition is len(status) - 1) else (iterationPosition + 1)

@client.event
async def on_ready():
    print( 'bot connected' )
    changeStatus.start()

*工作代码,但问题是他给我写的命令回答次数与他使用的所有服务器一样多

tasks.iterationPosition = 0

@tasks.loop( seconds = 5 )
async def changeStatus():
    status = [f" on { len(client.guilds) } servers", "~help"]
    await client.change_presence(
        activity=discord.Activity(
            type=discord.ActivityType.playing, name=status[tasks.iterationPosition]
        )
    )
    tasks.iterationPosition = (
        0
        if (tasks.iterationPosition is len(status) - 1)
        else (tasks.iterationPosition + 1)
    )

@client.event
async def on_ready():
    print( 'bot connected' )
    changeStatus.start()

1 个答案:

答案 0 :(得分:1)

在您的代码中,您总是在循环开始时将 0 分配给 iterationPosition,而不是像这样将其放在“全局”范围内:

tasks.iterationPosition = 0

@tasks.loop(seconds=12)
async def changeStatus():
    status = [f" on { len(client.guilds) } servers", "~help"]
    await client.change_presence(
        activity=discord.Activity(
            type=discord.ActivityType.playing, name=status[tasks.iterationPosition]
        )
    )
    tasks.iterationPosition = (
        0
        if (tasks.iterationPosition is len(status) - 1)
        else (tasks.iterationPosition + 1)
    )