0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

discord.pyにおける更新型メッセージ

Posted at

botでテキストを出力する時、前回の投稿を削除して新しくポストすると、
最新の投稿メッセージと表示されるので便利です。

ただ、投稿が短時間で行われると、二重に投稿が表示されたりします。

基本的にマルチスレッドにおけるロッキング処理ですが、
今のところうまく行っているのでソースを置いておきます。

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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?