【问题标题】:discord.py autorefresh an embeddiscord.py 自动刷新嵌入
【发布时间】:2021-04-24 07:31:31
【问题描述】:

嘿,我正在制作一个多用途的不和谐机器人,我有一个 cog 可以 ping 一个我的世界服务器并获取信息和 id,以便它每分钟左右更新一次。

代码https://haste.discordbots.mundane.nz/dudosavipa.py

此代码在值更改时对其进行了一次编辑,但之后它在没有值更改的情况下进行编辑,感谢任何帮助

【问题讨论】:

  • 尝试在 true 时删除第二个并将 time.sleep 更改为 await asyncio.sleep

标签: python python-3.x discord discord.py minecraft


【解决方案1】:

您可以使用discord.ext.tasks 以更好的方式完成此操作。

import discord
from discord.ext import commands, tasks

class Mcstats(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    
    @commands.Cog.listener()
    async def on_ready(self):
        self.channel = self.bot.get_channel(channel_id_of_the_message)
        self.message = await self.channel.fetch_message(message_id)
        self.task.start()
    
    @tasks.loop(minutes=1)
    async def task(self):
        #retrieving data and creating embed
        await self.message.edit(embed=your_embed) #editing message

【讨论】:

    猜你喜欢
    • 2020-01-22
    • 2019-03-14
    • 2016-07-26
    • 1970-01-01
    • 2022-01-01
    • 2020-12-09
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多