From ed2dab6ff8af66f620dc2798911ea98d88941d75 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 31 Dec 2021 11:42:09 +0100 Subject: [PATCH] Added logic to add commands --- src/gismo/startup.py | 6 ++++- src/gismo_core/abc/command_abc.py | 11 ++++++++ src/gismo_core/commands_meta.py | 5 ++++ src/gismo_core/service/message_service.py | 11 +++++--- src/gismo_core/service/module_service.py | 8 +++--- src/modules/base/base.py | 20 ++++++++------ src/modules/base/service/__init__.py | 1 + .../base/service/base_command_service.py | 27 +++++++++++++++++++ src/modules/boot_log/boot_log.py | 1 + 9 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 src/gismo_core/abc/command_abc.py create mode 100644 src/gismo_core/commands_meta.py create mode 100644 src/modules/base/service/__init__.py create mode 100644 src/modules/base/service/base_command_service.py diff --git a/src/gismo/startup.py b/src/gismo/startup.py index aaf9ba3..491dfae 100644 --- a/src/gismo/startup.py +++ b/src/gismo/startup.py @@ -11,6 +11,7 @@ from cpl_core.environment import ApplicationEnvironment from cpl_core.logging import LoggerABC from gismo_core.abc.bot_service_abc import BotServiceABC +from gismo_core.abc.command_abc import CommandABC from gismo_core.abc.message_service_abc import MessageServiceABC from gismo_core.service.bot_service import BotService from gismo_core.service.message_service import MessageService @@ -35,6 +36,7 @@ from gismo_data.service.user_joined_server_repository_service import UserJoinedS from gismo_data.service.user_joined_voice_channel_service import UserJoinedVoiceChannelRepositoryService from gismo_data.service.user_repository_service import UserRepositoryService from modules.base.base import Base +from modules.base.service.base_command_service import BaseCommandService from modules.boot_log.boot_log import BootLog from modules.database.database import Database from modules.permission.abc.permission_service_abc import PermissionServiceABC @@ -88,12 +90,14 @@ class Startup(StartupABC): # module services services.add_singleton(PermissionServiceABC, PermissionService) + # commands + services.add_singleton(CommandABC, BaseCommandService) + # modules services.add_transient(ModuleABC, Database) services.add_transient(ModuleABC, BootLog) services.add_singleton(ModuleABC, Permission) services.add_singleton(ModuleABC, Base) - # migrations services.add_transient(MigrationABC, InitialMigration) diff --git a/src/gismo_core/abc/command_abc.py b/src/gismo_core/abc/command_abc.py new file mode 100644 index 0000000..350f501 --- /dev/null +++ b/src/gismo_core/abc/command_abc.py @@ -0,0 +1,11 @@ +from abc import ABC, abstractmethod + +from discord.ext import commands + +from gismo_core.commands_meta import CommandsMeta + + +class CommandABC(ABC, commands.Cog, metaclass=CommandsMeta): + + @abstractmethod + def __init__(self): pass diff --git a/src/gismo_core/commands_meta.py b/src/gismo_core/commands_meta.py new file mode 100644 index 0000000..8b66f16 --- /dev/null +++ b/src/gismo_core/commands_meta.py @@ -0,0 +1,5 @@ +from abc import ABCMeta +from discord.ext import commands + + +class CommandsMeta(ABCMeta, commands.CogMeta): pass diff --git a/src/gismo_core/service/message_service.py b/src/gismo_core/service/message_service.py index b86a712..d31a733 100644 --- a/src/gismo_core/service/message_service.py +++ b/src/gismo_core/service/message_service.py @@ -26,12 +26,15 @@ class MessageService(MessageServiceABC): async def delete_messages(self, messages: List[discord.Message]): self._logger.debug(__name__, f'Try to delete {messages.count()} messages') for message in messages: - await self.delete_message(message) + server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.guild.id}') + await asyncio.sleep(server_st.message_delete_timer) + await self.delete_message(message, mass_delete=True) self._logger.debug(__name__, 'Deleting messages finished') - async def delete_message(self, message: discord.Message): + async def delete_message(self, message: discord.Message, mass_delete=False): server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.guild.id}') - await asyncio.sleep(server_st.message_delete_timer) + 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}') guild_id = message.guild.id try: @@ -86,4 +89,4 @@ class MessageService(MessageServiceABC): self._logger.info(__name__, f'Sent message to channel {ctx.channel.id}') self._clients.apppend_sent_message_count(self._bot.user.id, ctx.guild.id, 1) self._db.save_changes() - await self.delete_message(msg) + await self.delete_messages(List(discord.Message, [msg, ctx.message])) diff --git a/src/gismo_core/service/module_service.py b/src/gismo_core/service/module_service.py index 4934932..cb870df 100644 --- a/src/gismo_core/service/module_service.py +++ b/src/gismo_core/service/module_service.py @@ -59,12 +59,10 @@ from gismo_core.abc.events.on_voice_state_update_abc import OnVoiceStateUpdateAB from gismo_core.abc.events.on_webhooks_update_abc import OnWebhooksUpdateABC from gismo_core.abc.module_abc import ModuleABC from gismo_core.abc.module_service_abc import ModuleServiceABC +from gismo_core.commands_meta import CommandsMeta -class _MetaCogABC(ABCMeta, commands.CogMeta): pass - - -class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC): +class ModuleService(ModuleServiceABC, commands.Cog, metaclass=CommandsMeta): def __init__(self, logger: LoggerABC, config: ConfigurationABC, services: ServiceProviderABC, env: ApplicationEnvironmentABC): self._logger = logger @@ -90,7 +88,7 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC): settings: ConfigurationModelABC = module.settings_type() settings.from_dict(json_cfg[id]) self._config.add_configuration(f'{type(module).__name__}_{id}', settings) - self._logger.info(__name__, f'Added config: {type(module).__name__}_{id}') + self._logger.debug(__name__, f'Added config: {type(module).__name__}_{id}') modules.append(module) diff --git a/src/modules/base/base.py b/src/modules/base/base.py index 0f4a1fc..9666eb9 100644 --- a/src/modules/base/base.py +++ b/src/modules/base/base.py @@ -16,10 +16,12 @@ from gismo_data.abc.user_joined_server_repository_abc import \ from gismo_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC from gismo_data.abc.user_repository_abc import UserRepositoryABC from gismo_data.model.known_user import KnownUser +from gismo_data.model.server import Server from gismo_data.model.user import User from gismo_data.model.user_joined_server import UserJoinedServer from gismo_data.model.user_joined_voice_channel import UserJoinedVoiceChannel from modules.base.base_settings import BaseSettings +from modules.base.service.base_command_service import BaseCommandService from modules.permission.abc.permission_service_abc import PermissionServiceABC from gismo_core.abc.events.on_member_join_abc import OnMemberJoinABC from gismo_core.abc.events.on_member_remove_abc import OnMemberRemoveABC @@ -44,7 +46,8 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS bot: BotServiceABC, db: DatabaseContextABC, messenger: MessageServiceABC, - permission_service: PermissionServiceABC + permission_service: PermissionServiceABC, + base_command_service: BaseCommandService ): self._config = config self._logger = logger @@ -69,6 +72,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS }, BaseSettings ) + self._bot.add_cog(base_command_service) self._logger.info(__name__, f'Module {type(self)} loaded') def _get_config(self, g_id: int) -> BaseSettings: @@ -103,7 +107,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: BaseSettings = self._get_config() + settings: BaseSettings = self._get_config(member.guild.id) await self._messenger.send_dm_message(settings.welcome_message.format(member.guild.name), member) for admin in self._permission_service.get_admins(): @@ -131,7 +135,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: BaseSettings = self._get_config() + settings: BaseSettings = self._get_config(member.guild.id) await self._messenger.send_dm_message(settings.goodbye_message, member) try: @@ -149,10 +153,10 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS except Exception as e: self._logger.error(__name__, f'Cannot get user {member.id}', e) - def _update_voice_state(self, joined: bool, dc_user_id: int, dc_channel_id: int, srv_id: int): + def _update_voice_state(self, joined: bool, dc_user_id: int, dc_channel_id: int, server: Server): user: User = None try: - user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, srv_id) + 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 @@ -168,7 +172,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS self._db.save_changes() return - settings: BaseSettings = self._get_config() + settings: BaseSettings = self._get_config(server.discord_server_id) join = self._user_joins_vc.get_active_user_joined_voice_channel_by_user_id(user.user_id) join.leaved_on = datetime.now() @@ -205,7 +209,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS self._logger.error(__name__, f'User not found {dc_user_id}') return - settings: BaseSettings = self._get_config() + settings: BaseSettings = self._get_config(message.guild.id) old_xp = user.xp user.xp += settings._xp_per_message self._users.update_user(user) @@ -235,7 +239,7 @@ 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 - settings: BaseSettings = self._get_config() + settings: BaseSettings = self._get_config(member.guild.id) server = self._servers.get_server_by_discord_id(member.guild.id) try: diff --git a/src/modules/base/service/__init__.py b/src/modules/base/service/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/modules/base/service/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/modules/base/service/base_command_service.py b/src/modules/base/service/base_command_service.py new file mode 100644 index 0000000..9ea4ee4 --- /dev/null +++ b/src/modules/base/service/base_command_service.py @@ -0,0 +1,27 @@ +import discord +from discord.ext import commands +from discord.ext.commands import Context + +from cpl_core.logging import LoggerABC +from gismo_core.abc.command_abc import CommandABC +from gismo_core.abc.message_service_abc import MessageServiceABC + + +class BaseCommandService(CommandABC): + + def __init__( + self, + logger: LoggerABC, + message_service: MessageServiceABC + ): + CommandABC.__init__(self) + + self._logger = logger + self._message_service = message_service + + self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + + @commands.command() + async def ping(self, ctx: Context): + self._logger.debug(__name__, f'Received command ping {ctx}') + await self._message_service.send_ctx_msg(ctx, 'Pong') diff --git a/src/modules/boot_log/boot_log.py b/src/modules/boot_log/boot_log.py index 90cb559..3d0044c 100644 --- a/src/modules/boot_log/boot_log.py +++ b/src/modules/boot_log/boot_log.py @@ -79,5 +79,6 @@ class BootLog(ModuleABC, OnReadyABC): ), module_settings.login_message.format(init_time) ) + self._logger.info(__name__, 'Bot is ready') self._logger.trace(__name__, f'Module {type(self)} stopped')