简体   繁体   English

Discord.py | 每 60 秒发送一个嵌入文件并对其进行编辑

[英]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.我想创建一个 discord 机器人,它从 API 检索数据,然后将数据作为嵌入不和谐的消息发送。 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.但是,API 上的信息发生了变化,我希望嵌入的消息通过每 60 秒更新一次消息内容来保持最新。

The big problem is that I have other commands and the bot might send multiple messages within the 60-second period.最大的问题是我有其他命令,机器人可能会在 60 秒内发送多条消息。 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. *注意:我不介意使用 webhook 或类似的东西。 I just want the job to be done.我只想完成工作。

Yes.是的。 Use tasks.loop .使用tasks.loop To start the loop, use start() .要开始循环,请使用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):
    send_message_loop.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM