[PYTHON] Updated messages in discord.py

When outputting text with bot, if you delete the previous post and post a new one, It is convenient because it is displayed as the latest posted message.

However, if the post is made in a short time, the post may be displayed twice.

It's basically a multi-threaded locking process, It's working fine so far, so I'll leave the source.

class RenewalMessage():
    def __init__(self, channel):
        self.lastmessage = None
        self.outputlock = 0
        self.channel = channel

    async def SendMessage(self, message):
        if self.outputlock == 1: return
        try:
            while self.outputlock != 0:
                await asyncio.sleep(1)

            if self.lastmessage is not None:
                self.outputlock = 1
                try:
                    await self.lastmessage.delete()
                except discord.errors.NotFound:
                    pass
                self.lastmessage = None

            try:
                self.outputlock = 2
                self.lastmessage = await self.channel.send(message)
            except discord.errors.Forbidden:
                pass
        finally:
            self.outputlock = 0

Recommended Posts

Updated messages in discord.py
nest cog in discord.py
Pass arguments to Task in discord.py
Understand Cog and Extension in discord.py