From 468605fc4b73e8cc82ed81c3f2d7a13cbbb9ff1e Mon Sep 17 00:00:00 2001 From: rtk-rnjn Date: Tue, 23 Jul 2024 12:16:44 +0530 Subject: [PATCH] Refactor code to handle message events more efficiently --- events/on_msg.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/events/on_msg.py b/events/on_msg.py index 74b67166..340c0a2e 100644 --- a/events/on_msg.py +++ b/events/on_msg.py @@ -471,6 +471,9 @@ async def _global_chat_handler(self, message: discord.Message) -> None: if self.is_banned(message.author): return + if TYPE_CHECKING: + assert message.guild is not None + if message.guild.id not in self.__global_chat_cache: # LRU? data = await self.bot.guild_configurations.find_one( @@ -584,6 +587,10 @@ async def _scam_detection(self, message: discord.Message, *, to_send: bool = Tru if message.author.id == self.bot.user.id: return False + flags = message.author.public_flags + if any((flags.staff, flags.system, flags.verified_bot)): + return False + if not message.channel.permissions_for(message.guild.me).send_messages: return @@ -621,6 +628,9 @@ async def _scam_detection(self, message: discord.Message, *, to_send: bool = Tru aiohttp.ServerDisconnectedError, aiohttp.ClientResponseError, ): + if self.bot.http_session.closed: # RuntimeError: Session is closed + return + response = await self.bot.http_session.post( API, json={"message": message.content},