From 4cea31fce54303a2a49a6a121d8d9c442de10267 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 16 Aug 2023 21:20:14 +0200 Subject: [PATCH] Added achievement validation triggers #340 #1.1.0.rc3 --- .../achievements/achievements_module.py | 6 ++ .../achievement_on_reaction_add_event.py | 61 +++++++++++++++++++ .../achievement_on_reaction_remove_event.py | 61 +++++++++++++++++++ ...achievement_on_voice_state_update_event.py | 55 +++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 kdb-bot/src/modules/achievements/events/achievement_on_reaction_add_event.py create mode 100644 kdb-bot/src/modules/achievements/events/achievement_on_reaction_remove_event.py create mode 100644 kdb-bot/src/modules/achievements/events/achievement_on_voice_state_update_event.py diff --git a/kdb-bot/src/modules/achievements/achievements_module.py b/kdb-bot/src/modules/achievements/achievements_module.py index cfe1ce2b..3860feac 100644 --- a/kdb-bot/src/modules/achievements/achievements_module.py +++ b/kdb-bot/src/modules/achievements/achievements_module.py @@ -10,6 +10,9 @@ from modules.achievements.achievement_attribute_resolver import AchievementAttri from modules.achievements.achievement_service import AchievementService from modules.achievements.commands.achievements_group import AchievementGroup from modules.achievements.events.achievement_on_message_event import AchievementOnMessageEvent +from modules.achievements.events.achievement_on_reaction_add_event import AchievementOnReactionAddEvent +from modules.achievements.events.achievement_on_reaction_remove_event import AchievementOnReactionRemoveEvent +from modules.achievements.events.achievement_on_voice_state_update_event import AchievementOnVoiceStateUpdateEvent class AchievementsModule(ModuleABC): @@ -26,3 +29,6 @@ class AchievementsModule(ModuleABC): self._dc.add_command(AchievementGroup) self._dc.add_event(DiscordEventTypesEnum.on_message.value, AchievementOnMessageEvent) + self._dc.add_event(DiscordEventTypesEnum.on_reaction_add.value, AchievementOnReactionAddEvent) + self._dc.add_event(DiscordEventTypesEnum.on_reaction_remove.value, AchievementOnReactionRemoveEvent) + self._dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, AchievementOnVoiceStateUpdateEvent) diff --git a/kdb-bot/src/modules/achievements/events/achievement_on_reaction_add_event.py b/kdb-bot/src/modules/achievements/events/achievement_on_reaction_add_event.py new file mode 100644 index 00000000..f489bb08 --- /dev/null +++ b/kdb-bot/src/modules/achievements/events/achievement_on_reaction_add_event.py @@ -0,0 +1,61 @@ +from typing import Union + +import discord +from cpl_core.configuration import ConfigurationABC +from cpl_core.database.context import DatabaseContextABC +from cpl_core.logging import LoggerABC +from cpl_discord.events import OnReactionAddABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.server_config import ServerConfig +from modules.achievements.achievement_service import AchievementService + + +class AchievementOnReactionAddEvent(OnReactionAddABC): + def __init__( + self, + config: ConfigurationABC, + logger: LoggerABC, + bot: DiscordBotServiceABC, + achievements: AchievementService, + db: DatabaseContextABC, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + ): + OnReactionAddABC.__init__(self) + + self._config = config + self._logger = logger + self._bot = bot + self._achievements = achievements + self._db = db + self._servers = servers + self._users = users + + async def on_reaction_add(self, reaction: discord.reaction.Reaction, user: Union[discord.Member, discord.User]): + if not isinstance(user, discord.Member): + return + + if user.guild is None: + return + + if user.bot: + return + + server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{user.guild.id}") + if not FeatureFlagsSettings.get_flag_from_dict( + server_config.feature_flags, FeatureFlagsEnum.achievements_module + ): + return + + server = self._servers.get_server_by_discord_id(user.guild.id) + user = self._users.get_user_by_discord_id_and_server_id(user.author.id, server.id) + + user.reaction_count += 1 + self._db.save_changes() + + await self._achievements.validate_achievements_for_user(user) diff --git a/kdb-bot/src/modules/achievements/events/achievement_on_reaction_remove_event.py b/kdb-bot/src/modules/achievements/events/achievement_on_reaction_remove_event.py new file mode 100644 index 00000000..4efb5588 --- /dev/null +++ b/kdb-bot/src/modules/achievements/events/achievement_on_reaction_remove_event.py @@ -0,0 +1,61 @@ +from typing import Union + +import discord +from cpl_core.configuration import ConfigurationABC +from cpl_core.database.context import DatabaseContextABC +from cpl_core.logging import LoggerABC +from cpl_discord.events import OnReactionRemoveABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.server_config import ServerConfig +from modules.achievements.achievement_service import AchievementService + + +class AchievementOnReactionRemoveEvent(OnReactionRemoveABC): + def __init__( + self, + config: ConfigurationABC, + logger: LoggerABC, + bot: DiscordBotServiceABC, + achievements: AchievementService, + db: DatabaseContextABC, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + ): + OnReactionRemoveABC.__init__(self) + + self._config = config + self._logger = logger + self._bot = bot + self._achievements = achievements + self._db = db + self._servers = servers + self._users = users + + async def on_reaction_remove(self, reaction: discord.reaction.Reaction, user: Union[discord.Member, discord.User]): + if not isinstance(user, discord.Member): + return + + if user.guild is None: + return + + if user.bot: + return + + server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{user.guild.id}") + if not FeatureFlagsSettings.get_flag_from_dict( + server_config.feature_flags, FeatureFlagsEnum.achievements_module + ): + return + + server = self._servers.get_server_by_discord_id(user.guild.id) + user = self._users.get_user_by_discord_id_and_server_id(user.author.id, server.id) + + user.reaction_count -= 1 + self._db.save_changes() + + await self._achievements.validate_achievements_for_user(user) diff --git a/kdb-bot/src/modules/achievements/events/achievement_on_voice_state_update_event.py b/kdb-bot/src/modules/achievements/events/achievement_on_voice_state_update_event.py new file mode 100644 index 00000000..307d4e18 --- /dev/null +++ b/kdb-bot/src/modules/achievements/events/achievement_on_voice_state_update_event.py @@ -0,0 +1,55 @@ +import discord +from cpl_core.configuration import ConfigurationABC +from cpl_core.database.context import DatabaseContextABC +from cpl_core.logging import LoggerABC +from cpl_discord.events import OnVoiceStateUpdateABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.server_config import ServerConfig +from modules.achievements.achievement_service import AchievementService + + +class AchievementOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC): + def __init__( + self, + config: ConfigurationABC, + logger: LoggerABC, + bot: DiscordBotServiceABC, + achievements: AchievementService, + db: DatabaseContextABC, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + ): + OnVoiceStateUpdateABC.__init__(self) + + self._config = config + self._logger = logger + self._bot = bot + self._achievements = achievements + self._db = db + self._servers = servers + self._users = users + + async def on_voice_state_update( + self, member: discord.member.Member, before: discord.member.VoiceState, after: discord.member.VoiceState + ): + if member.guild is None: + return + + if member.author.bot: + return + + server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{member.guild.id}") + if not FeatureFlagsSettings.get_flag_from_dict( + server_config.feature_flags, FeatureFlagsEnum.achievements_module + ): + return + + server = self._servers.get_server_by_discord_id(member.guild.id) + user = self._users.get_user_by_discord_id_and_server_id(member.author.id, server.id) + + await self._achievements.validate_achievements_for_user(user)