Bots discord.py每60秒更改一次机器人状态

Bots discord.py每60秒更改一次机器人状态,bots,discord.py,status,Bots,Discord.py,Status,我正试图制作一个每60秒更改一次机器人状态的代码,但我无法让它工作。我四处寻找如何制作,但似乎没有人尝试过这样做。或者至少我找不到这样的东西 我尝试了这个代码,但没有成功,机器人永远不会改变到第二个状态=/ # Bot joins server/auto msg @bot.event async def on_ready(): log_channel = bot.get_channel(log_id) join_embed = discord.Embed(title='Rosy

我正试图制作一个每60秒更改一次机器人状态的代码,但我无法让它工作。我四处寻找如何制作,但似乎没有人尝试过这样做。或者至少我找不到这样的东西

我尝试了这个代码,但没有成功,机器人永远不会改变到第二个状态=/

# Bot joins server/auto msg
@bot.event
async def on_ready():
    log_channel = bot.get_channel(log_id)
    join_embed = discord.Embed(title='Rosy is back online', color=0xd3d3d3)
    join_embed.set_author(name='')
    await log_channel.send(embed=join_embed)
    while True:
        await bot.change_presence(activity=discord.Game(name='?help if you are wondering anything'))  # First status
        time.sleep(60)  # Wait 60 seconds
        await bot.change_presence(activity=discord.Game(name='TEST!'))  # Show second status then repeat
我还希望第二个状态显示discord服务器中的成员总数

谢谢你的帮助

编辑:

@tasks.loop(seconds=10.0)
async def my_background_task():
    """Will loop every 60 seconds and change the bots presence"""
    await bot.change_presence(activity=discord.Game(name='?help if you are wondering anything'))
    await bot.change_presence(activity=discord.Game(name='TEST!'))


@bot.event
async def on_ready():
    # Waiting until the bot is ready
    await bot.wait_until_ready()
    # Starting the loop
    my_background_task.start()

您可以使用
tasks.loop
进行此操作,方法如下:

来自discord.ext导入任务
@tasks.loop(秒=60.0)
异步定义my_后台任务():
“”“将每60秒循环一次并更改机器人程序的存在”“”
等待机器人。更改存在状态(…)
@机器人事件
_ready()上的异步定义:
#等待机器人准备就绪
等待机器人。等待直到准备就绪()
#开始循环
我的背景任务。开始()

编辑:

回答你的评论:

total_成员=[]
对于bot.guilds中的帮会:
会员总数。扩展(公会会员)
#bot在所有公会中“服务”的会员总数,无重复
成员总数=len(集合(成员总数))
编辑2:

wait bot.change_presence(activity=discord.Game(name=f'Member count:{total_Member_count}'))

您可以使用
任务。循环
为此,以下方法:

来自discord.ext导入任务
@tasks.loop(秒=60.0)
异步定义my_后台任务():
“”“将每60秒循环一次并更改机器人程序的存在”“”
等待机器人。更改存在状态(…)
@机器人事件
_ready()上的异步定义:
#等待机器人准备就绪
等待机器人。等待直到准备就绪()
#开始循环
我的背景任务。开始()

编辑:

回答你的评论:

total_成员=[]
对于bot.guilds中的帮会:
会员总数。扩展(公会会员)
#bot在所有公会中“服务”的会员总数,无重复
成员总数=len(集合(成员总数))
编辑2:

wait bot.change_presence(activity=discord.Game(name=f'Member count:{total_Member_count}'))

我找到了这个解决方案,它工作得非常完美,它会随机改变状态

自定义机器人状态
我找到了这个解决方案,它工作得非常完美,它会随机改变状态

自定义机器人状态 @tasks.loop(秒=40)#机器人改变状态的频率,我的设置为每40秒开启一次 异步def changepresence(): 全球x

@tasks.loop(秒=40)#机器人改变状态的频率,我的设置为每40秒开启一次 异步def changepresence(): 全球x


谢谢,但我真的不明白。我把第二个状态放在哪里?在第一个下面?很抱歉,我是一个不和谐的初学者。py=/你说第一个下的
是什么意思?
?看看我上面文本中的“编辑”,它应该是这样的吗?如果你想在更改存在之间有某种延迟,添加
等待异步。睡眠(秒)
它不起作用,我收到了这个错误“`RuntimeWarning:coroutine”命令。@bot.event RuntimeWarning:Enable tracemalloc获取对象分配回溯”从未等待过\uuu call\uuuu,谢谢,但我不太明白。我把第二个状态放在哪里?在第一个下面?很抱歉,我是一个不和谐的初学者。py=/你说第一个下的
是什么意思?
?看看我上面文本中的“编辑”,它应该是这样的吗?如果你想在更改存在之间有某种延迟,添加
等待异步。睡眠(秒)
它不起作用,我收到了这个错误“``RuntimeWarning:coroutine'命令。从未等待过\uuuu调用\uuuuuu@bot.event RuntimeWarning:Enable tracemalloc以获取对象分配回溯```
@tasks.loop(seconds=40)  # How often the bot should change status, mine is set on every 40 seconds
async def changepresence():
    global x

    game = iter(
        [
            "Status 1",
            "Status 2",
            "Status 3",
            "Status 4",
            "Status 5?",
            "Status 6",
        ]
    )  # Every line above ^^ is one new status the bot can have
    for x in range(random.randint(1, 6)):  # Here you write the total of different status you have for the bot, I have 6 and that's why I have number 6 there. This makes it a 16.666% chance to change status every 40 second
        x = next(game)
    await bot.change_presence(activity=discord.Game(name=x))
game = iter(
    [
        "Status 1",
        "Status 2",
        "Status 3",
        "Status 4",
        "Status 5?",
        "Status 6",
    ]
)  # Every line above ^^ is one new status the bot can have
for x in range(random.randint(1, 6)):  # Here you write the total of different status you have for the bot, I have 6 and that's why I have number 6 there. This makes it a 16.666% chance to change status every 40 second
    x = next(game)
await bot.change_presence(activity=discord.Game(name=x))