【问题标题】:Discord.py - changing statusDiscord.py - 改变状态
【发布时间】:2021-01-19 02:49:20
【问题描述】:

我希望我的机器人每 5 秒更改一次状态。当我运行代码时,它不会显示任何错误。我不知道出了什么问题,因为我对另一个机器人使用了相同的状态更改代码。 我有这个代码:

import discord
from discord.ext import commands, tasks
from itertools import cycle

client = commands.Bot(command_prefix="?")
status = cycle(["status1", "status2"])

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

@client.event
async def on_ready():
    notificationChannel = client.get_channel(channel_id)
    await notificationChannel.send("Bot booted up!")

client.run("token")

提前致谢。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    你忘记开始任务了。您不应该每 5 秒执行一次,因为它可能会限制您的速度。就个人而言,我每 5 分钟更换一次。

    import discord
    from discord.ext import commands, tasks
    from itertools import cycle
    
    client = commands.Bot(command_prefix="?")
    status = cycle(["status1", "status2"])
    
    @tasks.loop(seconds=5)
    async def changeStatus():
        await client.change_presence(status=discord.Status.do_not_disturb, activity=discord.Activity(type=discord.ActivityType.playing, name=next(status)))
    
    @client.event
    async def on_ready():
        notificationChannel = client.get_channel(channel_id)
        await notificationChannel.send("Bot booted up!")
        changeStatus.start()
    
    client.run("token")
    

    【讨论】:

    • 谢谢,帮了大忙。
    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2021-03-01
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    • 2021-03-14
    相关资源
    最近更新 更多