Added better config handling to modules

This commit is contained in:
Sven Heidemann 2021-12-27 19:54:10 +01:00
parent 2f657a7ce7
commit 500d884ea1
2 changed files with 25 additions and 18 deletions

View File

@ -8,7 +8,6 @@ from cpl_core.logging.logger_abc import LoggerABC
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings
from gismo_data.abc.client_repository_abc import ClientRepositoryABC
from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC
from gismo_data.abc.server_repository_abc import ServerRepositoryABC
@ -69,6 +68,9 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
)
self._logger.trace(__name__, f'Module {type(self)} loaded')
def _get_config(self, g_id: int) -> BaseSettings:
return self._config.get_configuration(f'{type(self).__name__}_{g_id}')
def _apppend_received_message_count(self, g_id: int):
try:
self._clients.apppend_received_message_count(self._bot.user.id, g_id, 1)
@ -98,7 +100,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
async def _add_if_not_exists_user(self, member: Union[discord.User, discord.Member]):
self._logger.debug(__name__, f'Check if user exists {member}')
settings: ServerSettings = self._config.get_configuration(f'DSERVER_{member.guild.id}')
settings: BaseSettings = self._get_config()
await self._messenger.send_dm_message(settings.welcome_message.format(member.guild.name), member)
for roleId in settings.admin_roles:
@ -132,7 +134,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
async def _remove_user(self, member: Union[discord.User, discord.Member]):
self._logger.debug(__name__, f'Remove user {member}')
settings: ServerSettings = self._config.get_configuration(f'DSERVER_{member.guild.id}')
settings: BaseSettings = self._get_config()
await self._messenger.send_dm_message(settings.goodbye_message, member)
try:
@ -169,7 +171,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
self._db.save_changes()
return
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{user.server.discord_server_id}')
settings: BaseSettings = self._get_config()
join = self._user_joins_vc.get_active_user_joined_voice_channel_by_user_id(user.user_id)
join.leaved_on = datetime.now()
@ -177,7 +179,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
# ontime as hours
ontime = round((join.leaved_on - join.joined_on).total_seconds()/3600, 2)
old_xp = user.xp
user.xp += round(ontime * server_st.xp_per_ontime_hour)
user.xp += round(ontime * settings.xp_per_ontime_hour)
self._user_joins_vc.update_user_joined_voice_channel(join)
self._users.update_user(user)
@ -206,9 +208,9 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
self._logger.error(__name__, f'User not found {dc_user_id}')
return
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{user.server.discord_server_id}')
settings: BaseSettings = self._get_config()
old_xp = user.xp
user.xp += server_st._xp_per_message
user.xp += settings._xp_per_message
self._users.update_user(user)
self._db.save_changes()
@ -236,29 +238,29 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
self._logger.debug(__name__, f'Module {type(self)} started')
self._logger.trace(__name__, f'Detected on_voice_state_update {member.id} from {before} to {after}')
u: discord.User = member
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{member.guild.id}')
settings: BaseSettings = self._get_config()
server = self._servers.get_server_by_discord_id(member.guild.id)
try:
# join
if before.channel is None and after.channel is not None and after.channel.id not in server_st.afk_channel_ids:
if before.channel is None and after.channel is not None and after.channel.id not in settings.afk_channel_ids:
self._logger.trace(__name__, f'User {u.id} joined {after.channel}')
self._update_voice_state(True, member.id, after.channel.id, server.server_id)
# leave
elif before.channel is not None and after.channel is None and before.channel.id not in server_st.afk_channel_ids:
elif before.channel is not None and after.channel is None and before.channel.id not in settings.afk_channel_ids:
self._logger.trace(__name__, f'User {u.id} left {before.channel}')
self._update_voice_state(False, member.id, before.channel.id, server.server_id)
# channel to channel
elif before.channel is not None and after.channel is not None:
# joined
if before.channel.id in server_st.afk_channel_ids and after.channel.id not in server_st.afk_channel_ids:
if before.channel.id in settings.afk_channel_ids and after.channel.id not in settings.afk_channel_ids:
self._logger.trace(__name__, f'User {u.id} joined {after.channel}')
self._update_voice_state(True, member.id, after.channel.id, server.server_id)
# left
elif after.channel.id in server_st.afk_channel_ids and before.channel.id not in server_st.afk_channel_ids:
elif after.channel.id in settings.afk_channel_ids and before.channel.id not in settings.afk_channel_ids:
self._logger.trace(__name__, f'User {u.id} left {before.channel}')
self._update_voice_state(False, member.id, before.channel.id, server.server_id)

View File

@ -61,18 +61,23 @@ class BootLog(ModuleABC, OnReadyABC):
g: guild = g
self._logger.debug(__name__, f'Server detected: {g.id}')
server_settings: ServerSettings = self._config.get_configuration(
f'DSERVER_{g.id}')
server_settings: ServerSettings = self._config.get_configuration(f'DSERVER_{g.id}')
if server_settings is None:
self._logger.error(
__name__, f'Config for server {g.id} not found!')
self._logger.error(__name__, f'Config for server {g.id} not found!')
await self._bot.close()
return
module_settings: BootLogSettings = self._config.get_configuration(f'{type(self).__name__}_{g.id}')
if module_settings is None:
self._logger.error(__name__, f'Config {type(self).__name__}_{g.id} not found!')
await self._bot.close()
return
await self._message_service.send_channel_message(
self._bot.get_channel(
server_settings.login_message_channel_id),
server_settings.login_message.format(init_time)
module_settings.login_message_channel_id
),
module_settings.login_message.format(init_time)
)
self._logger.trace(__name__, f'Module {type(self)} stopped')