Added logic to handle commands
This commit is contained in:
14
src/cpl_discord/service/command_error_handler_service.py
Normal file
14
src/cpl_discord/service/command_error_handler_service.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from discord.ext.commands import Context, CommandError
|
||||
|
||||
from cpl_core.logging import LoggerABC
|
||||
from cpl_discord.events.on_command_error_abc import OnCommandErrorABC
|
||||
|
||||
|
||||
class CommandErrorHandlerService(OnCommandErrorABC):
|
||||
|
||||
def __init__(self, logger: LoggerABC):
|
||||
OnCommandErrorABC.__init__(self)
|
||||
self._logger = logger
|
||||
|
||||
async def on_command_error(self, ctx: Context, error: CommandError):
|
||||
self._logger.error(__name__, f'Error in command: {ctx.command}', error)
|
||||
@@ -1,7 +1,4 @@
|
||||
import sys
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.console import Console
|
||||
@@ -66,6 +63,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.add_cog(self._discord_service)
|
||||
self._discord_service.init(self)
|
||||
|
||||
await self._discord_service.on_ready()
|
||||
|
||||
@@ -2,6 +2,8 @@ from typing import Type, Optional
|
||||
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.dependency_injection import ServiceCollectionABC
|
||||
from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum
|
||||
from cpl_discord.service.command_error_handler_service import CommandErrorHandlerService
|
||||
from cpl_discord.service.discord_collection_abc import DiscordCollectionABC
|
||||
from cpl_query.extension import List
|
||||
|
||||
@@ -15,9 +17,16 @@ class DiscordCollection(DiscordCollectionABC):
|
||||
|
||||
self._services = service_collection
|
||||
self._events: dict[str, List] = {}
|
||||
self._commands = List(type(CommandABC))
|
||||
|
||||
self.add_event(DiscordEventTypesEnum.on_command_error.value, CommandErrorHandlerService)
|
||||
|
||||
def add_command(self, _t: Type[CommandABC]):
|
||||
self._services.add_transient(CommandABC, _t)
|
||||
self._commands.append(_t)
|
||||
|
||||
def get_commands(self) -> List[CommandABC]:
|
||||
return self._commands
|
||||
|
||||
def add_event(self, _t_event: Type, _t: Type):
|
||||
self._services.add_transient(_t_event, _t)
|
||||
|
||||
@@ -14,6 +14,9 @@ class DiscordCollectionABC(ABC):
|
||||
@abstractmethod
|
||||
def add_command(self, _t: Type[CommandABC]): pass
|
||||
|
||||
@abstractmethod
|
||||
def get_commands(self) -> List[CommandABC]: pass
|
||||
|
||||
@abstractmethod
|
||||
def add_event(self, _t_event: Type, _t: Type): pass
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ from typing import Optional, Sequence, Union, Type
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from discord.ext.commands import Context, CommandError
|
||||
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
@@ -10,6 +11,9 @@ from cpl_core.logging import LoggerABC
|
||||
from cpl_core.utils import String
|
||||
from cpl_discord.command.discord_commands_meta import DiscordCogMeta
|
||||
from cpl_discord.events.on_bulk_message_delete_abc import OnBulkMessageDeleteABC
|
||||
from cpl_discord.events.on_command_abc import OnCommandABC
|
||||
from cpl_discord.events.on_command_completion_abc import OnCommandCompletionABC
|
||||
from cpl_discord.events.on_command_error_abc import OnCommandErrorABC
|
||||
from cpl_discord.events.on_connect_abc import OnConnectABC
|
||||
from cpl_discord.events.on_disconnect_abc import OnDisconnectABC
|
||||
from cpl_discord.events.on_group_join_abc import OnGroupJoinABC
|
||||
@@ -90,13 +94,45 @@ class DiscordService(DiscordServiceABC, commands.Cog, metaclass=DiscordCogMeta):
|
||||
func = getattr(event_instance, func_name)
|
||||
await func(*args)
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f'Cannot execute {func_name} of {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):
|
||||
try:
|
||||
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)
|
||||
if command is None:
|
||||
self._logger.warn(__name__, f'Instance of {command_type.__name__} not found')
|
||||
continue
|
||||
bot.add_cog(command)
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f'Registration of commands failed', e)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_connect(self):
|
||||
self._logger.trace(__name__, f'Received on_connect')
|
||||
await self._handle_event(OnConnectABC)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_command(self, ctx: Context):
|
||||
self._logger.trace(__name__, f'Received on_command')
|
||||
await self._handle_event(OnCommandABC, ctx)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_command_error(self, ctx: Context, error: CommandError):
|
||||
self._logger.trace(__name__, f'Received on_command_error')
|
||||
await self._handle_event(OnCommandErrorABC, ctx, error)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_command_completion(self, ctx: Context):
|
||||
self._logger.trace(__name__, f'Received on_command_completion')
|
||||
await self._handle_event(OnCommandCompletionABC, ctx)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_disconnect(self):
|
||||
self._logger.trace(__name__, f'Received on_disconnect')
|
||||
|
||||
@@ -3,6 +3,7 @@ from datetime import datetime
|
||||
from typing import Optional, Sequence, Union
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
|
||||
class DiscordServiceABC(ABC):
|
||||
@@ -10,9 +11,21 @@ class DiscordServiceABC(ABC):
|
||||
def __init__(self):
|
||||
ABC.__init__(self)
|
||||
|
||||
@abstractmethod
|
||||
def init(self, bot: commands.Bot): pass
|
||||
|
||||
@abstractmethod
|
||||
async def on_connect(self): pass
|
||||
|
||||
@abstractmethod
|
||||
async def on_command(self): pass
|
||||
|
||||
@abstractmethod
|
||||
async def on_command_error(self): pass
|
||||
|
||||
@abstractmethod
|
||||
async def on_command_completion(self): pass
|
||||
|
||||
@abstractmethod
|
||||
async def on_disconnect(self): pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user