Improved logging to message service

This commit is contained in:
Sven Heidemann 2021-11-16 20:48:53 +01:00
parent 5e1caf713c
commit 9584b34f8f

View File

@ -38,12 +38,14 @@ class MessageService(MessageServiceABC):
async def send_channel_message(self, channel: discord.TextChannel, message: str): 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}') self._logger.debug(__name__, f'Try to send message {message} to channel {channel.id}')
msg = None
try: try:
await self.delete_message(await channel.send(message)) msg = await channel.send(message)
except Exception as e: except Exception as e:
self._logger.error(__name__, f'Send message to channel {channel.id} failed', e) self._logger.error(__name__, f'Send message to channel {channel.id} failed', e)
else: else:
self._logger.info(__name__, f'Sent message to channel {channel.id}') self._logger.info(__name__, f'Sent message to channel {channel.id}')
await self.delete_message(msg)
async def send_dm_message(self, message: str, receiver: Union[discord.User, discord.Member]): async def send_dm_message(self, message: str, receiver: Union[discord.User, discord.Member]):
self._logger.debug(__name__, f'Try to send message {message} to user {receiver.id}') self._logger.debug(__name__, f'Try to send message {message} to user {receiver.id}')
@ -61,12 +63,14 @@ class MessageService(MessageServiceABC):
return return
self._logger.debug(__name__, f'Try to send message {message} to channel {ctx.channel.id}') self._logger.debug(__name__, f'Try to send message {message} to channel {ctx.channel.id}')
msg = None
try: try:
if isinstance(message, discord.File): if isinstance(message, discord.File):
await self.delete_message(await ctx.send(file=message)) msg = await ctx.send(file=message)
else: else:
await self.delete_message(await ctx.send(message)) msg = await ctx.send(message)
except Exception as e: except Exception as e:
self._logger.error(__name__, f'Send message to channel {ctx.channel.id} failed', e) self._logger.error(__name__, f'Send message to channel {ctx.channel.id} failed', e)
else: else:
self._logger.info(__name__, f'Sent message to channel {ctx.channel.id}') self._logger.info(__name__, f'Sent message to channel {ctx.channel.id}')
await self.delete_message(msg)