diff options
| -rw-r--r-- | bot/exts/moderation/defcon.py | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/bot/exts/moderation/defcon.py b/bot/exts/moderation/defcon.py index 4b25c36df..a180d7aae 100644 --- a/bot/exts/moderation/defcon.py +++ b/bot/exts/moderation/defcon.py @@ -44,13 +44,11 @@ class Action(Enum): class Defcon(Cog): """Time-sensitive server defense mechanisms.""" - days = None # type: timedelta - enabled = False # type: bool - def __init__(self, bot: Bot): self.bot = bot self.channel = None self.days = timedelta(days=0) + self.enabled = False self.bot.loop.create_task(self.sync_settings()) @@ -61,7 +59,10 @@ class Defcon(Cog): async def sync_settings(self) -> None: """On cog load, try to synchronize DEFCON settings to the API.""" + log.trace("Waiting for the guild to become available before syncing.") await self.bot.wait_until_guild_available() + + log.trace("Syncing settings.") self.channel = await self.bot.fetch_channel(Channels.defcon) try: @@ -142,6 +143,9 @@ class Defcon(Cog): except Exception: pass + self.days = timedelta(days=days) + self.enabled = action != Action.DISABLED + error = None try: await self.bot.api_client.put( @@ -150,22 +154,22 @@ class Defcon(Cog): 'name': 'defcon', 'data': { # TODO: retrieve old days count - 'days': days, - 'enabled': action is not Action.DISABLED, + 'days': self.days.days, + 'enabled': self.enabled, 'enable_date': datetime.now().isoformat() } } ) - self.days = timedelta(days=days) - self.update_notifier() - except Exception as err: log.exception("Unable to update DEFCON settings.") error = err finally: + self.update_notifier() + await ctx.send(self.build_defcon_msg(action, error)) await self.send_defcon_log(action, ctx.author, error) + await self.update_channel_topic() self.bot.stats.gauge("defcon.threshold", days) @@ -178,17 +182,13 @@ class Defcon(Cog): Currently, this just adds an account age requirement. Use !defcon days <int> to set how old an account must be, in days. """ - self.enabled = True await self._defcon_action(ctx, days=0, action=Action.ENABLED) - await self.update_channel_topic() @defcon_group.command(name='disable', aliases=('off', 'd'), root_aliases=("defoff",)) @has_any_role(*MODERATION_ROLES) async def disable_command(self, ctx: Context) -> None: """Disable DEFCON mode. Useful in a pinch, but be sure you know what you're doing!""" - self.enabled = False await self._defcon_action(ctx, days=0, action=Action.DISABLED) - await self.update_channel_topic() @defcon_group.command(name='status', aliases=('s',)) @has_any_role(*MODERATION_ROLES) @@ -206,9 +206,7 @@ class Defcon(Cog): @has_any_role(*MODERATION_ROLES) async def days_command(self, ctx: Context, days: int) -> None: """Set how old an account must be to join the server, in days, with DEFCON mode enabled.""" - self.enabled = True await self._defcon_action(ctx, days=days, action=Action.UPDATED) - await self.update_channel_topic() async def update_channel_topic(self) -> None: """Update the #defcon channel topic with the current DEFCON status.""" @@ -273,6 +271,11 @@ class Defcon(Cog): """Routinely notify moderators that DEFCON is active.""" await self.channel.send(f"Defcon is on and is set to {self.days.days} day{ngettext('', 's', self.days.days)}.") + def cog_unload(self) -> None: + """Cancel the notifer task when the cog unloads.""" + log.trace("Cog unload: canceling defcon notifier task.") + self.defcon_notifier.cancel() + def setup(bot: Bot) -> None: """Load the Defcon cog.""" |