forked from sh-edraft.de/sh_discord_bot
Merge branch '0.2' into #42
This commit is contained in:
60
src/bot_core/abc/custom_file_logger_abc.py
Normal file
60
src/bot_core/abc/custom_file_logger_abc.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
from cpl_core.logging import LoggingSettings, Logger, LoggingLevelEnum, LoggerABC
|
||||
from cpl_core.time import TimeFormatSettings
|
||||
|
||||
from bot_core.configuration.file_logging_settings import FileLoggingSettings
|
||||
|
||||
|
||||
class CustomFileLoggerABC(Logger, ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self, key: str, config: ConfigurationABC, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC):
|
||||
self._key = key
|
||||
settings: LoggingSettings = config.get_configuration(f'{FileLoggingSettings.__name__}_{key}')
|
||||
Logger.__init__(self, settings, time_format, env)
|
||||
self._begin_log()
|
||||
|
||||
def _begin_log(self):
|
||||
console_level = self._console.value
|
||||
self._console = LoggingLevelEnum.OFF
|
||||
self.info(__name__, f'Starting...')
|
||||
self._console = LoggingLevelEnum(console_level)
|
||||
|
||||
def _get_string(self, name_list_as_str: str, level: LoggingLevelEnum, message: str) -> str:
|
||||
names = name_list_as_str.split(' ')
|
||||
log_level = level.name
|
||||
string = f'<{self._get_datetime_now()}> [ {log_level} ]'
|
||||
for name in names:
|
||||
string += f' [ {name} ]'
|
||||
string += f': {message}'
|
||||
return string
|
||||
|
||||
def header(self, string: str):
|
||||
super().header(string)
|
||||
|
||||
def trace(self, name: str, message: str):
|
||||
name = f'{name} {self._key}'
|
||||
super().trace(name, message)
|
||||
|
||||
def debug(self, name: str, message: str):
|
||||
name = f'{name} {self._key}'
|
||||
super().debug(name, message)
|
||||
|
||||
def info(self, name: str, message: str):
|
||||
name = f'{name} {self._key}'
|
||||
super().info(name, message)
|
||||
|
||||
def warn(self, name: str, message: str):
|
||||
name = f'{name} {self._key}'
|
||||
super().warn(name, message)
|
||||
|
||||
def error(self, name: str, message: str, ex: Exception = None):
|
||||
name = f'{name} {self._key}'
|
||||
super().error(name, message, ex)
|
||||
|
||||
def fatal(self, name: str, message: str, ex: Exception = None):
|
||||
name = f'{name} {self._key}'
|
||||
super().fatal(name, message, ex)
|
33
src/bot_core/configuration/bot_logging_settings.py
Normal file
33
src/bot_core/configuration/bot_logging_settings.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import traceback
|
||||
|
||||
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl_core.console import Console, ForegroundColorEnum
|
||||
from cpl_query.extension import List
|
||||
|
||||
from bot_core.configuration.file_logging_settings import FileLoggingSettings
|
||||
|
||||
|
||||
class BotLoggingSettings(ConfigurationModelABC):
|
||||
|
||||
def __init__(self):
|
||||
ConfigurationModelABC.__init__(self)
|
||||
self._files: List[FileLoggingSettings] = List(FileLoggingSettings)
|
||||
|
||||
@property
|
||||
def files(self) -> List[FileLoggingSettings]:
|
||||
return self._files
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
files = List(FileLoggingSettings)
|
||||
for s in settings:
|
||||
st = FileLoggingSettings()
|
||||
settings[s]['Key'] = s
|
||||
st.from_dict(settings[s])
|
||||
files.append(st)
|
||||
self._files = files
|
||||
except Exception as e:
|
||||
Console.set_foreground_color(ForegroundColorEnum.red)
|
||||
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings')
|
||||
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
||||
Console.set_foreground_color(ForegroundColorEnum.default)
|
65
src/bot_core/configuration/feature_flags_settings.py
Normal file
65
src/bot_core/configuration/feature_flags_settings.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import traceback
|
||||
from typing import Optional
|
||||
|
||||
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl_core.console import Console
|
||||
|
||||
|
||||
class FeatureFlagsSettings(ConfigurationModelABC):
|
||||
|
||||
def __init__(self):
|
||||
ConfigurationModelABC.__init__(self)
|
||||
|
||||
self._admin_module = False # 02.10.2022 #48
|
||||
self._base_module = True # 02.10.2022 #48
|
||||
self._boot_log_module = True # 02.10.2022 #48
|
||||
self._database_module = True # 02.10.2022 #48
|
||||
self._moderator_module = False # 02.10.2022 #48
|
||||
self._permission_module = True # 02.10.2022 #48
|
||||
|
||||
@property
|
||||
def admin_module(self) -> bool:
|
||||
return self._admin_module
|
||||
|
||||
@property
|
||||
def base_module(self) -> bool:
|
||||
return self._base_module
|
||||
|
||||
@property
|
||||
def boot_log_module(self) -> bool:
|
||||
return self._boot_log_module
|
||||
|
||||
@property
|
||||
def database_module(self) -> bool:
|
||||
return self._database_module
|
||||
|
||||
@property
|
||||
def moderator_module(self) -> bool:
|
||||
return self._moderator_module
|
||||
|
||||
@property
|
||||
def permission_module(self) -> bool:
|
||||
return self._permission_module
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
if 'AdminModule' in settings:
|
||||
self._admin_module = settings['AdminModule']
|
||||
|
||||
if 'BaseModule' in settings:
|
||||
self._base_module = settings['BaseModule']
|
||||
|
||||
if 'BootLogModule' in settings:
|
||||
self._boot_log_module = settings['BootLogModule']
|
||||
|
||||
if 'DatabaseModule' in settings:
|
||||
self._database_module = settings['DatabaseModule']
|
||||
|
||||
if 'ModeratorModule' in settings:
|
||||
self._moderator_module = settings['ModeratorModule']
|
||||
|
||||
if 'PermissionModule' in settings:
|
||||
self._permission_module = settings['PermissionModule']
|
||||
except Exception as e:
|
||||
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings')
|
||||
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
24
src/bot_core/configuration/file_logging_settings.py
Normal file
24
src/bot_core/configuration/file_logging_settings.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import traceback
|
||||
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.logging import LoggingSettings
|
||||
|
||||
|
||||
class FileLoggingSettings(LoggingSettings):
|
||||
|
||||
def __init__(self):
|
||||
LoggingSettings.__init__(self)
|
||||
|
||||
self._key = ''
|
||||
|
||||
@property
|
||||
def key(self) -> str:
|
||||
return self._key
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
self._key = settings['Key']
|
||||
super().from_dict(settings)
|
||||
except Exception as e:
|
||||
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings')
|
||||
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
0
src/bot_core/helper/__init__.py
Normal file
0
src/bot_core/helper/__init__.py
Normal file
9
src/bot_core/helper/log_message_helper.py
Normal file
9
src/bot_core/helper/log_message_helper.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import discord
|
||||
|
||||
|
||||
class LogMessageHelper:
|
||||
|
||||
@staticmethod
|
||||
def get_log_string(message: discord.Message):
|
||||
content = message.content.replace("\n", "\n\t")
|
||||
return f'{message.author} @ {message.channel} -> \n\t{content}'
|
1
src/bot_core/logging/__init__.py
Normal file
1
src/bot_core/logging/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# imports
|
11
src/bot_core/logging/command_logger.py
Normal file
11
src/bot_core/logging/command_logger.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
from cpl_core.time import TimeFormatSettings
|
||||
|
||||
from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC
|
||||
|
||||
|
||||
class CommandLogger(CustomFileLoggerABC):
|
||||
|
||||
def __init__(self, config: ConfigurationABC, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC):
|
||||
CustomFileLoggerABC.__init__(self, 'Command', config, time_format, env)
|
11
src/bot_core/logging/database_logger.py
Normal file
11
src/bot_core/logging/database_logger.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
from cpl_core.time import TimeFormatSettings
|
||||
|
||||
from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC
|
||||
|
||||
|
||||
class DatabaseLogger(CustomFileLoggerABC):
|
||||
|
||||
def __init__(self, config: ConfigurationABC, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC):
|
||||
CustomFileLoggerABC.__init__(self, 'Database', config, time_format, env)
|
11
src/bot_core/logging/message_logger.py
Normal file
11
src/bot_core/logging/message_logger.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
from cpl_core.time import TimeFormatSettings
|
||||
|
||||
from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC
|
||||
|
||||
|
||||
class MessageLogger(CustomFileLoggerABC):
|
||||
|
||||
def __init__(self, config: ConfigurationABC, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC):
|
||||
CustomFileLoggerABC.__init__(self, 'Message', config, time_format, env)
|
@@ -4,19 +4,20 @@ from typing import Union
|
||||
import discord
|
||||
from cpl_core.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl_core.database.context.database_context_abc import DatabaseContextABC
|
||||
from cpl_core.logging import LoggerABC
|
||||
from cpl_discord.service import DiscordBotServiceABC
|
||||
from cpl_query.extension import List
|
||||
from discord.ext.commands import Context
|
||||
|
||||
from bot_core.abc.message_service_abc import MessageServiceABC
|
||||
from bot_core.configuration.server_settings import ServerSettings
|
||||
from bot_core.helper.log_message_helper import LogMessageHelper
|
||||
from bot_core.logging.message_logger import MessageLogger
|
||||
from bot_data.abc.client_repository_abc import ClientRepositoryABC
|
||||
|
||||
|
||||
class MessageService(MessageServiceABC):
|
||||
|
||||
def __init__(self, config: ConfigurationABC, logger: LoggerABC, bot: DiscordBotServiceABC, clients: ClientRepositoryABC, db: DatabaseContextABC):
|
||||
def __init__(self, config: ConfigurationABC, logger: MessageLogger, bot: DiscordBotServiceABC, clients: ClientRepositoryABC, db: DatabaseContextABC):
|
||||
self._config = config
|
||||
self._logger = logger
|
||||
self._bot = bot
|
||||
@@ -35,7 +36,7 @@ class MessageService(MessageServiceABC):
|
||||
server_st: ServerSettings = self._config.get_configuration(f'ServerSettings_{message.guild.id}')
|
||||
if not mass_delete:
|
||||
await asyncio.sleep(server_st.message_delete_timer)
|
||||
self._logger.debug(__name__, f'Try to delete message:\n\t{message}\n\t{message.content}')
|
||||
self._logger.debug(__name__, f'Try to delete message: {LogMessageHelper.get_log_string(message)}')
|
||||
guild_id = message.guild.id
|
||||
try:
|
||||
await message.delete()
|
||||
|
Reference in New Issue
Block a user