From bfe72668dc098270b8687c2a2dcd22cabe87d0fc Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 14 Aug 2023 18:44:07 +0200 Subject: [PATCH] Send message to team chat when a member leaves the server #295 --- kdb-bot/src/bot/translation/de.json | 1 + .../base/events/base_on_member_join_event.py | 2 +- .../events/base_on_member_remove_event.py | 24 +++++++++++++++---- .../base/events/base_on_message_event.py | 5 +++- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index c4939ad7..82842240 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -151,6 +151,7 @@ } }, "base": { + "member_left_message": "{} hat uns leider verlassen :(", "complaints": { "title": "Beschwerde einreichen", "label": "Beschwerde", diff --git a/kdb-bot/src/modules/base/events/base_on_member_join_event.py b/kdb-bot/src/modules/base/events/base_on_member_join_event.py index 8f8007f5..58af0a00 100644 --- a/kdb-bot/src/modules/base/events/base_on_member_join_event.py +++ b/kdb-bot/src/modules/base/events/base_on_member_join_event.py @@ -87,7 +87,7 @@ class BaseOnMemberJoinEvent(OnMemberJoinABC): return self._logger.debug(__name__, f"Add user: {member.id}") - self._users.add_user(User(member.id, 0, server)) + self._users.add_user(User(member.id, 0, 0, 0, server)) self._db.save_changes() user = self._users.get_user_by_discord_id_and_server_id(member.id, server.id) self._user_joins.add_user_joined_server(UserJoinedServer(user, datetime.now())) diff --git a/kdb-bot/src/modules/base/events/base_on_member_remove_event.py b/kdb-bot/src/modules/base/events/base_on_member_remove_event.py index 8e565267..adfe7e87 100644 --- a/kdb-bot/src/modules/base/events/base_on_member_remove_event.py +++ b/kdb-bot/src/modules/base/events/base_on_member_remove_event.py @@ -2,6 +2,7 @@ from datetime import datetime 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 OnMemberRemoveABC @@ -12,14 +13,16 @@ from bot_core.helper.event_checks import EventChecks from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.server_config import ServerConfig class BaseOnMemberRemoveEvent(OnMemberRemoveABC): def __init__( self, + config: ConfigurationABC, logger: LoggerABC, db: DatabaseContextABC, - messenger: MessageServiceABC, + message_service: MessageServiceABC, users: UserRepositoryABC, servers: ServerRepositoryABC, user_joins: UserJoinedServerRepositoryABC, @@ -27,10 +30,10 @@ class BaseOnMemberRemoveEvent(OnMemberRemoveABC): ): OnMemberRemoveABC.__init__(self) + self._config = config self._logger = logger - self._base_helper = base_helper self._db = db - self._messenger = messenger + self._message_service = message_service self._users = users self._servers = servers self._user_joins = user_joins @@ -38,8 +41,7 @@ class BaseOnMemberRemoveEvent(OnMemberRemoveABC): async def _remove_user(self, member: Union[discord.User, discord.Member]): self._logger.debug(__name__, f"Remove user {member}") - settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{member.guild.id}") - await self._messenger.send_dm_message(self._t.transform("modules.base.goodbye_message"), member) + await self._message_service.send_dm_message(self._t.transform("modules.base.goodbye_message"), member) try: server = self._servers.get_server_by_discord_id(member.guild.id) @@ -56,7 +58,19 @@ class BaseOnMemberRemoveEvent(OnMemberRemoveABC): except Exception as e: self._logger.error(__name__, f"Cannot remove user {member.id}", e) + async def _notify_team(self, member: discord.Member): + self._logger.debug(__name__, f"Notify team that a member left") + settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{member.guild.id}") + channel = member.guild.get_channel(settings.team_channel_id) + await self._message_service.send_channel_message( + channel, + self._t.transform("modules.base.member_left_message").format(member.mention), + is_persistent=True, + ) + self._logger.trace(__name__, f"Notified team that a member left") + @EventChecks.check_is_ready() async def on_member_remove(self, member: discord.Member): self._logger.debug(__name__, f"Module {type(self)} started") await self._remove_user(member) + await self._notify_team(member) diff --git a/kdb-bot/src/modules/base/events/base_on_message_event.py b/kdb-bot/src/modules/base/events/base_on_message_event.py index 61789132..e6531385 100644 --- a/kdb-bot/src/modules/base/events/base_on_message_event.py +++ b/kdb-bot/src/modules/base/events/base_on_message_event.py @@ -1,6 +1,7 @@ from typing import Optional import discord +from cpl_core.configuration import ConfigurationABC from cpl_core.database.context import DatabaseContextABC from cpl_discord.events import OnMessageABC from cpl_discord.service import DiscordBotServiceABC @@ -19,6 +20,7 @@ from bot_data.model.user import User class BaseOnMessageEvent(OnMessageABC): def __init__( self, + config: ConfigurationABC, logger: MessageLogger, client_utils: ClientUtilsService, db: DatabaseContextABC, @@ -28,6 +30,7 @@ class BaseOnMessageEvent(OnMessageABC): servers: ServerRepositoryABC, ): OnMessageABC.__init__(self) + self._config = config self._logger = logger self._client_utils = client_utils self._bot = bot @@ -63,7 +66,7 @@ class BaseOnMessageEvent(OnMessageABC): self._logger.error(__name__, f"User not found {dc_user_id}") return - settings: ServerConfig = se + settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{server.discord_id}") if self._client_utils.is_message_xp_count_by_hour_higher_that_max_message_count_per_hour( message.created_at, user, settings ):