From 3b7345b4048746a3ff4d173adb977b5c72d4382e Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 27 Dec 2022 19:00:50 +0100 Subject: [PATCH] Added logic to handle public and private messages #147 --- kdb-bot/src/bot_core/abc/message_service_abc.py | 4 ++-- kdb-bot/src/bot_core/service/message_service.py | 12 ++++++------ kdb-bot/src/modules/base/command/help_command.py | 2 +- .../src/modules/technician/command/log_command.py | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/kdb-bot/src/bot_core/abc/message_service_abc.py b/kdb-bot/src/bot_core/abc/message_service_abc.py index 3542e2b7..4af35bff 100644 --- a/kdb-bot/src/bot_core/abc/message_service_abc.py +++ b/kdb-bot/src/bot_core/abc/message_service_abc.py @@ -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 @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 - 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 diff --git a/kdb-bot/src/bot_core/service/message_service.py b/kdb-bot/src/bot_core/service/message_service.py index dcd329a2..967d23e5 100644 --- a/kdb-bot/src/bot_core/service/message_service.py +++ b/kdb-bot/src/bot_core/service/message_service.py @@ -89,7 +89,7 @@ class MessageService(MessageServiceABC): self._db.save_changes() 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: self._logger.warn(__name__, 'Message context is empty') self._logger.debug(__name__, f'Message: {message}') @@ -99,9 +99,9 @@ class MessageService(MessageServiceABC): msg = None try: 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: - msg = await ctx.send(message, file=file) + msg = await ctx.send(message, file=file, ephemeral=not is_public) except Exception as e: self._logger.error(__name__, f'Send message to channel {ctx.channel.id} failed', e) else: @@ -119,7 +119,7 @@ class MessageService(MessageServiceABC): if ctx.guild is not None: 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: self._logger.warn(__name__, 'Message context is empty') 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}') try: 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: - await interaction.response.send_message(message, **kwargs) + await interaction.response.send_message(message, ephemeral=not is_public, **kwargs) except Exception as e: self._logger.error(__name__, f'Send message to channel {interaction.channel.id} failed', e) else: diff --git a/kdb-bot/src/modules/base/command/help_command.py b/kdb-bot/src/modules/base/command/help_command.py index 0c690ecb..dd252740 100644 --- a/kdb-bot/src/modules/base/command/help_command.py +++ b/kdb-bot/src/modules/base/command/help_command.py @@ -42,7 +42,7 @@ class HelpCommand(DiscordCommandABC): self._logger.debug(__name__, f'Received command help {ctx}:{persistent_flag}') settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}') 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') @help.autocomplete('persistent_flag') diff --git a/kdb-bot/src/modules/technician/command/log_command.py b/kdb-bot/src/modules/technician/command/log_command.py index 13160538..3988ce63 100644 --- a/kdb-bot/src/modules/technician/command/log_command.py +++ b/kdb-bot/src/modules/technician/command/log_command.py @@ -110,7 +110,7 @@ class LogCommand(DiscordCommandABC): zip_file = ZipFile('logs.zip', 'w') files.for_each(lambda x: zip_file.write(x)) 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) self._logger.trace(__name__, f'Finished log command') -- 2.45.2