Merge branch '0.2' into #42
This commit is contained in:
		| @@ -1,5 +1,4 @@ | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.command import DiscordCommandABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from cpl_translation import TranslatePipe | ||||
| @@ -8,6 +7,7 @@ from discord.ext.commands import Context | ||||
|  | ||||
| from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC | ||||
| from bot_core.abc.message_service_abc import MessageServiceABC | ||||
| from bot_core.logging.command_logger import CommandLogger | ||||
| from modules.permission.abc.permission_service_abc import PermissionServiceABC | ||||
|  | ||||
|  | ||||
| @@ -15,7 +15,7 @@ class RestartCommand(DiscordCommandABC): | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             logger: LoggerABC, | ||||
|             logger: CommandLogger, | ||||
|             config: ConfigurationABC, | ||||
|             message_service: MessageServiceABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
|   | ||||
| @@ -1,5 +1,4 @@ | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.command import DiscordCommandABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from cpl_translation import TranslatePipe | ||||
| @@ -8,6 +7,7 @@ from discord.ext.commands import Context | ||||
|  | ||||
| from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC | ||||
| from bot_core.abc.message_service_abc import MessageServiceABC | ||||
| from bot_core.logging.command_logger import CommandLogger | ||||
| from modules.permission.abc.permission_service_abc import PermissionServiceABC | ||||
|  | ||||
|  | ||||
| @@ -15,7 +15,7 @@ class ShutdownCommand(DiscordCommandABC): | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             logger: LoggerABC, | ||||
|             logger: CommandLogger, | ||||
|             config: ConfigurationABC, | ||||
|             message_service: MessageServiceABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
|   | ||||
| @@ -1,5 +1,4 @@ | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.command import DiscordCommandABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from cpl_translation import TranslatePipe | ||||
| @@ -9,6 +8,7 @@ from discord.ext.commands import Context | ||||
|  | ||||
| from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC | ||||
| from bot_core.abc.message_service_abc import MessageServiceABC | ||||
| from bot_core.logging.command_logger import CommandLogger | ||||
| from modules.base.configuration.base_server_settings import BaseServerSettings | ||||
|  | ||||
|  | ||||
| @@ -16,7 +16,7 @@ class AFKCommand(DiscordCommandABC): | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             logger: LoggerABC, | ||||
|             logger: CommandLogger, | ||||
|             config: ConfigurationABC, | ||||
|             message_service: MessageServiceABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
|   | ||||
| @@ -1,5 +1,4 @@ | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.command import DiscordCommandABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from discord.ext import commands | ||||
| @@ -7,6 +6,7 @@ from discord.ext.commands import Context | ||||
|  | ||||
| from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC | ||||
| from bot_core.abc.message_service_abc import MessageServiceABC | ||||
| from bot_core.logging.command_logger import CommandLogger | ||||
| from modules.base.configuration.base_server_settings import BaseServerSettings | ||||
|  | ||||
|  | ||||
| @@ -15,7 +15,7 @@ class HelpCommand(DiscordCommandABC): | ||||
|     def __init__( | ||||
|             self, | ||||
|             config: ConfigurationABC, | ||||
|             logger: LoggerABC, | ||||
|             logger: CommandLogger, | ||||
|             message_service: MessageServiceABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
|             client_utils: ClientUtilsServiceABC | ||||
|   | ||||
| @@ -2,7 +2,6 @@ from datetime import datetime | ||||
|  | ||||
| import discord | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.command import DiscordCommandABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from cpl_translation import TranslatePipe | ||||
| @@ -12,6 +11,7 @@ from discord.ext.commands import Context | ||||
| import bot | ||||
| from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC | ||||
| from bot_core.abc.message_service_abc import MessageServiceABC | ||||
| from bot_core.logging.command_logger import CommandLogger | ||||
|  | ||||
|  | ||||
| class InfoCommand(DiscordCommandABC): | ||||
| @@ -19,7 +19,7 @@ class InfoCommand(DiscordCommandABC): | ||||
|     def __init__( | ||||
|             self, | ||||
|             config: ConfigurationABC, | ||||
|             logger: LoggerABC, | ||||
|             logger: CommandLogger, | ||||
|             message_service: MessageServiceABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
|             client_utils: ClientUtilsServiceABC, | ||||
|   | ||||
| @@ -1,4 +1,3 @@ | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.command import DiscordCommandABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from cpl_translation import TranslatePipe | ||||
| @@ -7,13 +6,14 @@ from discord.ext.commands import Context | ||||
|  | ||||
| from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC | ||||
| from bot_core.abc.message_service_abc import MessageServiceABC | ||||
| from bot_core.logging.command_logger import CommandLogger | ||||
|  | ||||
|  | ||||
| class PingCommand(DiscordCommandABC): | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             logger: LoggerABC, | ||||
|             logger: CommandLogger, | ||||
|             message_service: MessageServiceABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
|             client_utils: ClientUtilsServiceABC, | ||||
|   | ||||
| @@ -2,7 +2,6 @@ from typing import Optional | ||||
|  | ||||
| import discord | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.command import DiscordCommandABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from cpl_translation import TranslatePipe | ||||
| @@ -11,6 +10,7 @@ from discord.ext.commands import Context | ||||
|  | ||||
| from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC | ||||
| from bot_core.abc.message_service_abc import MessageServiceABC | ||||
| from bot_core.logging.command_logger import CommandLogger | ||||
| from bot_core.pipes.date_time_offset_pipe import DateTimeOffsetPipe | ||||
| from bot_data.abc.server_repository_abc import ServerRepositoryABC | ||||
| from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC | ||||
| @@ -23,7 +23,7 @@ class UserInfoCommand(DiscordCommandABC): | ||||
|     def __init__( | ||||
|             self, | ||||
|             config: ConfigurationABC, | ||||
|             logger: LoggerABC, | ||||
|             logger: CommandLogger, | ||||
|             message_service: MessageServiceABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
|             client_utils: ClientUtilsServiceABC, | ||||
|   | ||||
| @@ -2,6 +2,7 @@ import traceback | ||||
|  | ||||
| from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC | ||||
| from cpl_core.console import Console | ||||
| from cpl_query.extension import List | ||||
|  | ||||
|  | ||||
| class BaseServerSettings(ConfigurationModelABC): | ||||
| @@ -13,7 +14,9 @@ class BaseServerSettings(ConfigurationModelABC): | ||||
|         self._max_voice_state_hours: int = 0 | ||||
|         self._xp_per_message: int = 0 | ||||
|         self._xp_per_ontime_hour: int = 0 | ||||
|         self._afk_channel_ids: list[int] = [] | ||||
|         self._afk_channel_ids: List[int] = List(int) | ||||
|         self._afk_command_channel_id: int = 0 | ||||
|         self._help_command_reference_url: str = '' | ||||
|  | ||||
|     @property | ||||
|     def id(self) -> int: | ||||
| @@ -32,7 +35,7 @@ class BaseServerSettings(ConfigurationModelABC): | ||||
|         return self._xp_per_ontime_hour | ||||
|  | ||||
|     @property | ||||
|     def afk_channel_ids(self) -> list[int]: | ||||
|     def afk_channel_ids(self) -> List[int]: | ||||
|         return self._afk_channel_ids | ||||
|  | ||||
|     @property | ||||
|   | ||||
							
								
								
									
										90
									
								
								src/modules/base/events/base_on_command_event.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								src/modules/base/events/base_on_command_event.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | ||||
| from typing import Optional | ||||
|  | ||||
| import discord | ||||
| from cpl_core.database.context import DatabaseContextABC | ||||
| from cpl_core.time import TimeFormatSettings | ||||
| from cpl_discord.events import OnCommandABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from cpl_translation import TranslatePipe | ||||
| from discord.ext.commands import Context | ||||
|  | ||||
| from bot_core.abc.message_service_abc import MessageServiceABC | ||||
| from bot_core.configuration.bot_settings import BotSettings | ||||
| from bot_core.logging.command_logger import CommandLogger | ||||
| from bot_data.abc.client_repository_abc import ClientRepositoryABC | ||||
| from bot_data.abc.server_repository_abc import ServerRepositoryABC | ||||
| from bot_data.abc.user_repository_abc import UserRepositoryABC | ||||
| from bot_data.model.user import User | ||||
| from modules.base.abc.base_helper_abc import BaseHelperABC | ||||
| from modules.base.configuration.base_server_settings import BaseServerSettings | ||||
|  | ||||
|  | ||||
| class BaseOnCommandEvent(OnCommandABC): | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             logger: CommandLogger, | ||||
|             bot: DiscordBotServiceABC, | ||||
|             messenger: MessageServiceABC, | ||||
|             bot_settings: BotSettings, | ||||
|             time_format_settings: TimeFormatSettings, | ||||
|             translate: TranslatePipe, | ||||
|             bhs: BaseHelperABC, | ||||
|             db: DatabaseContextABC, | ||||
|             users: UserRepositoryABC, | ||||
|             clients: ClientRepositoryABC, | ||||
|             servers: ServerRepositoryABC, | ||||
|     ): | ||||
|         OnCommandABC.__init__(self) | ||||
|         self._logger = logger | ||||
|         self._bot = bot | ||||
|         self._messenger = messenger | ||||
|         self._bot_settings = bot_settings | ||||
|         self._time_format_settings = time_format_settings | ||||
|         self._t = translate | ||||
|         self._base_helper = bhs | ||||
|         self._db = db | ||||
|         self._users = users | ||||
|         self._clients = clients | ||||
|         self._servers = servers | ||||
|  | ||||
|     def _append_received_message_count(self, g_id: int): | ||||
|         try: | ||||
|             self._clients.append_received_message_count(self._bot.user.id, g_id, 1) | ||||
|             self._db.save_changes() | ||||
|         except Exception as e: | ||||
|             self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}', e) | ||||
|  | ||||
|     def _handle_message_for_xp(self, message: discord.Message): | ||||
|         dc_user_id = message.author.id | ||||
|         try: | ||||
|             server = self._servers.get_server_by_discord_id(message.guild.id) | ||||
|         except Exception as e: | ||||
|             self._logger.error(__name__, f'Cannot get server {message.guild.id}', e) | ||||
|             return | ||||
|  | ||||
|         user: Optional[User] = None | ||||
|         try: | ||||
|             user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, server.server_id) | ||||
|         except Exception as e: | ||||
|             self._logger.error(__name__, f'Cannot get user {dc_user_id}', e) | ||||
|             return | ||||
|  | ||||
|         if user is None: | ||||
|             self._logger.error(__name__, f'User not found {dc_user_id}') | ||||
|             return | ||||
|  | ||||
|         settings: BaseServerSettings = self._base_helper.get_config(message.guild.id) | ||||
|         old_xp = user.xp | ||||
|         user.xp += settings.xp_per_message | ||||
|         self._users.update_user(user) | ||||
|         self._db.save_changes() | ||||
|  | ||||
|         self._logger.debug(__name__, f'User {user} sent message. xp: from {old_xp} to {user.xp}') | ||||
|  | ||||
|     async def on_command(self, ctx: Context): | ||||
|         self._logger.debug(__name__, f'Module {type(self)} started') | ||||
|         self._logger.info(__name__, f'Received command: {ctx.command} from {ctx.channel}') | ||||
|         if ctx is None or ctx.guild is None: | ||||
|             return | ||||
|         self._append_received_message_count(ctx.guild.id) | ||||
| @@ -2,10 +2,11 @@ from typing import Optional | ||||
|  | ||||
| import discord | ||||
| from cpl_core.database.context import DatabaseContextABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.events import OnMessageABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
|  | ||||
| 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 | ||||
| from bot_data.abc.server_repository_abc import ServerRepositoryABC | ||||
| from bot_data.abc.user_repository_abc import UserRepositoryABC | ||||
| @@ -18,7 +19,7 @@ class BaseOnMessageEvent(OnMessageABC): | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             logger: LoggerABC, | ||||
|             logger: MessageLogger, | ||||
|             bhs: BaseHelperABC, | ||||
|             db: DatabaseContextABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
| @@ -71,6 +72,7 @@ class BaseOnMessageEvent(OnMessageABC): | ||||
|  | ||||
|     async def on_message(self, message: discord.Message): | ||||
|         self._logger.debug(__name__, f'Module {type(self)} started') | ||||
|         self._logger.info(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}') | ||||
|         if message is None or message.guild is None: | ||||
|             return | ||||
|         self._append_received_message_count(message.guild.id) | ||||
|   | ||||
| @@ -5,6 +5,8 @@ from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.dependency_injection import ServiceProviderABC | ||||
| from cpl_core.logging import LoggerABC | ||||
|  | ||||
| from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings | ||||
|  | ||||
|  | ||||
| class BootLogExtension(ApplicationExtensionABC): | ||||
|  | ||||
| @@ -12,6 +14,9 @@ class BootLogExtension(ApplicationExtensionABC): | ||||
|         pass | ||||
|  | ||||
|     async def run(self, config: ConfigurationABC, services: ServiceProviderABC): | ||||
|         feature_flags: FeatureFlagsSettings = config.get_configuration(FeatureFlagsSettings) | ||||
|         if not feature_flags.boot_log_module: | ||||
|             return | ||||
|         logger: LoggerABC = services.get_service(LoggerABC) | ||||
|         logger.debug(__name__, 'BootLog extension started') | ||||
|         config.add_configuration('Bot_StartTime', str(datetime.now())) | ||||
|   | ||||
| @@ -42,12 +42,12 @@ class BootLogOnReadyEvent(OnReadyABC): | ||||
|             # print warning if initialisation took too long | ||||
|             if init_time >= 30: | ||||
|                 self._logger.warn( | ||||
|                     __name__, 'It takes long time to start the bot!') | ||||
|                     __name__, 'It takes too long to start the bot!') | ||||
|  | ||||
|             # print error if initialisation took way too long | ||||
|             elif init_time >= 90: | ||||
|                 self._logger.error( | ||||
|                     __name__, 'It takes very long time to start the bot!!!') | ||||
|                     __name__, 'It takes far too long to start the bot!!!') | ||||
|         except Exception as e: | ||||
|             self._logger.error(__name__, 'Init time calculation failed', e) | ||||
|             return | ||||
|   | ||||
| @@ -5,6 +5,8 @@ from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.dependency_injection import ServiceProviderABC | ||||
| from cpl_core.logging import LoggerABC | ||||
|  | ||||
| from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings | ||||
| from bot_core.logging.database_logger import DatabaseLogger | ||||
| from bot_data.service.migration_service import MigrationService | ||||
|  | ||||
|  | ||||
| @@ -14,7 +16,10 @@ class DatabaseExtension(ApplicationExtensionABC): | ||||
|         pass | ||||
|  | ||||
|     async def run(self, config: ConfigurationABC, services: ServiceProviderABC): | ||||
|         logger: LoggerABC = services.get_service(LoggerABC) | ||||
|         feature_flags: FeatureFlagsSettings = config.get_configuration(FeatureFlagsSettings) | ||||
|         if not feature_flags.database_module: | ||||
|             return | ||||
|         logger: LoggerABC = services.get_service(DatabaseLogger) | ||||
|         logger.debug(__name__, 'Database extension started') | ||||
|         config.add_configuration('Database_StartTime', str(datetime.now())) | ||||
|         migrations: MigrationService = services.get_service(MigrationService) | ||||
|   | ||||
| @@ -4,10 +4,10 @@ from datetime import datetime, timedelta | ||||
| import discord | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.database.context import DatabaseContextABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.events import OnReadyABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
|  | ||||
| from bot_core.logging.database_logger import DatabaseLogger | ||||
| from bot_core.pipes.date_time_offset_pipe import DateTimeOffsetPipe | ||||
| from bot_data.abc.client_repository_abc import ClientRepositoryABC | ||||
| from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC | ||||
| @@ -29,7 +29,7 @@ class DatabaseOnReadyEvent(OnReadyABC): | ||||
|     def __init__( | ||||
|         self, | ||||
|         config: ConfigurationABC, | ||||
|         logger: LoggerABC, | ||||
|         logger: DatabaseLogger, | ||||
|         bot: DiscordBotServiceABC, | ||||
|         db_context: DatabaseContextABC, | ||||
|         server_repo: ServerRepositoryABC, | ||||
| @@ -65,12 +65,12 @@ class DatabaseOnReadyEvent(OnReadyABC): | ||||
|             # print warning if initialisation took too long | ||||
|             if init_time >= 30: | ||||
|                 self._logger.warn( | ||||
|                     __name__, 'It takes long time to start the bot!') | ||||
|                     __name__, 'It takes too long to prepare the database!') | ||||
|  | ||||
|             # print error if initialisation took way too long | ||||
|             elif init_time >= 90: | ||||
|                 self._logger.error( | ||||
|                     __name__, 'It takes very long time to start the bot!!!') | ||||
|                     __name__, 'It takes far too long to prepare the database!!!') | ||||
|         except Exception as e:  # | ||||
|             self._logger.error(__name__, 'Database init time calculation failed', e) | ||||
|             return | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| import asyncio | ||||
|  | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.command import DiscordCommandABC | ||||
| from cpl_translation import TranslatePipe | ||||
| from discord.ext import commands | ||||
| @@ -10,6 +9,7 @@ from discord.ext.commands import Context | ||||
| from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC | ||||
| from bot_core.abc.message_service_abc import MessageServiceABC | ||||
| from bot_core.configuration.server_settings import ServerSettings | ||||
| from bot_core.logging.command_logger import CommandLogger | ||||
| from modules.permission.abc.permission_service_abc import PermissionServiceABC | ||||
|  | ||||
|  | ||||
| @@ -17,7 +17,7 @@ class PurgeCommand(DiscordCommandABC): | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             logger: LoggerABC, | ||||
|             logger: CommandLogger, | ||||
|             config: ConfigurationABC, | ||||
|             message_service: MessageServiceABC, | ||||
|             permissions: PermissionServiceABC, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user