From 92ed6848668f2a1e0d74bcbe0da77a33fe5f6798 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 10 Sep 2022 10:27:31 +0200 Subject: [PATCH] First migrations --- src/cpl_core/cpl-core.json | 16 ++++----- src/cpl_discord/cpl-discord.json | 6 ++-- src/cpl_discord/discord_event_types_enum.py | 6 ---- src/cpl_discord/events/__init__.py | 3 -- .../events/on_relationship_add_abc.py | 11 ------ .../events/on_relationship_remove_abc.py | 11 ------ .../events/on_relationship_update_abc.py | 12 ------- .../service/discord_bot_service.py | 36 +++++++++++-------- src/cpl_discord/service/discord_service.py | 29 +++------------ .../service/discord_service_abc.py | 11 +----- .../discord/src/discord_bot/appsettings.json | 14 +++++--- .../custom/discord/src/discord_bot/main.py | 4 +-- .../custom/discord/src/discord_bot/startup.py | 2 ++ .../src/modules/hello_world/ping_command.py | 1 + .../src/modules/hello_world/purge_command.py | 28 +++++++++++++++ 15 files changed, 81 insertions(+), 109 deletions(-) delete mode 100644 src/cpl_discord/events/on_relationship_add_abc.py delete mode 100644 src/cpl_discord/events/on_relationship_remove_abc.py delete mode 100644 src/cpl_discord/events/on_relationship_update_abc.py create mode 100644 src/tests/custom/discord/src/modules/hello_world/purge_command.py diff --git a/src/cpl_core/cpl-core.json b/src/cpl_core/cpl-core.json index 29c2da08..04214452 100644 --- a/src/cpl_core/cpl-core.json +++ b/src/cpl_core/cpl-core.json @@ -3,8 +3,8 @@ "Name": "cpl-core", "Version": { "Major": "2022", - "Minor": "7", - "Micro": "0.post5" + "Minor": "10", + "Micro": "1" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", @@ -16,16 +16,16 @@ "LicenseName": "MIT", "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ - "art==5.6", - "colorama==0.4.4", + "art==5.7", + "colorama==0.4.5", "mysql-connector==2.2.9", - "psutil==5.9.0", + "psutil==5.9.2", "packaging==21.3", "pynput==1.7.6", - "setuptools==62.1.0", - "tabulate==0.8.9", + "setuptools==65.3.0", + "tabulate==0.8.10", "termcolor==1.1.0", - "watchdog==2.1.7", + "watchdog==2.1.9", "wheel==0.37.1" ], "DevDependencies": [ diff --git a/src/cpl_discord/cpl-discord.json b/src/cpl_discord/cpl-discord.json index 3ffa8ef4..d82c1478 100644 --- a/src/cpl_discord/cpl-discord.json +++ b/src/cpl_discord/cpl-discord.json @@ -3,8 +3,8 @@ "Name": "cpl-discord", "Version": { "Major": "2022", - "Minor": "7", - "Micro": "0.post4" + "Minor": "10", + "Micro": "1" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", @@ -17,7 +17,7 @@ "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ "cpl-core>=2022.7.0.post2", - "discord.py==1.7.3", + "discord.py==2.0.1", "cpl-query==2022.7.0" ], "DevDependencies": [ diff --git a/src/cpl_discord/discord_event_types_enum.py b/src/cpl_discord/discord_event_types_enum.py index 342633a0..7106d29a 100644 --- a/src/cpl_discord/discord_event_types_enum.py +++ b/src/cpl_discord/discord_event_types_enum.py @@ -42,9 +42,6 @@ from cpl_discord.events.on_reaction_clear_abc import OnReactionClearABC from cpl_discord.events.on_reaction_clear_emoji_abc import OnReactionClearEmojiABC from cpl_discord.events.on_reaction_remove_abc import OnReactionRemoveABC from cpl_discord.events.on_ready_abc import OnReadyABC -from cpl_discord.events.on_relationship_add_abc import OnRelationshipAddABC -from cpl_discord.events.on_relationship_remove_abc import OnRelationshipRemoveABC -from cpl_discord.events.on_relationship_update_abc import OnRelationshipUpdateABC from cpl_discord.events.on_resume_abc import OnResumeABC from cpl_discord.events.on_typing_abc import OnTypingABC from cpl_discord.events.on_user_update_abc import OnUserUpdateABC @@ -95,9 +92,6 @@ class DiscordEventTypesEnum(Enum): on_reaction_clear_emoji = OnReactionClearEmojiABC on_reaction_remove = OnReactionRemoveABC on_ready = OnReadyABC - on_relationship_add = OnRelationshipAddABC - on_relationship_remove = OnRelationshipRemoveABC - on_relationship_update = OnRelationshipUpdateABC on_resume = OnResumeABC on_typing = OnTypingABC on_user_update = OnUserUpdateABC diff --git a/src/cpl_discord/events/__init__.py b/src/cpl_discord/events/__init__.py index 84115097..a89be5ba 100644 --- a/src/cpl_discord/events/__init__.py +++ b/src/cpl_discord/events/__init__.py @@ -62,9 +62,6 @@ from .on_reaction_clear_abc import OnReactionClearABC from .on_reaction_clear_emoji_abc import OnReactionClearEmojiABC from .on_reaction_remove_abc import OnReactionRemoveABC from .on_ready_abc import OnReadyABC -from .on_relationship_add_abc import OnRelationshipAddABC -from .on_relationship_remove_abc import OnRelationshipRemoveABC -from .on_relationship_update_abc import OnRelationshipUpdateABC from .on_resume_abc import OnResumeABC from .on_typing_abc import OnTypingABC from .on_user_update_abc import OnUserUpdateABC diff --git a/src/cpl_discord/events/on_relationship_add_abc.py b/src/cpl_discord/events/on_relationship_add_abc.py deleted file mode 100644 index 5cba49a0..00000000 --- a/src/cpl_discord/events/on_relationship_add_abc.py +++ /dev/null @@ -1,11 +0,0 @@ -from abc import ABC, abstractmethod -import discord - -class OnRelationshipAddABC(ABC): - - @abstractmethod - def __init__(self): pass - - @abstractmethod - async def on_relationship_add(self, relationship: discord.Relationship): pass - \ No newline at end of file diff --git a/src/cpl_discord/events/on_relationship_remove_abc.py b/src/cpl_discord/events/on_relationship_remove_abc.py deleted file mode 100644 index 4c8907ec..00000000 --- a/src/cpl_discord/events/on_relationship_remove_abc.py +++ /dev/null @@ -1,11 +0,0 @@ -from abc import ABC, abstractmethod -import discord - - -class OnRelationshipRemoveABC(ABC): - - @abstractmethod - def __init__(self): pass - - @abstractmethod - async def on_relationship_remove(self, relationship: discord.Relationship): pass diff --git a/src/cpl_discord/events/on_relationship_update_abc.py b/src/cpl_discord/events/on_relationship_update_abc.py deleted file mode 100644 index 4d970914..00000000 --- a/src/cpl_discord/events/on_relationship_update_abc.py +++ /dev/null @@ -1,12 +0,0 @@ -from abc import ABC, abstractmethod -import discord - - -class OnRelationshipUpdateABC(ABC): - - @abstractmethod - def __init__(self): pass - - @abstractmethod - async def on_relationship_update( - self, before: discord.Relationship, after: discord.Relationship): pass diff --git a/src/cpl_discord/service/discord_bot_service.py b/src/cpl_discord/service/discord_bot_service.py index 8d4b1afa..aa4ac44c 100644 --- a/src/cpl_discord/service/discord_bot_service.py +++ b/src/cpl_discord/service/discord_bot_service.py @@ -21,29 +21,37 @@ class DiscordBotService(DiscordBotServiceABC): discord_service: DiscordServiceABC ): # services + self._config = config self._logger = logger self._env = env self._logging_st = logging_st self._discord_service = discord_service # settings - if discord_bot_settings is None: - self._discord_settings = DiscordBotSettings() - token = config.get_configuration('TOKEN') - if token is None: - raise Exception('You have to configure discord token by appsettings or environment variables') - - prefix = config.get_configuration('PREFIX') - self._discord_settings.from_dict({ - 'Token': token, - 'Prefix': prefix if prefix is not None else '! ' - }) - else: - self._discord_settings = discord_bot_settings + self._discord_settings = self._get_settings(discord_bot_settings) # setup super DiscordBotServiceABC.__init__(self, command_prefix=self._discord_settings.prefix, help_command=None, intents=discord.Intents().all()) + @staticmethod + def _is_string_invalid(x): + return x is None or x == '' + + def _get_settings(self, settings_from_config: DiscordBotSettings) -> DiscordBotSettings: + new_settings = DiscordBotSettings() + token = settings_from_config.token + prefix = settings_from_config.prefix + env_token = self._config.get_configuration('TOKEN') + env_prefix = self._config.get_configuration('PREFIX') + + new_settings.from_dict({ + 'Token': env_token if token is None or token == '' else token, + 'Prefix': ('! ' if self._is_string_invalid(env_prefix) else env_prefix) if self._is_string_invalid(prefix) else prefix + }) + if new_settings.token is None or new_settings.token == '': + raise Exception('You have to configure discord token by appsettings or environment variables') + return new_settings + async def start_async(self): self._logger.trace(__name__, 'Try to connect to discord') await self.start(self._discord_settings.token) @@ -63,6 +71,6 @@ class DiscordBotService(DiscordBotServiceABC): if self._logging_st.console.value >= LoggingLevelEnum.INFO.value: Console.banner(self._env.application_name if self._env.application_name != '' else 'A bot') - self._discord_service.init(self) + await self._discord_service.init(self) await self._discord_service.on_ready() diff --git a/src/cpl_discord/service/discord_service.py b/src/cpl_discord/service/discord_service.py index d2dcf0dc..89b1951e 100644 --- a/src/cpl_discord/service/discord_service.py +++ b/src/cpl_discord/service/discord_service.py @@ -3,9 +3,8 @@ from typing import Optional, Sequence, Union, Type import discord from discord.ext import commands -from discord.ext.commands import Context, CommandError +from discord.ext.commands import Context, CommandError, Cog -from cpl_core.console import Console from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.logging import LoggerABC from cpl_core.utils import String @@ -52,9 +51,6 @@ from cpl_discord.events.on_reaction_clear_abc import OnReactionClearABC from cpl_discord.events.on_reaction_clear_emoji_abc import OnReactionClearEmojiABC from cpl_discord.events.on_reaction_remove_abc import OnReactionRemoveABC from cpl_discord.events.on_ready_abc import OnReadyABC -from cpl_discord.events.on_relationship_add_abc import OnRelationshipAddABC -from cpl_discord.events.on_relationship_remove_abc import OnRelationshipRemoveABC -from cpl_discord.events.on_relationship_update_abc import OnRelationshipUpdateABC from cpl_discord.events.on_resume_abc import OnResumeABC from cpl_discord.events.on_typing_abc import OnTypingABC from cpl_discord.events.on_user_update_abc import OnUserUpdateABC @@ -97,20 +93,20 @@ class DiscordService(DiscordServiceABC, commands.Cog, metaclass=DiscordCogMeta): except Exception as e: self._logger.error(__name__, f'Cannot execute {func_name} of {type(event_instance).__name__}', e) - def init(self, bot: commands.Bot): + async def init(self, bot: commands.Bot): try: - bot.add_cog(self) + await bot.add_cog(self) except Exception as e: self._logger.error(__name__, f'{type(self).__name__} initialization failed', e) try: for command_type in self._collection.get_commands(): self._logger.trace(__name__, f'Register command {command_type.__name__}') - command = self._services.get_service(command_type) + command: Cog = self._services.get_service(command_type) if command is None: self._logger.warn(__name__, f'Instance of {command_type.__name__} not found') continue - bot.add_cog(command) + await bot.add_cog(command) except Exception as e: self._logger.error(__name__, f'Registration of commands failed', e) @@ -352,18 +348,3 @@ class DiscordService(DiscordServiceABC, commands.Cog, metaclass=DiscordCogMeta): async def on_group_remove(self, channel: discord.GroupChannel, user: discord.User): self._logger.trace(__name__, f'Received on_group_remove:\n\t{channel}\n\t{user}') await self._handle_event(OnGroupRemoveABC, channel, user) - - @commands.Cog.listener() - async def on_relationship_add(self, relationship: discord.Relationship): - self._logger.trace(__name__, f'Received on_relationship_add:\n\t{relationship}') - await self._handle_event(OnRelationshipAddABC, relationship) - - @commands.Cog.listener() - async def on_relationship_remove(self, relationship: discord.Relationship): - self._logger.trace(__name__, f'Received on_relationship_remove:\n\t{relationship}') - await self._handle_event(OnRelationshipRemoveABC, relationship) - - @commands.Cog.listener() - async def on_relationship_update(self, before: discord.Relationship, after: discord.Relationship): - self._logger.trace(__name__, f'Received on_relationship_update:\n\t{before}\n\t{after}') - await self._handle_event(OnRelationshipUpdateABC, before, after) diff --git a/src/cpl_discord/service/discord_service_abc.py b/src/cpl_discord/service/discord_service_abc.py index c0911a05..ca2993a3 100644 --- a/src/cpl_discord/service/discord_service_abc.py +++ b/src/cpl_discord/service/discord_service_abc.py @@ -156,13 +156,4 @@ class DiscordServiceABC(ABC): async def on_group_join(self, chhanel: discord.GroupChannel, user: discord.User): pass @abstractmethod - async def on_group_remove(self, chhanel: discord.GroupChannel, user: discord.User): pass - - @abstractmethod - async def on_relationship_add(self, relationship: discord.Relationship): pass - - @abstractmethod - async def on_relationship_remove(self, relationship: discord.Relationship): pass - - @abstractmethod - async def on_relationship_update(self, before: discord.Relationship, after: discord.Relationship): pass + async def on_group_remove(self, chhanel: discord.GroupChannel, user: discord.User): pass \ No newline at end of file diff --git a/src/tests/custom/discord/src/discord_bot/appsettings.json b/src/tests/custom/discord/src/discord_bot/appsettings.json index 2725302b..5e211c01 100644 --- a/src/tests/custom/discord/src/discord_bot/appsettings.json +++ b/src/tests/custom/discord/src/discord_bot/appsettings.json @@ -6,9 +6,13 @@ "DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S" }, "LoggingSettings": { - "Path": "logs/", - "Filename": "log_dev.log", - "ConsoleLogLevel": "TRACE", - "FileLogLevel": "TRACE" - } + "Path": "logs/", + "Filename": "log_dev.log", + "ConsoleLogLevel": "TRACE", + "FileLogLevel": "TRACE" + }, + "DiscordBotSettings": { + "Token": "", + "Prefix": "!cd " + } } \ No newline at end of file diff --git a/src/tests/custom/discord/src/discord_bot/main.py b/src/tests/custom/discord/src/discord_bot/main.py index f6c79e69..669645fe 100644 --- a/src/tests/custom/discord/src/discord_bot/main.py +++ b/src/tests/custom/discord/src/discord_bot/main.py @@ -1,7 +1,7 @@ import asyncio from typing import Optional -from cpl_core.application import ApplicationBuilder +from cpl_core.application import ApplicationBuilder, ApplicationABC from discord_bot.application import Application from discord_bot.startup import Startup @@ -15,7 +15,7 @@ class Main: async def main(self): app_builder = ApplicationBuilder(Application) app_builder.use_startup(Startup) - self._app: Application = await app_builder.build_async() + self._app: ApplicationABC = await app_builder.build_async() await self._app.run_async() async def stop(self): diff --git a/src/tests/custom/discord/src/discord_bot/startup.py b/src/tests/custom/discord/src/discord_bot/startup.py index e138c5d1..11599cb2 100644 --- a/src/tests/custom/discord/src/discord_bot/startup.py +++ b/src/tests/custom/discord/src/discord_bot/startup.py @@ -7,6 +7,7 @@ from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum from modules.hello_world.on_ready_event import OnReadyEvent from modules.hello_world.on_ready_test_event import OnReadyTestEvent from modules.hello_world.ping_command import PingCommand +from modules.hello_world.purge_command import PurgeCommand class Startup(StartupABC): @@ -28,5 +29,6 @@ class Startup(StartupABC): dc_collection.add_event(DiscordEventTypesEnum.on_ready.value, OnReadyEvent) dc_collection.add_event(DiscordEventTypesEnum.on_ready.value, OnReadyTestEvent) dc_collection.add_command(PingCommand) + dc_collection.add_command(PurgeCommand) return services.build_service_provider() diff --git a/src/tests/custom/discord/src/modules/hello_world/ping_command.py b/src/tests/custom/discord/src/modules/hello_world/ping_command.py index 0f6b8106..16af3887 100644 --- a/src/tests/custom/discord/src/modules/hello_world/ping_command.py +++ b/src/tests/custom/discord/src/modules/hello_world/ping_command.py @@ -25,3 +25,4 @@ class PingCommand(DiscordCommandABC): self._logger.debug(__name__, f'Received command ping {ctx}') self._logger.info(__name__, f'Bot name {self._bot.user.name}') self._logger.trace(__name__, f'Finished ping command') + await ctx.send('Pong') diff --git a/src/tests/custom/discord/src/modules/hello_world/purge_command.py b/src/tests/custom/discord/src/modules/hello_world/purge_command.py new file mode 100644 index 00000000..0a36f379 --- /dev/null +++ b/src/tests/custom/discord/src/modules/hello_world/purge_command.py @@ -0,0 +1,28 @@ +from discord.ext import commands +from discord.ext.commands import Context + +from cpl_core.logging import LoggerABC +from cpl_discord.command.discord_command_abc import DiscordCommandABC +from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC + + +class PurgeCommand(DiscordCommandABC): + + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._bot = bot + + self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + + @commands.command() + async def purge(self, ctx: Context): + self._logger.debug(__name__, f'Received command ping {ctx}') + self._logger.info(__name__, f'Bot name {self._bot.user.name}') + self._logger.trace(__name__, f'Finished ping command') + await ctx.channel.purge()