From 81499ef8e6a01954c438feb9dd3fe675e85f6712 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 16 Nov 2021 20:19:19 +0100 Subject: [PATCH] Added logic to send context messages --- src/gismo_core/service/message_service.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/gismo_core/service/message_service.py b/src/gismo_core/service/message_service.py index 9009a6f..40da6eb 100644 --- a/src/gismo_core/service/message_service.py +++ b/src/gismo_core/service/message_service.py @@ -39,8 +39,7 @@ class MessageService(MessageServiceABC): async def send_channel_message(self, channel: discord.TextChannel, message: str): self._logger.debug(__name__, f'Try to send message {message} to channel {channel.id}') try: - msg = await channel.send(message) - await self.delete_message(msg) + await self.delete_message(await channel.send(message)) except Exception as e: self._logger.error(__name__, f'Send message to channel {channel.id} failed', e) else: @@ -55,4 +54,19 @@ class MessageService(MessageServiceABC): else: self._logger.debug(__name__, f'Send message to user {receiver.id}') - async def send_ctx_msg(self, ctx: Context, message: str, file: discord.File = None): pass + async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.File]): + if ctx is None: + self._logger.warn(__name__, 'Message context is empty') + self._logger.debug(__name__, f'Message: {message}') + return + + self._logger.debug(__name__, f'Try to send message {message} to channel {ctx.channel.id}') + try: + if isinstance(message, discord.File): + await self.delete_message(await ctx.send(file=message)) + else: + await self.delete_message(await ctx.send(message)) + except Exception as e: + self._logger.error(__name__, f'Send message to channel {ctx.channel.id} failed', e) + else: + self._logger.debug(__name__, f'Send message to channel {ctx.channel.id}')