First migrations

This commit is contained in:
Sven Heidemann 2022-09-10 10:27:31 +02:00
parent 88e83db330
commit 92ed684866
15 changed files with 81 additions and 109 deletions

View File

@ -3,8 +3,8 @@
"Name": "cpl-core", "Name": "cpl-core",
"Version": { "Version": {
"Major": "2022", "Major": "2022",
"Minor": "7", "Minor": "10",
"Micro": "0.post5" "Micro": "1"
}, },
"Author": "Sven Heidemann", "Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de", "AuthorEmail": "sven.heidemann@sh-edraft.de",
@ -16,16 +16,16 @@
"LicenseName": "MIT", "LicenseName": "MIT",
"LicenseDescription": "MIT, see LICENSE for more details.", "LicenseDescription": "MIT, see LICENSE for more details.",
"Dependencies": [ "Dependencies": [
"art==5.6", "art==5.7",
"colorama==0.4.4", "colorama==0.4.5",
"mysql-connector==2.2.9", "mysql-connector==2.2.9",
"psutil==5.9.0", "psutil==5.9.2",
"packaging==21.3", "packaging==21.3",
"pynput==1.7.6", "pynput==1.7.6",
"setuptools==62.1.0", "setuptools==65.3.0",
"tabulate==0.8.9", "tabulate==0.8.10",
"termcolor==1.1.0", "termcolor==1.1.0",
"watchdog==2.1.7", "watchdog==2.1.9",
"wheel==0.37.1" "wheel==0.37.1"
], ],
"DevDependencies": [ "DevDependencies": [

View File

@ -3,8 +3,8 @@
"Name": "cpl-discord", "Name": "cpl-discord",
"Version": { "Version": {
"Major": "2022", "Major": "2022",
"Minor": "7", "Minor": "10",
"Micro": "0.post4" "Micro": "1"
}, },
"Author": "Sven Heidemann", "Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de", "AuthorEmail": "sven.heidemann@sh-edraft.de",
@ -17,7 +17,7 @@
"LicenseDescription": "MIT, see LICENSE for more details.", "LicenseDescription": "MIT, see LICENSE for more details.",
"Dependencies": [ "Dependencies": [
"cpl-core>=2022.7.0.post2", "cpl-core>=2022.7.0.post2",
"discord.py==1.7.3", "discord.py==2.0.1",
"cpl-query==2022.7.0" "cpl-query==2022.7.0"
], ],
"DevDependencies": [ "DevDependencies": [

View File

@ -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_clear_emoji_abc import OnReactionClearEmojiABC
from cpl_discord.events.on_reaction_remove_abc import OnReactionRemoveABC from cpl_discord.events.on_reaction_remove_abc import OnReactionRemoveABC
from cpl_discord.events.on_ready_abc import OnReadyABC 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_resume_abc import OnResumeABC
from cpl_discord.events.on_typing_abc import OnTypingABC from cpl_discord.events.on_typing_abc import OnTypingABC
from cpl_discord.events.on_user_update_abc import OnUserUpdateABC from cpl_discord.events.on_user_update_abc import OnUserUpdateABC
@ -95,9 +92,6 @@ class DiscordEventTypesEnum(Enum):
on_reaction_clear_emoji = OnReactionClearEmojiABC on_reaction_clear_emoji = OnReactionClearEmojiABC
on_reaction_remove = OnReactionRemoveABC on_reaction_remove = OnReactionRemoveABC
on_ready = OnReadyABC on_ready = OnReadyABC
on_relationship_add = OnRelationshipAddABC
on_relationship_remove = OnRelationshipRemoveABC
on_relationship_update = OnRelationshipUpdateABC
on_resume = OnResumeABC on_resume = OnResumeABC
on_typing = OnTypingABC on_typing = OnTypingABC
on_user_update = OnUserUpdateABC on_user_update = OnUserUpdateABC

View File

@ -62,9 +62,6 @@ from .on_reaction_clear_abc import OnReactionClearABC
from .on_reaction_clear_emoji_abc import OnReactionClearEmojiABC from .on_reaction_clear_emoji_abc import OnReactionClearEmojiABC
from .on_reaction_remove_abc import OnReactionRemoveABC from .on_reaction_remove_abc import OnReactionRemoveABC
from .on_ready_abc import OnReadyABC 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_resume_abc import OnResumeABC
from .on_typing_abc import OnTypingABC from .on_typing_abc import OnTypingABC
from .on_user_update_abc import OnUserUpdateABC from .on_user_update_abc import OnUserUpdateABC

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -21,29 +21,37 @@ class DiscordBotService(DiscordBotServiceABC):
discord_service: DiscordServiceABC discord_service: DiscordServiceABC
): ):
# services # services
self._config = config
self._logger = logger self._logger = logger
self._env = env self._env = env
self._logging_st = logging_st self._logging_st = logging_st
self._discord_service = discord_service self._discord_service = discord_service
# settings # settings
if discord_bot_settings is None: self._discord_settings = self._get_settings(discord_bot_settings)
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
# setup super # setup super
DiscordBotServiceABC.__init__(self, command_prefix=self._discord_settings.prefix, help_command=None, intents=discord.Intents().all()) 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): async def start_async(self):
self._logger.trace(__name__, 'Try to connect to discord') self._logger.trace(__name__, 'Try to connect to discord')
await self.start(self._discord_settings.token) await self.start(self._discord_settings.token)
@ -63,6 +71,6 @@ class DiscordBotService(DiscordBotServiceABC):
if self._logging_st.console.value >= LoggingLevelEnum.INFO.value: if self._logging_st.console.value >= LoggingLevelEnum.INFO.value:
Console.banner(self._env.application_name if self._env.application_name != '' else 'A bot') 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() await self._discord_service.on_ready()

View File

@ -3,9 +3,8 @@ from typing import Optional, Sequence, Union, Type
import discord import discord
from discord.ext import commands 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.dependency_injection import ServiceProviderABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from cpl_core.utils import String 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_clear_emoji_abc import OnReactionClearEmojiABC
from cpl_discord.events.on_reaction_remove_abc import OnReactionRemoveABC from cpl_discord.events.on_reaction_remove_abc import OnReactionRemoveABC
from cpl_discord.events.on_ready_abc import OnReadyABC 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_resume_abc import OnResumeABC
from cpl_discord.events.on_typing_abc import OnTypingABC from cpl_discord.events.on_typing_abc import OnTypingABC
from cpl_discord.events.on_user_update_abc import OnUserUpdateABC 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: except Exception as e:
self._logger.error(__name__, f'Cannot execute {func_name} of {type(event_instance).__name__}', 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: try:
bot.add_cog(self) await bot.add_cog(self)
except Exception as e: except Exception as e:
self._logger.error(__name__, f'{type(self).__name__} initialization failed', e) self._logger.error(__name__, f'{type(self).__name__} initialization failed', e)
try: try:
for command_type in self._collection.get_commands(): for command_type in self._collection.get_commands():
self._logger.trace(__name__, f'Register command {command_type.__name__}') 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: if command is None:
self._logger.warn(__name__, f'Instance of {command_type.__name__} not found') self._logger.warn(__name__, f'Instance of {command_type.__name__} not found')
continue continue
bot.add_cog(command) await bot.add_cog(command)
except Exception as e: except Exception as e:
self._logger.error(__name__, f'Registration of commands failed', 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): 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}') self._logger.trace(__name__, f'Received on_group_remove:\n\t{channel}\n\t{user}')
await self._handle_event(OnGroupRemoveABC, channel, 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)

View File

@ -156,13 +156,4 @@ class DiscordServiceABC(ABC):
async def on_group_join(self, chhanel: discord.GroupChannel, user: discord.User): pass async def on_group_join(self, chhanel: discord.GroupChannel, user: discord.User): pass
@abstractmethod @abstractmethod
async def on_group_remove(self, chhanel: discord.GroupChannel, user: discord.User): pass 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

View File

@ -6,9 +6,13 @@
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S" "DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
}, },
"LoggingSettings": { "LoggingSettings": {
"Path": "logs/", "Path": "logs/",
"Filename": "log_dev.log", "Filename": "log_dev.log",
"ConsoleLogLevel": "TRACE", "ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE" "FileLogLevel": "TRACE"
} },
"DiscordBotSettings": {
"Token": "",
"Prefix": "!cd "
}
} }

View File

@ -1,7 +1,7 @@
import asyncio import asyncio
from typing import Optional 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.application import Application
from discord_bot.startup import Startup from discord_bot.startup import Startup
@ -15,7 +15,7 @@ class Main:
async def main(self): async def main(self):
app_builder = ApplicationBuilder(Application) app_builder = ApplicationBuilder(Application)
app_builder.use_startup(Startup) 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() await self._app.run_async()
async def stop(self): async def stop(self):

View File

@ -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_event import OnReadyEvent
from modules.hello_world.on_ready_test_event import OnReadyTestEvent from modules.hello_world.on_ready_test_event import OnReadyTestEvent
from modules.hello_world.ping_command import PingCommand from modules.hello_world.ping_command import PingCommand
from modules.hello_world.purge_command import PurgeCommand
class Startup(StartupABC): 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, OnReadyEvent)
dc_collection.add_event(DiscordEventTypesEnum.on_ready.value, OnReadyTestEvent) dc_collection.add_event(DiscordEventTypesEnum.on_ready.value, OnReadyTestEvent)
dc_collection.add_command(PingCommand) dc_collection.add_command(PingCommand)
dc_collection.add_command(PurgeCommand)
return services.build_service_provider() return services.build_service_provider()

View File

@ -25,3 +25,4 @@ class PingCommand(DiscordCommandABC):
self._logger.debug(__name__, f'Received command ping {ctx}') self._logger.debug(__name__, f'Received command ping {ctx}')
self._logger.info(__name__, f'Bot name {self._bot.user.name}') self._logger.info(__name__, f'Bot name {self._bot.user.name}')
self._logger.trace(__name__, f'Finished ping command') self._logger.trace(__name__, f'Finished ping command')
await ctx.send('Pong')

View File

@ -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()