Added PyCharm support to database module

This commit is contained in:
Sven Heidemann 2022-01-08 19:14:58 +01:00
parent d83e842551
commit 7d4a28d524
4 changed files with 24 additions and 28 deletions

View File

@ -6,7 +6,7 @@ from cpl_core.configuration import ConfigurationModelABC
class ModuleABC(ABC): class ModuleABC(ABC):
@abstractmethod @abstractmethod
def __init__(self, priorities: dict[type, int], settings_type: type): def __init__(self, priorities: dict[type, int], settings_type: Optional[type]):
self._priorities = priorities self._priorities = priorities
self._success = True self._success = True
self._settings_type = settings_type self._settings_type = settings_type

View File

@ -14,7 +14,7 @@ class ServerSettings(ConfigurationModelABC):
self._bot_has_no_permission_message: str = '' self._bot_has_no_permission_message: str = ''
@property @property
def id(self) -> str: def id(self) -> int:
return self._id return self._id
@property @property

View File

@ -23,6 +23,7 @@ from gismo_data.model.user_joined_voice_channel import UserJoinedVoiceChannel
from gismo_data.service.user_repository_service import ServerRepositoryABC from gismo_data.service.user_repository_service import ServerRepositoryABC
from gismo_core.abc.events.on_ready_abc import OnReadyABC from gismo_core.abc.events.on_ready_abc import OnReadyABC
from gismo_core.abc.module_abc import ModuleABC from gismo_core.abc.module_abc import ModuleABC
from modules.base.base_settings import BaseSettings
class Database(ModuleABC, OnReadyABC): class Database(ModuleABC, OnReadyABC):
@ -54,7 +55,7 @@ class Database(ModuleABC, OnReadyABC):
ModuleABC.__init__( ModuleABC.__init__(
self, self,
{ OnReadyABC: 0 }, {OnReadyABC: 0},
None None
) )
self._logger.info(__name__, f'Module {type(self)} loaded') self._logger.info(__name__, f'Module {type(self)} loaded')
@ -63,7 +64,7 @@ class Database(ModuleABC, OnReadyABC):
try: try:
start_time = self._config.get_configuration('Database_StartTime') start_time = self._config.get_configuration('Database_StartTime')
init_time = round((datetime.now() - start_time).total_seconds(), 2) init_time = round((datetime.now() - start_time).total_seconds(), 2)
self._config.add_configuration('Database_InitTime', init_time) self._config.add_configuration('Database_InitTime', str(init_time))
self._logger.debug(__name__, f'Database Init time: {init_time}s') self._logger.debug(__name__, f'Database Init time: {init_time}s')
# print warning if initialisation took too long # print warning if initialisation took too long
if init_time >= 30: if init_time >= 30:
@ -231,7 +232,6 @@ class Database(ModuleABC, OnReadyABC):
if results is None or len(results) == 0: if results is None or len(results) == 0:
self._logger.error(__name__, f'Table Users is empty!') self._logger.error(__name__, f'Table Users is empty!')
joins = self._user_joins.get_user_joined_servers() joins = self._user_joins.get_user_joined_servers()
for join in joins: for join in joins:
join: UserJoinedServer = join join: UserJoinedServer = join
@ -259,50 +259,46 @@ class Database(ModuleABC, OnReadyABC):
self._logger.fatal(__name__, f'Server not found in database: {guild.id}') self._logger.fatal(__name__, f'Server not found in database: {guild.id}')
try: try:
for u in guild.members: for member in guild.members:
u: discord.User = u if member.bot:
if u.bot: self._logger.trace(__name__, f'User {member.id} is ignored, because its a bot')
self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot')
continue continue
user = self._users.find_user_by_discord_id_and_server_id(u.id, server.server_id) user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id)
if user is None: if user is None:
self._logger.fatal(__name__, f'User not found in database: {u.id}') self._logger.fatal(__name__, f'User not found in database: {member.id}')
join = self._user_joins_vc.find_active_user_joined_voice_channel_by_user_id(user.user_id) join = self._user_joins_vc.find_active_user_joined_voice_channel_by_user_id(user.user_id)
if join is None: if join is None:
continue continue
m: discord.Member = u self._logger.warn(__name__, f'Active UserJoinedVoiceChannel found in database: {guild.id}:{member.id}@{join.joined_on}')
self._logger.warn(__name__, f'Active UserJoinedVoiceChannel found in database: {guild.id}:{u.id}@{m.joined_at}')
join.leaved_on = datetime.now() join.leaved_on = datetime.now()
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{guild.id}') settings: BaseSettings = 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: if ((join.leaved_on - join.joined_on).total_seconds()/60/60) > settings.max_voice_state_hours:
join.leaved_on = join.joined_on + datetime.timedelta(hours=server_st.max_voice_state_hours) join.leaved_on = join.joined_on + datetime.timedelta(hours=settings.max_voice_state_hours)
self._user_joins_vc.update_user_joined_voice_channel(join) self._user_joins_vc.update_user_joined_voice_channel(join)
# todo: maybe add XP # todo: maybe add XP
self._db_context.save_changes() self._db_context.save_changes()
for u in guild.members: for member in guild.members:
if u.bot: if member.bot:
self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot') self._logger.trace(__name__, f'User {member.id} is ignored, because its a bot')
continue continue
m: discord.Member = u if member.voice is None:
if m.voice is None:
continue continue
user = self._users.find_user_by_discord_id_and_server_id(u.id, server.server_id) user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id)
if user is None: if user is None:
self._logger.fatal(__name__, f'User not found in database: {u.id}') self._logger.fatal(__name__, f'User not found in database: {member.id}')
join = UserJoinedVoiceChannel(user, m.voice.channel.id, datetime.now()) join = UserJoinedVoiceChannel(user, member.voice.channel.id, datetime.now())
self._user_joins_vc.add_user_joined_voice_channel(join) self._user_joins_vc.add_user_joined_voice_channel(join)
self._db_context.save_changes() self._db_context.save_changes()
self._logger.warn(__name__, f'VS {m.voice}') self._logger.warn(__name__, f'VS {member.voice}')
except Exception as e: except Exception as e:
self._logger.error(__name__, f'Cannot get UserJoinedVoiceChannel', e) self._logger.error(__name__, f'Cannot get UserJoinedVoiceChannel', e)

View File

@ -14,4 +14,4 @@ class DatabaseExtension(ApplicationExtensionABC):
async def run(self, config: ConfigurationABC, services: ServiceProviderABC): async def run(self, config: ConfigurationABC, services: ServiceProviderABC):
logger: LoggerABC = services.get_service(LoggerABC) logger: LoggerABC = services.get_service(LoggerABC)
logger.debug(__name__, 'Database extension started') logger.debug(__name__, 'Database extension started')
config.add_configuration('Database_StartTime', datetime.now()) config.add_configuration('Database_StartTime', str(datetime.now()))