diff --git a/somnus/discord_provider/main.py b/somnus/discord_provider/main.py index 6503a85..1bc9cab 100644 --- a/somnus/discord_provider/main.py +++ b/somnus/discord_provider/main.py @@ -42,8 +42,6 @@ class ActionWrapperProperties(BaseModel): async def action_wrapper(props: ActionWrapperProperties) -> None: - global inactivity_seconds # noqa: PLW0603 - if busy_provider.is_busy(): await props.ctx.response.send_message(LH("other.busy"), ephemeral=True) return @@ -88,10 +86,6 @@ async def action_wrapper(props: ActionWrapperProperties) -> None: finally: busy_provider.make_available() - if CONFIG.INACTIVITY_SHUTDOWN_MINUTES: - inactivity_seconds = CONFIG.INACTIVITY_SHUTDOWN_MINUTES * 60 - update_players_online_status.start() - log.info("Status Updater started!") await _update_bot_presence_and_inactivity() @@ -135,6 +129,8 @@ async def ping_command(ctx: discord.Interaction) -> None: @tree.command(name="start", description=LH("commands.start.description")) async def start_server_command(ctx: discord.Interaction) -> None: + global inactivity_seconds + world_config = await world_selector.get_world_selector_config() action_props = ActionWrapperProperties( @@ -149,6 +145,11 @@ async def start_server_command(ctx: discord.Interaction) -> None: await action_wrapper(action_props) except Exception: pass + else: + if CONFIG.INACTIVITY_SHUTDOWN_MINUTES: + inactivity_seconds = CONFIG.INACTIVITY_SHUTDOWN_MINUTES * 60 + update_players_online_status.start() + log.info("Status Updater started!") @tree.command(name="stop", description=LH("commands.stop.description")) @@ -738,8 +739,12 @@ async def _stop_inactivity() -> bool | None: activity = discord.Game(name=LH("status.text.stopping", args={"world_name": world_config.current_world})) await bot.change_presence(status=Status.idle, activity=activity) - async for _ in stop.stop_server(True, CONFIG): - pass + try: + async for _ in stop.stop_server(True, CONFIG): + pass + except Exception as e: + log.error("Failed to stop server during inactivity shutdown", exc_info=e) + await message.edit(content=LH("commands.stop.error.general", args={"e": e})) await _update_bot_presence_and_inactivity() await message.edit(content=LH("other.inactivity_shutdown.finished_msg")) busy_provider.make_available() @@ -824,7 +829,10 @@ async def _is_super_user(ctx: discord.Interaction, message: bool = True) -> bool @tasks.loop(seconds=10) async def update_players_online_status() -> None: - await _update_bot_presence_and_inactivity() + try: + await _update_bot_presence_and_inactivity() + except Exception as e: + log.error("Failed to update bot presence and inactivity", exc_info=e) def main(config: Config = CONFIG) -> None: