From 6bf73fcf91ed6a3408684515b5f407c0f865509a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 13 Feb 2023 17:28:59 +0100 Subject: [PATCH] Added logic to save active events locally #167 --- kdb-bot/src/bot/bot.json | 4 +-- kdb-bot/src/modules/base/base_module.py | 8 +++++ .../configuration/base_server_settings.py | 8 +++++ .../base_on_scheduled_event_update_event.py | 35 +++++++++++++++++++ .../src/modules/base/service/event_service.py | 19 ++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 kdb-bot/src/modules/base/events/base_on_scheduled_event_update_event.py create mode 100644 kdb-bot/src/modules/base/service/event_service.py diff --git a/kdb-bot/src/bot/bot.json b/kdb-bot/src/bot/bot.json index a0297264..e5666f6a 100644 --- a/kdb-bot/src/bot/bot.json +++ b/kdb-bot/src/bot/bot.json @@ -19,7 +19,6 @@ "cpl-core==2022.12.1.post3", "cpl-translation==2022.12.1", "cpl-query==2022.12.2.post1", - "cpl-discord==2022.12.1.post2", "Flask==2.2.2", "Flask-Classful==0.14.2", "Flask-Cors==3.0.10", @@ -29,7 +28,8 @@ "eventlet==0.33.3", "requests-oauthlib==1.3.1", "icmplib==3.0.3", - "ariadne==0.17.1" + "ariadne==0.17.1", + "cpl-discord==2022.12.2" ], "DevDependencies": [ "cpl-cli==2022.12.1.post3" diff --git a/kdb-bot/src/modules/base/base_module.py b/kdb-bot/src/modules/base/base_module.py index 9377c6e9..b2c91348 100644 --- a/kdb-bot/src/modules/base/base_module.py +++ b/kdb-bot/src/modules/base/base_module.py @@ -23,6 +23,7 @@ from modules.base.events.base_on_message_delete_event import BaseOnMessageDelete from modules.base.events.base_on_message_event import BaseOnMessageEvent from modules.base.events.base_on_raw_reaction_add import BaseOnRawReactionAddEvent from modules.base.events.base_on_raw_reaction_remove import BaseOnRawReactionRemoveEvent +from modules.base.events.base_on_scheduled_event_update_event import BaseOnScheduledEventUpdateEvent from modules.base.events.base_on_voice_state_update_event import ( BaseOnVoiceStateUpdateEvent, ) @@ -31,6 +32,7 @@ from modules.base.events.base_on_voice_state_update_event_help_channel import ( ) from modules.base.helper.base_reaction_handler import BaseReactionHandler from modules.base.service.base_helper_service import BaseHelperService +from modules.base.service.event_service import EventService class BaseModule(ModuleABC): @@ -43,6 +45,8 @@ class BaseModule(ModuleABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): services.add_transient(BaseHelperABC, BaseHelperService) services.add_transient(BaseReactionHandler) + services.add_singleton(EventService) + # commands self._dc.add_command(AFKCommand) self._dc.add_command(HelpCommand) @@ -73,3 +77,7 @@ class BaseModule(ModuleABC): DiscordEventTypesEnum.on_voice_state_update.value, BaseOnVoiceStateUpdateEventHelpChannel, ) + self._dc.add_event( + DiscordEventTypesEnum.on_scheduled_event_update.value, + BaseOnScheduledEventUpdateEvent, + ) diff --git a/kdb-bot/src/modules/base/configuration/base_server_settings.py b/kdb-bot/src/modules/base/configuration/base_server_settings.py index 531e44c9..f9555890 100644 --- a/kdb-bot/src/modules/base/configuration/base_server_settings.py +++ b/kdb-bot/src/modules/base/configuration/base_server_settings.py @@ -15,6 +15,7 @@ class BaseServerSettings(ConfigurationModelABC): self._xp_per_reaction: int = 0 self._max_message_xp_per_hour: int = 0 self._xp_per_ontime_hour: int = 0 + self._xp_per_event_participation: int = 0 self._afk_channel_ids: List[int] = List(int) self._afk_command_channel_id: int = 0 self._help_command_reference_url: str = "" @@ -45,6 +46,10 @@ class BaseServerSettings(ConfigurationModelABC): def xp_per_ontime_hour(self) -> int: return self._xp_per_ontime_hour + @property + def xp_per_event_participation(self) -> int: + return self._xp_per_event_participation + @property def afk_channel_ids(self) -> List[int]: return self._afk_channel_ids @@ -73,6 +78,9 @@ class BaseServerSettings(ConfigurationModelABC): self._xp_per_reaction = int(settings["XpPerReaction"]) self._max_message_xp_per_hour = int(settings["MaxMessageXpPerHour"]) self._xp_per_ontime_hour = int(settings["XpPerOntimeHour"]) + self._xp_per_event_participation = ( + 0 if "XpPerEventParticipation" in settings else settings["XpPerEventParticipation"] + ) for index in settings["AFKChannelIds"]: self._afk_channel_ids.append(int(index)) self._afk_command_channel_id = settings["AFKCommandChannelId"] diff --git a/kdb-bot/src/modules/base/events/base_on_scheduled_event_update_event.py b/kdb-bot/src/modules/base/events/base_on_scheduled_event_update_event.py new file mode 100644 index 00000000..456599ee --- /dev/null +++ b/kdb-bot/src/modules/base/events/base_on_scheduled_event_update_event.py @@ -0,0 +1,35 @@ +import discord +from cpl_core.logging import LoggerABC +from cpl_discord.events.on_scheduled_event_update_abc import OnScheduledEventUpdateABC +from cpl_discord.service import DiscordBotServiceABC +from discord import EventStatus + +from modules.base.service.event_service import EventService + + +class BaseOnScheduledEventUpdateEvent(OnScheduledEventUpdateABC): + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + events: EventService, + ): + OnScheduledEventUpdateABC.__init__(self) + + self._logger = logger + self._bot = bot + self._events = events + + async def on_scheduled_event_update(self, before: discord.ScheduledEvent, after: discord.ScheduledEvent): + self._logger.debug(__name__, f"Module {type(self)} started") + + # save started event + if before.status != after.status and after.status == EventStatus.active: + self._events.add_event(after) + # delete stopped event + if before.status != after.status and ( + after.status.value == EventStatus.cancelled.value or after.status.value == EventStatus.completed.value + ): + self._events.remove_event(after) + + self._logger.debug(__name__, f"Module {type(self)} stopped") diff --git a/kdb-bot/src/modules/base/service/event_service.py b/kdb-bot/src/modules/base/service/event_service.py new file mode 100644 index 00000000..03cb0b02 --- /dev/null +++ b/kdb-bot/src/modules/base/service/event_service.py @@ -0,0 +1,19 @@ +from cpl_query.extension import List +from discord import ScheduledEvent + + +class EventService: + def __init__(self): + self._active_events = List(ScheduledEvent) + + def add_event(self, event: ScheduledEvent): + if self._active_events.contains(event): + return + + self._active_events.add(event) + + def remove_event(self, event: ScheduledEvent): + if not self._active_events.contains(event): + return + + self._active_events.remove(event)