diff options
author | 2022-04-21 16:56:18 +0300 | |
---|---|---|
committer | 2022-04-21 16:56:18 +0300 | |
commit | 138a39c6d8c8324f5dbd0b2cf1670ccb5161db12 (patch) | |
tree | ddea5a65a237fc19572cca5bd16588f895e27572 | |
parent | Merge pull request #2136 from python-discord/fix-modpings (diff) |
Make mod_log getter asyncmbaruh/modlog_fix
Loading order is not guaranteed. Sleep until the cog loads.
-rw-r--r-- | bot/exts/moderation/defcon.py | 33 |
1 files changed, 10 insertions, 23 deletions
diff --git a/bot/exts/moderation/defcon.py b/bot/exts/moderation/defcon.py index d092af6e9..1df79149d 100644 --- a/bot/exts/moderation/defcon.py +++ b/bot/exts/moderation/defcon.py @@ -71,10 +71,11 @@ class Defcon(Cog): scheduling.create_task(self._sync_settings(), event_loop=self.bot.loop) - @property - def mod_log(self) -> ModLog: + async def get_mod_log(self) -> ModLog: """Get currently loaded ModLog cog instance.""" - return self.bot.get_cog("ModLog") + while not (cog := self.bot.get_cog("ModLog")): + await asyncio.sleep(1) + return cog @defcon_settings.atomic_transaction async def _sync_settings(self) -> None: @@ -82,20 +83,6 @@ class Defcon(Cog): log.trace("Waiting for the guild to become available before syncing.") await self.bot.wait_until_guild_available() - # Load order isn't guaranteed, attempt to check mod log load status 3 times before erroring. - for _ in range(3): - if self.mod_log: - break - else: - await asyncio.sleep(5) - else: - log.exception("Modlog cog not loaded, aborting sync.") - await self.channel.send( - f"<@&{Roles.moderators}> <@&{Roles.devops}> **WARNING**: Unable to get DEFCON settings!" - f"\n\nmod log cog could not be found after 3 tries." - ) - return - self.channel = await self.bot.fetch_channel(Channels.defcon) log.trace("Syncing settings.") @@ -118,7 +105,7 @@ class Defcon(Cog): self._update_notifier() log.info(f"DEFCON synchronized: {time.humanize_delta(self.threshold) if self.threshold else '-'}") - self._update_channel_topic() + await self._update_channel_topic() @Cog.listener() async def on_member_join(self, member: Member) -> None: @@ -150,7 +137,7 @@ class Defcon(Cog): if not message_sent: message = f"{message}\n\nUnable to send rejection message via DM; they probably have DMs disabled." - await self.mod_log.send_log_message( + await (await self.get_mod_log()).send_log_message( Icons.defcon_denied, Colours.soft_red, "Entry denied", message, member.display_avatar.url ) @@ -226,12 +213,12 @@ class Defcon(Cog): await role.edit(reason="DEFCON unshutdown", permissions=permissions) await ctx.send(f"{Action.SERVER_OPEN.value.emoji} Server reopened.") - def _update_channel_topic(self) -> None: + async def _update_channel_topic(self) -> None: """Update the #defcon channel topic with the current DEFCON status.""" threshold = time.humanize_delta(self.threshold) if self.threshold else '-' new_topic = f"{BASE_CHANNEL_TOPIC}\n(Threshold: {threshold})" - self.mod_log.ignore(Event.guild_channel_update, Channels.defcon) + (await self.get_mod_log()).ignore(Event.guild_channel_update, Channels.defcon) scheduling.create_task(self.channel.edit(topic=new_topic)) @defcon_settings.atomic_transaction @@ -290,7 +277,7 @@ class Defcon(Cog): await channel.send(message) await self._send_defcon_log(action, author) - self._update_channel_topic() + await self._update_channel_topic() self._log_threshold_stat(threshold) @@ -318,7 +305,7 @@ class Defcon(Cog): ) status_msg = f"DEFCON {action.name.lower()}" - await self.mod_log.send_log_message(info.icon, info.color, status_msg, log_msg) + await (await self.get_mod_log()).send_log_message(info.icon, info.color, status_msg, log_msg) def _update_notifier(self) -> None: """Start or stop the notifier according to the DEFCON status.""" |