简体   繁体   中英

discord.py autorefresh an embed

Hey I'm making a multipurpose discord bot and i have cog that pings a minecraft server and gets info and id like to make it so that it updates every minute or so.

code : https://haste.discordbots.mundane.nz/dudosavipa.py

this code edits it once with values change but after that it edits with no value change Any help is appreciated

You can use discord.ext.tasks to make this in a better way.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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