Merge pull request '0.3 - Xp für Reaction (#118)' (#121) from #118 into 0.3

Reviewed-on: sh-edraft.de/kd_discord_bot#121
Reviewed-by: Ebola-Chan <nick.jungmann@gmail.com>
Closes #121
This commit is contained in:
Sven Heidemann 2022-11-17 20:01:12 +01:00
commit 4f4e80fb6b
11 changed files with 170 additions and 8 deletions

@ -1 +1 @@
Subproject commit 58934dde3ca8446212a0319088b8726527bd89ab Subproject commit c8e3ac096317cfdafe809398a80cf659189d42a5

View File

@ -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.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_add import AutoRoleOnRawReactionAddEvent
from modules.auto_role.events.auto_role_on_raw_reaction_remove import AutoRoleOnRawReactionRemoveEvent 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): class AutoRoleModule(ModuleABC):
@ -21,7 +21,7 @@ class AutoRoleModule(ModuleABC):
pass pass
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
services.add_transient(ReactionHandler) services.add_transient(AutoRoleReactionHandler)
# commands # commands
self._dc.add_command(AutoRoleGroup) self._dc.add_command(AutoRoleGroup)
# events # events

View File

@ -6,7 +6,7 @@ from discord import RawReactionActionEvent
from bot_core.helper.event_checks import EventChecks from bot_core.helper.event_checks import EventChecks
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC 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): class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC):
@ -17,7 +17,7 @@ class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC):
bot: DiscordBotServiceABC, bot: DiscordBotServiceABC,
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
auto_roles: AutoRoleRepositoryABC, auto_roles: AutoRoleRepositoryABC,
reaction_handler: ReactionHandler reaction_handler: AutoRoleReactionHandler
): ):
OnRawReactionAddABC.__init__(self) OnRawReactionAddABC.__init__(self)

View File

@ -6,7 +6,7 @@ from discord import RawReactionActionEvent
from bot_core.helper.event_checks import EventChecks from bot_core.helper.event_checks import EventChecks
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC 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): class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC):
@ -17,7 +17,7 @@ class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC):
bot: DiscordBotServiceABC, bot: DiscordBotServiceABC,
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
auto_roles: AutoRoleRepositoryABC, auto_roles: AutoRoleRepositoryABC,
reaction_handler: ReactionHandler reaction_handler: AutoRoleReactionHandler
): ):
OnRawReactionRemoveABC.__init__(self) OnRawReactionRemoveABC.__init__(self)

View File

@ -10,7 +10,7 @@ from bot_data.model.auto_role import AutoRole
from bot_data.model.auto_role_rule import AutoRoleRule from bot_data.model.auto_role_rule import AutoRoleRule
class ReactionHandler: class AutoRoleReactionHandler:
def __init__( def __init__(
self, self,

View File

@ -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_join_event import BaseOnMemberJoinEvent
from modules.base.events.base_on_member_remove_event import BaseOnMemberRemoveEvent 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_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 import BaseOnVoiceStateUpdateEvent
from modules.base.events.base_on_voice_state_update_event_help_channel import BaseOnVoiceStateUpdateEventHelpChannel 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 from modules.base.service.base_helper_service import BaseHelperService
@ -35,6 +38,7 @@ class BaseModule(ModuleABC):
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
services.add_transient(BaseHelperABC, BaseHelperService) services.add_transient(BaseHelperABC, BaseHelperService)
services.add_transient(BaseReactionHandler)
# commands # commands
self._dc.add_command(AFKCommand) self._dc.add_command(AFKCommand)
self._dc.add_command(HelpCommand) 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_join.value, BaseOnMemberJoinEvent)
self._dc.add_event(DiscordEventTypesEnum.on_member_remove.value, BaseOnMemberRemoveEvent) 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_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, BaseOnVoiceStateUpdateEvent)
self._dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, BaseOnVoiceStateUpdateEventHelpChannel) self._dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, BaseOnVoiceStateUpdateEventHelpChannel)

View File

@ -13,6 +13,7 @@ class BaseServerSettings(ConfigurationModelABC):
self._id: int = 0 self._id: int = 0
self._max_voice_state_hours: int = 0 self._max_voice_state_hours: int = 0
self._xp_per_message: int = 0 self._xp_per_message: int = 0
self._xp_per_reaction: int = 0
self._xp_per_ontime_hour: int = 0 self._xp_per_ontime_hour: int = 0
self._afk_channel_ids: List[int] = List(int) self._afk_channel_ids: List[int] = List(int)
self._afk_command_channel_id: int = 0 self._afk_command_channel_id: int = 0
@ -32,6 +33,10 @@ class BaseServerSettings(ConfigurationModelABC):
def xp_per_message(self) -> int: def xp_per_message(self) -> int:
return self._xp_per_message return self._xp_per_message
@property
def xp_per_reaction(self) -> int:
return self._xp_per_reaction
@property @property
def xp_per_ontime_hour(self) -> int: def xp_per_ontime_hour(self) -> int:
return self._xp_per_ontime_hour return self._xp_per_ontime_hour
@ -61,6 +66,7 @@ class BaseServerSettings(ConfigurationModelABC):
self._id = int(settings['Id']) self._id = int(settings['Id'])
self._max_voice_state_hours = int(settings['MaxVoiceStateHours']) self._max_voice_state_hours = int(settings['MaxVoiceStateHours'])
self._xp_per_message = int(settings['XpPerMessage']) self._xp_per_message = int(settings['XpPerMessage'])
self._xp_per_reaction = int(settings['XpPerReaction'])
self._xp_per_ontime_hour = int(settings['XpPerOntimeHour']) self._xp_per_ontime_hour = int(settings['XpPerOntimeHour'])
for index in settings['AFKChannelIds']: for index in settings['AFKChannelIds']:
self._afk_channel_ids.append(int(index)) self._afk_channel_ids.append(int(index))

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -0,0 +1,52 @@
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()
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}')