Search code examples
pythondiscorddiscord.pybots

Discord.py | Send an embed and edit it every 60 seconds


I want to create a discord bot that retrieves data from an API and then sends the data as a discord-embedded message. However, the information on the API changes and I want the embedded message to stay up to date by updating the message contents every 60 seconds.

The big problem is that I have other commands and the bot might send multiple messages within the 60-second period. How do I keep track of that message and edit it when needed? Can this be done by a single background task?

*Note: I don't mind using webhooks or something of the sort. I just want the job to be done.


Solution

  • Yes. Use tasks.loop. To start the loop, use start(). I do this with a command.

    @tasks.loop(minutes=1)
    async def send_message_loop(msg):
        await msg.edit(content="World!")
    
    @bot.command()
    async def start(ctx):
        msg = await ctx.guild.fetch_message(ID)
        send_message_loop.start(msg)
    

    Edit

    As @TheFungusAmongUs mentioned, better to pass the message as an arg instead of keeping track.