First migrations
This commit is contained in:
@@ -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": [
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
@@ -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
|
@@ -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()
|
||||
|
@@ -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)
|
||||
|
@@ -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
|
Reference in New Issue
Block a user