From d45d787cea4275360aeafc066a3b991dc77e70c0 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 17 Nov 2022 16:35:47 +0100 Subject: [PATCH] Added logic to add xp on reaction #118 --- kdb-bot/src/bot/config | 2 +- .../src/modules/auto_role/auto_role_module.py | 4 +- .../events/auto_role_on_raw_reaction_add.py | 4 +- .../auto_role_on_raw_reaction_remove.py | 4 +- ...ndler.py => auto_role_reaction_handler.py} | 2 +- kdb-bot/src/modules/base/base_module.py | 6 +++ .../configuration/base_server_settings.py | 6 +++ .../base/events/base_on_raw_reaction_add.py | 36 +++++++++++++ .../events/base_on_raw_reaction_remove.py | 36 +++++++++++++ kdb-bot/src/modules/base/helper/__init__.py | 26 +++++++++ .../base/helper/base_reaction_handler.py | 53 +++++++++++++++++++ 11 files changed, 171 insertions(+), 8 deletions(-) rename kdb-bot/src/modules/auto_role/helper/{reaction_handler.py => auto_role_reaction_handler.py} (98%) create mode 100644 kdb-bot/src/modules/base/events/base_on_raw_reaction_add.py create mode 100644 kdb-bot/src/modules/base/events/base_on_raw_reaction_remove.py create mode 100644 kdb-bot/src/modules/base/helper/__init__.py create mode 100644 kdb-bot/src/modules/base/helper/base_reaction_handler.py diff --git a/kdb-bot/src/bot/config b/kdb-bot/src/bot/config index bd8d3a5d..c8e3ac09 160000 --- a/kdb-bot/src/bot/config +++ b/kdb-bot/src/bot/config @@ -1 +1 @@ -Subproject commit bd8d3a5dad13e0fdcad79b767c032997b716b1a4 +Subproject commit c8e3ac096317cfdafe809398a80cf659189d42a5 diff --git a/kdb-bot/src/modules/auto_role/auto_role_module.py b/kdb-bot/src/modules/auto_role/auto_role_module.py index cf53c6c4..f4528625 100644 --- a/kdb-bot/src/modules/auto_role/auto_role_module.py +++ b/kdb-bot/src/modules/auto_role/auto_role_module.py @@ -9,7 +9,7 @@ from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from modules.auto_role.command.auto_role_group import AutoRoleGroup from modules.auto_role.events.auto_role_on_raw_reaction_add import AutoRoleOnRawReactionAddEvent from modules.auto_role.events.auto_role_on_raw_reaction_remove import AutoRoleOnRawReactionRemoveEvent -from modules.auto_role.helper.reaction_handler import ReactionHandler +from modules.auto_role.helper.auto_role_reaction_handler import AutoRoleReactionHandler class AutoRoleModule(ModuleABC): @@ -21,7 +21,7 @@ class AutoRoleModule(ModuleABC): pass def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): - services.add_transient(ReactionHandler) + services.add_transient(AutoRoleReactionHandler) # commands self._dc.add_command(AutoRoleGroup) # events diff --git a/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_add.py b/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_add.py index 5893d463..5c891c02 100644 --- a/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_add.py +++ b/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_add.py @@ -6,7 +6,7 @@ from discord import RawReactionActionEvent from bot_core.helper.event_checks import EventChecks from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC -from modules.auto_role.helper.reaction_handler import ReactionHandler +from modules.auto_role.helper.auto_role_reaction_handler import AutoRoleReactionHandler class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC): @@ -17,7 +17,7 @@ class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC): bot: DiscordBotServiceABC, servers: ServerRepositoryABC, auto_roles: AutoRoleRepositoryABC, - reaction_handler: ReactionHandler + reaction_handler: AutoRoleReactionHandler ): OnRawReactionAddABC.__init__(self) diff --git a/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py b/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py index 49899a0e..5e85f740 100644 --- a/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py +++ b/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py @@ -6,7 +6,7 @@ from discord import RawReactionActionEvent from bot_core.helper.event_checks import EventChecks from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC -from modules.auto_role.helper.reaction_handler import ReactionHandler +from modules.auto_role.helper.auto_role_reaction_handler import AutoRoleReactionHandler class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC): @@ -17,7 +17,7 @@ class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC): bot: DiscordBotServiceABC, servers: ServerRepositoryABC, auto_roles: AutoRoleRepositoryABC, - reaction_handler: ReactionHandler + reaction_handler: AutoRoleReactionHandler ): OnRawReactionRemoveABC.__init__(self) diff --git a/kdb-bot/src/modules/auto_role/helper/reaction_handler.py b/kdb-bot/src/modules/auto_role/helper/auto_role_reaction_handler.py similarity index 98% rename from kdb-bot/src/modules/auto_role/helper/reaction_handler.py rename to kdb-bot/src/modules/auto_role/helper/auto_role_reaction_handler.py index 56c8df87..713af5cc 100644 --- a/kdb-bot/src/modules/auto_role/helper/reaction_handler.py +++ b/kdb-bot/src/modules/auto_role/helper/auto_role_reaction_handler.py @@ -10,7 +10,7 @@ from bot_data.model.auto_role import AutoRole from bot_data.model.auto_role_rule import AutoRoleRule -class ReactionHandler: +class AutoRoleReactionHandler: def __init__( self, diff --git a/kdb-bot/src/modules/base/base_module.py b/kdb-bot/src/modules/base/base_module.py index 8da27656..c19ca761 100644 --- a/kdb-bot/src/modules/base/base_module.py +++ b/kdb-bot/src/modules/base/base_module.py @@ -20,8 +20,11 @@ from modules.base.events.base_on_command_event import BaseOnCommandEvent from modules.base.events.base_on_member_join_event import BaseOnMemberJoinEvent from modules.base.events.base_on_member_remove_event import BaseOnMemberRemoveEvent 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_voice_state_update_event import BaseOnVoiceStateUpdateEvent from modules.base.events.base_on_voice_state_update_event_help_channel import BaseOnVoiceStateUpdateEventHelpChannel +from modules.base.helper.base_reaction_handler import BaseReactionHandler from modules.base.service.base_helper_service import BaseHelperService @@ -35,6 +38,7 @@ class BaseModule(ModuleABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): services.add_transient(BaseHelperABC, BaseHelperService) + services.add_transient(BaseReactionHandler) # commands self._dc.add_command(AFKCommand) self._dc.add_command(HelpCommand) @@ -51,5 +55,7 @@ class BaseModule(ModuleABC): self._dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberJoinEvent) self._dc.add_event(DiscordEventTypesEnum.on_member_remove.value, BaseOnMemberRemoveEvent) self._dc.add_event(DiscordEventTypesEnum.on_message.value, BaseOnMessageEvent) + self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, BaseOnRawReactionAddEvent) + self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_remove.value, BaseOnRawReactionRemoveEvent) self._dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, BaseOnVoiceStateUpdateEvent) self._dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, BaseOnVoiceStateUpdateEventHelpChannel) 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 578839b9..bebd14bd 100644 --- a/kdb-bot/src/modules/base/configuration/base_server_settings.py +++ b/kdb-bot/src/modules/base/configuration/base_server_settings.py @@ -13,6 +13,7 @@ class BaseServerSettings(ConfigurationModelABC): self._id: int = 0 self._max_voice_state_hours: int = 0 self._xp_per_message: int = 0 + self._xp_per_reaction: int = 0 self._xp_per_ontime_hour: int = 0 self._afk_channel_ids: List[int] = List(int) self._afk_command_channel_id: int = 0 @@ -31,6 +32,10 @@ class BaseServerSettings(ConfigurationModelABC): def xp_per_message(self) -> int: return self._xp_per_message + @property + def xp_per_reaction(self) -> int: + return self._xp_per_reaction + @property def xp_per_ontime_hour(self) -> int: return self._xp_per_ontime_hour @@ -56,6 +61,7 @@ class BaseServerSettings(ConfigurationModelABC): self._id = int(settings['Id']) self._max_voice_state_hours = int(settings['MaxVoiceStateHours']) self._xp_per_message = int(settings['XpPerMessage']) + self._xp_per_reaction = int(settings['XpPerReaction']) self._xp_per_ontime_hour = int(settings['XpPerOntimeHour']) for index in settings['AFKChannelIds']: self._afk_channel_ids.append(int(index)) diff --git a/kdb-bot/src/modules/base/events/base_on_raw_reaction_add.py b/kdb-bot/src/modules/base/events/base_on_raw_reaction_add.py new file mode 100644 index 00000000..b8fe597b --- /dev/null +++ b/kdb-bot/src/modules/base/events/base_on_raw_reaction_add.py @@ -0,0 +1,36 @@ +from cpl_core.logging import LoggerABC +from cpl_discord.events.on_raw_reaction_add_abc import OnRawReactionAddABC +from cpl_discord.service import DiscordBotServiceABC +from discord import RawReactionActionEvent + +from bot_core.helper.event_checks import EventChecks +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from modules.base.helper.base_reaction_handler import BaseReactionHandler + + +class BaseOnRawReactionAddEvent(OnRawReactionAddABC): + + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC, + reaction_handler: BaseReactionHandler + ): + OnRawReactionAddABC.__init__(self) + + self._logger = logger + self._bot = bot + self._servers = servers + self._auto_roles = auto_roles + self._reaction_handler = reaction_handler + + @EventChecks.check_is_ready() + async def on_raw_reaction_add(self, payload: RawReactionActionEvent): + self._logger.debug(__name__, f'Module {type(self)} started') + + await self._reaction_handler.handle(payload, 'add') + + self._logger.debug(__name__, f'Module {type(self)} stopped') diff --git a/kdb-bot/src/modules/base/events/base_on_raw_reaction_remove.py b/kdb-bot/src/modules/base/events/base_on_raw_reaction_remove.py new file mode 100644 index 00000000..4f2f0bb7 --- /dev/null +++ b/kdb-bot/src/modules/base/events/base_on_raw_reaction_remove.py @@ -0,0 +1,36 @@ +from cpl_core.logging import LoggerABC +from cpl_discord.events.on_raw_reaction_remove_abc import OnRawReactionRemoveABC +from cpl_discord.service import DiscordBotServiceABC +from discord import RawReactionActionEvent + +from bot_core.helper.event_checks import EventChecks +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from modules.base.helper.base_reaction_handler import BaseReactionHandler + + +class BaseOnRawReactionRemoveEvent(OnRawReactionRemoveABC): + + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC, + reaction_handler: BaseReactionHandler, + ): + OnRawReactionRemoveABC.__init__(self) + + self._logger = logger + self._bot = bot + self._servers = servers + self._auto_roles = auto_roles + self._reaction_handler = reaction_handler + + @EventChecks.check_is_ready() + async def on_raw_reaction_remove(self, payload: RawReactionActionEvent): + self._logger.debug(__name__, f'Module {type(self)} started') + + await self._reaction_handler.handle(payload, 'remove') + + self._logger.debug(__name__, f'Module {type(self)} stopped') diff --git a/kdb-bot/src/modules/base/helper/__init__.py b/kdb-bot/src/modules/base/helper/__init__.py new file mode 100644 index 00000000..a19a540f --- /dev/null +++ b/kdb-bot/src/modules/base/helper/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +bot Keksdose bot +~~~~~~~~~~~~~~~~~~~ + +Discord bot for the Keksdose discord Server + +:copyright: (c) 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'modules.auto_role.helper' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2022 sh-edraft.de' +__version__ = '0.3.dev25' + +from collections import namedtuple + + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='3', micro='dev25') diff --git a/kdb-bot/src/modules/base/helper/base_reaction_handler.py b/kdb-bot/src/modules/base/helper/base_reaction_handler.py new file mode 100644 index 00000000..b7b07136 --- /dev/null +++ b/kdb-bot/src/modules/base/helper/base_reaction_handler.py @@ -0,0 +1,53 @@ +from cpl_core.database.context import DatabaseContextABC +from cpl_core.logging import LoggerABC +from cpl_discord.service import DiscordBotServiceABC +from discord import RawReactionActionEvent + +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from modules.base.abc.base_helper_abc import BaseHelperABC +from modules.base.configuration.base_server_settings import BaseServerSettings + + +class BaseReactionHandler: + + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + base_helper: BaseHelperABC, + db: DatabaseContextABC, + ): + self._logger = logger + self._bot = bot + self._servers = servers + self._users = users + self._base_helper = base_helper + self._db = db + + async def handle(self, payload: RawReactionActionEvent, r_type=None) -> None: + self._logger.trace(__name__, f'Handle reaction {payload} {r_type}') + + guild = self._bot.get_guild(payload.guild_id) + member = guild.get_member(payload.user_id) + if member is None: + self._logger.warn(__name__, f'User {payload.user_id} in {guild.name} not found - skipping') + return + + server = self._servers.get_server_by_discord_id(guild.id) + user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id) + settings: BaseServerSettings = self._base_helper.get_config(guild.id) + + if r_type == 'add': + user.xp += settings.xp_per_reaction + self._users.update_user(user) + self._db.save_changes() + # for future use maybe + # elif r_type == 'remove': + # user.xp -= settings.xp_per_reaction + # self._users.update_user(user) + # self._db.save_changes() + else: + self._logger.warn(__name__, f'Invalid reaction type {r_type}')