Added logic to handle public and private messages #147

This commit is contained in:
Sven Heidemann 2022-12-27 19:00:50 +01:00
parent 59d38f8f2a
commit 3b7345b404
4 changed files with 10 additions and 10 deletions

View File

@ -25,7 +25,7 @@ class MessageServiceABC(ABC):
async def send_dm_message(self, message: Union[str, discord.Embed], receiver: Union[discord.User, discord.Member], without_tracking=False): pass async def send_dm_message(self, message: Union[str, discord.Embed], receiver: Union[discord.User, discord.Member], without_tracking=False): pass
@abstractmethod @abstractmethod
async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.Embed], file: discord.File = None, is_persistent: bool = False, wait_before_delete: int = None, without_tracking=True): pass async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.Embed], file: discord.File = None, is_persistent: bool = False, is_public: bool = False, wait_before_delete: int = None, without_tracking=True): pass
@abstractmethod @abstractmethod
async def send_interaction_msg(self, interaction: Interaction, message: Union[str, discord.Embed], is_persistent: bool = False, wait_before_delete: int = None, without_tracking=True, **kwargs): pass async def send_interaction_msg(self, interaction: Interaction, message: Union[str, discord.Embed], is_persistent: bool = False, is_public: bool = False, wait_before_delete: int = None, without_tracking=True, **kwargs): pass

View File

@ -89,7 +89,7 @@ class MessageService(MessageServiceABC):
self._db.save_changes() self._db.save_changes()
self._logger.info(__name__, f'Sent message to user {receiver.id}') self._logger.info(__name__, f'Sent message to user {receiver.id}')
async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.Embed], file: discord.File = None, is_persistent: bool = False, wait_before_delete: int = None, without_tracking=False): async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.Embed], file: discord.File = None, is_persistent: bool = False, is_public: bool = False, wait_before_delete: int = None, without_tracking=False):
if ctx is None: if ctx is None:
self._logger.warn(__name__, 'Message context is empty') self._logger.warn(__name__, 'Message context is empty')
self._logger.debug(__name__, f'Message: {message}') self._logger.debug(__name__, f'Message: {message}')
@ -99,9 +99,9 @@ class MessageService(MessageServiceABC):
msg = None msg = None
try: try:
if isinstance(message, discord.Embed): if isinstance(message, discord.Embed):
msg = await ctx.send(embed=message, file=file) msg = await ctx.send(embed=message, file=file, ephemeral=not is_public)
else: else:
msg = await ctx.send(message, file=file) msg = await ctx.send(message, file=file, ephemeral=not is_public)
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:
@ -119,7 +119,7 @@ class MessageService(MessageServiceABC):
if ctx.guild is not None: if ctx.guild is not None:
await self.delete_message(msg, without_tracking) await self.delete_message(msg, without_tracking)
async def send_interaction_msg(self, interaction: Interaction, message: Union[str, discord.Embed], is_persistent: bool = False, wait_before_delete: int = None, without_tracking=False, **kwargs): async def send_interaction_msg(self, interaction: Interaction, message: Union[str, discord.Embed], is_persistent: bool = False, is_public: bool = False, wait_before_delete: int = None, without_tracking=False, **kwargs):
if interaction is None: if interaction is None:
self._logger.warn(__name__, 'Message context is empty') self._logger.warn(__name__, 'Message context is empty')
self._logger.debug(__name__, f'Message: {message}') self._logger.debug(__name__, f'Message: {message}')
@ -128,9 +128,9 @@ class MessageService(MessageServiceABC):
self._logger.debug(__name__, f'Try to send message\t\t{message}\n\tto: {interaction.channel}') self._logger.debug(__name__, f'Try to send message\t\t{message}\n\tto: {interaction.channel}')
try: try:
if isinstance(message, discord.Embed): if isinstance(message, discord.Embed):
await interaction.response.send_message(embed=message, **kwargs) await interaction.response.send_message(embed=message, ephemeral=not is_public, **kwargs)
else: else:
await interaction.response.send_message(message, **kwargs) await interaction.response.send_message(message, ephemeral=not is_public, **kwargs)
except Exception as e: except Exception as e:
self._logger.error(__name__, f'Send message to channel {interaction.channel.id} failed', e) self._logger.error(__name__, f'Send message to channel {interaction.channel.id} failed', e)
else: else:

View File

@ -42,7 +42,7 @@ class HelpCommand(DiscordCommandABC):
self._logger.debug(__name__, f'Received command help {ctx}:{persistent_flag}') self._logger.debug(__name__, f'Received command help {ctx}:{persistent_flag}')
settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}') settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}')
is_persistent = persistent_flag == '--stay' is_persistent = persistent_flag == '--stay'
await self._message_service.send_ctx_msg(ctx, settings.help_command_reference_url, is_persistent=is_persistent) await self._message_service.send_ctx_msg(ctx, settings.help_command_reference_url, is_persistent=is_persistent, is_public=True)
self._logger.trace(__name__, f'Finished help command') self._logger.trace(__name__, f'Finished help command')
@help.autocomplete('persistent_flag') @help.autocomplete('persistent_flag')

View File

@ -110,7 +110,7 @@ class LogCommand(DiscordCommandABC):
zip_file = ZipFile('logs.zip', 'w') zip_file = ZipFile('logs.zip', 'w')
files.for_each(lambda x: zip_file.write(x)) files.for_each(lambda x: zip_file.write(x))
zip_file.close() zip_file.close()
await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.technician.log_message'), file=discord.File(zip_file.filename, 'logs.zip'), ephemeral=True) await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.technician.log_message'), file=discord.File(zip_file.filename, 'logs.zip'))
os.remove(zip_file.filename) os.remove(zip_file.filename)
self._logger.trace(__name__, f'Finished log command') self._logger.trace(__name__, f'Finished log command')