Finished log command #44

This commit is contained in:
Nick Jungmann 2022-11-17 22:45:10 +01:00
parent 2c7f4647af
commit 7b8dca64bf
4 changed files with 15 additions and 7 deletions

View File

@ -247,7 +247,8 @@
}, },
"technician": { "technician": {
"restart_message": "Bin gleich wieder da :D", "restart_message": "Bin gleich wieder da :D",
"shutdown_message": "Trauert nicht um mich, es war eine logische Entscheidung. Das Wohl von Vielen, es wiegt schwerer als das Wohl von Wenigen oder eines Einzelnen. Ich war es und ich werde es immer sein, Euer Freund. Lebt lange und in Frieden :)" "shutdown_message": "Trauert nicht um mich, es war eine logische Entscheidung. Das Wohl von Vielen, es wiegt schwerer als das Wohl von Wenigen oder eines Einzelnen. Ich war es und ich werde es immer sein, Euer Freund. Lebt lange und in Frieden :)",
"log_message": "Hier sind deine Logdateien! :)"
} }
}, },
"api": { "api": {

View File

@ -28,4 +28,4 @@ class MessageServiceABC(ABC):
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, 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): pass 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

View File

@ -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): 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):
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) await interaction.response.send_message(embed=message, **kwargs)
else: else:
await interaction.response.send_message(message) await interaction.response.send_message(message, **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

@ -1,6 +1,8 @@
import os import os
from datetime import datetime from datetime import datetime
from zipfile import ZipFile
import discord
from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.dependency_injection import ServiceProviderABC
from cpl_core.logging import LoggingSettings from cpl_core.logging import LoggingSettings
from cpl_discord.command import DiscordCommandABC from cpl_discord.command import DiscordCommandABC
@ -51,7 +53,7 @@ class LogCommand(DiscordCommandABC):
@commands.guild_only() @commands.guild_only()
@CommandChecks.check_is_ready() @CommandChecks.check_is_ready()
@CommandChecks.check_is_member_technician() @CommandChecks.check_is_member_technician()
async def log(self, ctx: Context, date_from: datetime = datetime.now()): async def log(self, ctx: Context):
self._logger.debug(__name__, f'Received command log {ctx}') self._logger.debug(__name__, f'Received command log {ctx}')
possible_log_paths = List(str) possible_log_paths = List(str)
@ -79,7 +81,6 @@ class LogCommand(DiscordCommandABC):
possible_log_paths.append(path) possible_log_paths.append(path)
files = List(str) files = List(str)
now = datetime.now()
for possible_path in possible_log_paths: for possible_path in possible_log_paths:
for r, d, f in os.walk(possible_path): for r, d, f in os.walk(possible_path):
for file in f: for file in f:
@ -92,4 +93,10 @@ class LogCommand(DiscordCommandABC):
files.append(os.path.join(r, file)) files.append(os.path.join(r, file))
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)
os.remove(zip_file.filename)
self._logger.trace(__name__, f'Finished log command') self._logger.trace(__name__, f'Finished log command')