【问题标题】:discord.py change bot status every 60 secdiscord.py 每 60 秒更改一次机器人状态
【发布时间】:2020-12-02 13:50:33
【问题描述】:

我正在尝试编写一个每 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

我还希望第二个状态显示不和谐服务器中的总成员...

感谢您的帮助!

编辑:

@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()

【问题讨论】:

    标签: bots discord.py status


    【解决方案1】:

    您可以为此使用tasks.loop,方法如下:

    from discord.ext import tasks
    
    @tasks.loop(seconds=60.0)
    async def my_background_task():
        """Will loop every 60 seconds and change the bots presence"""
        await bot.change_presence(...)
    
    
    @bot.event
    async def on_ready():
        # Waiting until the bot is ready
        await bot.wait_until_ready()
        # Starting the loop
        my_background_task.start()
    

    docs

    编辑:

    回答您的评论:

    total_members = []
    
    for guild in bot.guilds:
        total_members.expand(guild.members)
    
    # Number of total members the bot is `serving` in all guilds, without duplicates
    total_members_count = len(set(total_members))
    

    EDIT2:

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

    【讨论】:

    • 谢谢,但我不太明白。我在哪里放置第二个状态?在第一个下?不好意思问,我是 discord.py 的初学者 =/
    • under the first one 是什么意思?
    • 看我上面文字中的“EDIT”,应该是这样的吗?
    • 如果您想在更改存在之间有某种延迟,请添加 await asyncio.sleep(seconds)
    • 它不起作用,我得到了这个错误``` RuntimeWarning: coroutine 'Command.__call__' is never awaited @bot.event RuntimeWarning: E​​nable tracemalloc to get the object allocation traceback ```
    【解决方案2】:

    我找到了这个解决方案,它运行良好,它会随机改变状态。

    自定义机器人状态

    @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))
    

    【讨论】:

      【解决方案3】:

      @tasks.loop(seconds=40) # 机器人应该多久改变一次状态,我的是每 40 秒设置一次 异步定义更改存在(): 全局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))
      

      【讨论】:

        猜你喜欢
        • 2019-05-26
        • 2021-01-13
        • 2018-12-13
        • 2018-02-26
        • 2021-06-10
        • 2012-01-09
        • 2021-12-06
        • 1970-01-01
        • 2020-12-21
        相关资源
        最近更新 更多