Added logic to check table at startup

This commit is contained in:
Sven Heidemann 2021-12-21 15:39:46 +01:00
parent 39079b2b58
commit 3eaae4679f
4 changed files with 69 additions and 2 deletions

View File

@ -23,7 +23,10 @@
"LoginMessage": "",
"MessageDeleteTimer": 0,
"WelcomeMessage": "",
"GoodbyeMessage": ""
"GoodbyeMessage": "",
"MaxVoiceStateHours": 0,
"XpPerMessage": 0,
"XpPerOntimeHour": 0
}
]
}

View File

@ -23,6 +23,7 @@ from gismo_data.abc.user_repository_abc import UserRepositoryABC
from gismo_data.db_context import DBContext
from gismo_data.migration.initial_migration import InitialMigration
from gismo_data.migration.migration_0_3 import Migration_0_3
from gismo_data.migration.migration_0_3_1 import Migration_0_3_1
from gismo_data.service.client_repository_service import (
ClientRepositoryABC, ClientRepositoryService)
from gismo_data.service.known_user_repository_service import \
@ -91,6 +92,7 @@ class Startup(StartupABC):
# migrations
services.add_transient(MigrationABC, InitialMigration)
services.add_transient(MigrationABC, Migration_0_3)
services.add_transient(MigrationABC, Migration_0_3_1)
provider: ServiceProviderABC = services.build_service_provider()

View File

@ -15,6 +15,9 @@ class ServerSettings(ConfigurationModelABC):
self._login_message: str = ''
self._welcome_message: str = ''
self._goodbye_message: str = ''
self._max_voice_state_hours: int = 0
self._xp_per_message: int = 0
self._xp_per_ontime_hour: int = 0
@property
def id(self) -> str:
@ -40,6 +43,18 @@ class ServerSettings(ConfigurationModelABC):
def goodbye_message(self) -> str:
return self._goodbye_message
@property
def max_voice_state_hours(self) -> int:
return self._max_voice_state_hours
@property
def xp_per_message(self) -> int:
return self._xp_per_message
@property
def xp_per_ontime_hour(self) -> int:
return self._xp_per_ontime_hour
def from_dict(self, settings: dict):
try:
self._id = int(settings['Id'])
@ -48,6 +63,9 @@ class ServerSettings(ConfigurationModelABC):
self._login_message = settings['LoginMessage']
self._welcome_message = settings['WelcomeMessage']
self._goodbye_message = settings['GoodbyeMessage']
self._max_voice_state_hours = int(settings['MaxVoiceStateHours'])
self._xp_per_message = int(settings['XpPerMessage'])
self._xp_per_ontime_hour = int(settings['XpPerOntimeHour'])
except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in settings')
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')

View File

@ -7,10 +7,12 @@ from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from gismo_core.abc.bot_service_abc import BotServiceABC
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.user_joined_server_repository_abc import \
UserJoinedServerRepositoryABC
from gismo_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC
from gismo_data.abc.user_repository_abc import UserRepositoryABC
from gismo_data.model.client import Client
from gismo_data.model.known_user import KnownUser
@ -34,7 +36,8 @@ class Database(ModuleABC, OnReadyABC):
user_repo: UserRepositoryABC,
client_repo: ClientRepositoryABC,
known_users: KnownUserRepositoryABC,
user_joins: UserJoinedServerRepositoryABC
user_joins: UserJoinedServerRepositoryABC,
user_joins_vc: UserJoinedVoiceChannelRepositoryABC
):
self._config = config
@ -46,6 +49,7 @@ class Database(ModuleABC, OnReadyABC):
self._clients = client_repo
self._known_users = known_users
self._user_joins = user_joins
self._user_joins_vc = user_joins_vc
ModuleABC.__init__(self)
self._priorities[OnReadyABC] = 0
@ -241,6 +245,45 @@ class Database(ModuleABC, OnReadyABC):
self._db_context.save_changes()
def _check_user_joins_vc(self):
self._logger.debug(__name__, f'Start checking UserJoinedVoiceChannel table')
for guild in self._bot.guilds:
guild: discord.Guild = guild
server = self._servers.find_server_by_discord_id(guild.id)
if server is None:
self._logger.fatal(__name__, f'Server not found in database: {guild.id}')
try:
for u in guild.members:
u: discord.User = u
if u.bot:
self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot')
continue
user = self._users.find_user_by_discord_id_and_server_id(u.id, server.server_id)
if user is None:
self._logger.fatal(__name__, f'User not found in database: {u.id}')
join = self._user_joins_vc.find_active_user_joined_voice_channel_by_user_id(user.user_id)
if join is None:
continue
m: discord.Member = u
self._logger.warn(__name__, f'Active UserJoinedVoiceChannel found in database: {guild.id}:{u.id}@{m.joined_at}')
join.leaved_on = datetime.now()
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{guild.id}')
if ((join.leaved_on - join.joined_on).total_seconds/60/60) > server_st.max_voice_state_hours:
join.leaved_on = join.joined_on + datetime.timedelta(hours=server_st.max_voice_state_hours)
self._user_joins_vc.update_user_joined_voice_channel(join)
# todo: maybe add XP
self._db_context.save_changes()
except Exception as e:
self._logger.error(__name__, f'Cannot get UserJoinedVoiceChannel', e)
async def on_ready(self):
self._logger.debug(__name__, f'Module {type(self)} started')
@ -249,6 +292,7 @@ class Database(ModuleABC, OnReadyABC):
self._check_clients()
self._check_users()
self._check_user_joins()
self._check_user_joins_vc()
self._validate_init_time()
self._logger.trace(__name__, f'Module {type(self)} stopped')