aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar kwzrd <[email protected]>2020-03-22 15:52:32 +0100
committerGravatar kwzrd <[email protected]>2020-03-22 15:52:32 +0100
commit1b9437101bb6be9727fdda6d09521e2e810dc101 (patch)
treebc8c8b23824a0428c927778cd5357f8dff7f6d93
parentDeseasonify: return timedelta rather than raw seconds (diff)
Deseasonify: add daemon command group
This allows manually starting and stopping the daemon if necessary. Enables introspection in the sense that we can check whether the daemon is currently running, and for when is the next refresh scheduled. In the case that the daemon dies due to an unhandled exception, the start command is able to revive it. The daemon currently does not alert us when it dies.
-rw-r--r--bot/branding.py40
1 files changed, 39 insertions, 1 deletions
diff --git a/bot/branding.py b/bot/branding.py
index eb360f5f..4204bcd6 100644
--- a/bot/branding.py
+++ b/bot/branding.py
@@ -5,12 +5,13 @@ import random
import typing as t
from datetime import datetime, time, timedelta
+import arrow
import discord
from discord.embeds import EmptyEmbed
from discord.ext import commands
from bot.bot import SeasonalBot
-from bot.constants import Branding, MODERATION_ROLES, Tokens
+from bot.constants import Branding, Colours, Emojis, MODERATION_ROLES, Tokens
from bot.decorators import with_role
from bot.seasons import SeasonBase, get_current_season, get_season
@@ -348,6 +349,43 @@ class BrandingManager(commands.Cog):
else:
await ctx.send(f"Season {self.current_season.season_name} already active")
+ @branding_cmds.group(name="daemon", aliases=["d"])
+ async def daemon_group(self, ctx: commands.Context) -> None:
+ """
+ Check whether the daemon is currently active.
+
+ Sub-commands allow starting and stopping the daemon.
+ """
+ if not ctx.invoked_subcommand:
+ if self._daemon_running:
+ remaining_time = (arrow.utcnow() + await time_until_midnight()).humanize()
+ response = discord.Embed(description=f"Daemon running {Emojis.ok_hand}", colour=Colours.soft_green)
+ response.set_footer(text=f"Next refresh {remaining_time}")
+ else:
+ response = discord.Embed(description="Daemon not running", colour=Colours.soft_red)
+
+ await ctx.send(embed=response)
+
+ @daemon_group.command(name="start")
+ async def daemon_start(self, ctx: commands.Context) -> None:
+ """If the daemon isn't running, start it."""
+ if self._daemon_running:
+ raise commands.BadArgument("Daemon already running")
+
+ self.daemon = self.bot.loop.create_task(self._daemon_func())
+ response = discord.Embed(description=f"Daemon started {Emojis.ok_hand}", colour=Colours.soft_green)
+ await ctx.send(embed=response)
+
+ @daemon_group.command(name="stop")
+ async def daemon_stop(self, ctx: commands.Context) -> None:
+ """If the daemon is running, stop it."""
+ if not self._daemon_running:
+ raise commands.BadArgument("Daemon not running")
+
+ self.daemon.cancel()
+ response = discord.Embed(description=f"Daemon stopped {Emojis.ok_hand}", colour=Colours.soft_green)
+ await ctx.send(embed=response)
+
def setup(bot: SeasonalBot) -> None:
"""Load BrandingManager cog."""