Send message to team chat when a member leaves the server #295

This commit is contained in:
Sven Heidemann 2023-08-14 18:44:07 +02:00
parent 51d95c81c1
commit bfe72668dc
4 changed files with 25 additions and 7 deletions

View File

@ -151,6 +151,7 @@
} }
}, },
"base": { "base": {
"member_left_message": "{} hat uns leider verlassen :(",
"complaints": { "complaints": {
"title": "Beschwerde einreichen", "title": "Beschwerde einreichen",
"label": "Beschwerde", "label": "Beschwerde",

View File

@ -87,7 +87,7 @@ class BaseOnMemberJoinEvent(OnMemberJoinABC):
return return
self._logger.debug(__name__, f"Add user: {member.id}") 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() self._db.save_changes()
user = self._users.get_user_by_discord_id_and_server_id(member.id, server.id) 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())) self._user_joins.add_user_joined_server(UserJoinedServer(user, datetime.now()))

View File

@ -2,6 +2,7 @@ from datetime import datetime
from typing import Union from typing import Union
import discord import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from cpl_discord.events import OnMemberRemoveABC 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.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.server_config import ServerConfig
class BaseOnMemberRemoveEvent(OnMemberRemoveABC): class BaseOnMemberRemoveEvent(OnMemberRemoveABC):
def __init__( def __init__(
self, self,
config: ConfigurationABC,
logger: LoggerABC, logger: LoggerABC,
db: DatabaseContextABC, db: DatabaseContextABC,
messenger: MessageServiceABC, message_service: MessageServiceABC,
users: UserRepositoryABC, users: UserRepositoryABC,
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
user_joins: UserJoinedServerRepositoryABC, user_joins: UserJoinedServerRepositoryABC,
@ -27,10 +30,10 @@ class BaseOnMemberRemoveEvent(OnMemberRemoveABC):
): ):
OnMemberRemoveABC.__init__(self) OnMemberRemoveABC.__init__(self)
self._config = config
self._logger = logger self._logger = logger
self._base_helper = base_helper
self._db = db self._db = db
self._messenger = messenger self._message_service = message_service
self._users = users self._users = users
self._servers = servers self._servers = servers
self._user_joins = user_joins self._user_joins = user_joins
@ -38,8 +41,7 @@ class BaseOnMemberRemoveEvent(OnMemberRemoveABC):
async def _remove_user(self, member: Union[discord.User, discord.Member]): async def _remove_user(self, member: Union[discord.User, discord.Member]):
self._logger.debug(__name__, f"Remove user {member}") self._logger.debug(__name__, f"Remove user {member}")
settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{member.guild.id}") await self._message_service.send_dm_message(self._t.transform("modules.base.goodbye_message"), member)
await self._messenger.send_dm_message(self._t.transform("modules.base.goodbye_message"), member)
try: try:
server = self._servers.get_server_by_discord_id(member.guild.id) server = self._servers.get_server_by_discord_id(member.guild.id)
@ -56,7 +58,19 @@ class BaseOnMemberRemoveEvent(OnMemberRemoveABC):
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Cannot remove user {member.id}", 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() @EventChecks.check_is_ready()
async def on_member_remove(self, member: discord.Member): async def on_member_remove(self, member: discord.Member):
self._logger.debug(__name__, f"Module {type(self)} started") self._logger.debug(__name__, f"Module {type(self)} started")
await self._remove_user(member) await self._remove_user(member)
await self._notify_team(member)

View File

@ -1,6 +1,7 @@
from typing import Optional from typing import Optional
import discord import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_discord.events import OnMessageABC from cpl_discord.events import OnMessageABC
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
@ -19,6 +20,7 @@ from bot_data.model.user import User
class BaseOnMessageEvent(OnMessageABC): class BaseOnMessageEvent(OnMessageABC):
def __init__( def __init__(
self, self,
config: ConfigurationABC,
logger: MessageLogger, logger: MessageLogger,
client_utils: ClientUtilsService, client_utils: ClientUtilsService,
db: DatabaseContextABC, db: DatabaseContextABC,
@ -28,6 +30,7 @@ class BaseOnMessageEvent(OnMessageABC):
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
): ):
OnMessageABC.__init__(self) OnMessageABC.__init__(self)
self._config = config
self._logger = logger self._logger = logger
self._client_utils = client_utils self._client_utils = client_utils
self._bot = bot self._bot = bot
@ -63,7 +66,7 @@ class BaseOnMessageEvent(OnMessageABC):
self._logger.error(__name__, f"User not found {dc_user_id}") self._logger.error(__name__, f"User not found {dc_user_id}")
return 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( if self._client_utils.is_message_xp_count_by_hour_higher_that_max_message_count_per_hour(
message.created_at, user, settings message.created_at, user, settings
): ):