from config.config_types import factory import config.config_types as c_t import discord from mod_base import BaseModule import traceback class Logging(BaseModule): def __init__(self, client): super().__init__(client) self.config.register("error_channels", factory(c_t.List, factory(c_t.discord_types.Channel, client))) self.config.register("info_channels", factory(c_t.List, factory(c_t.discord_types.Channel, client))) self.config.register("warning_channels", factory(c_t.List, factory(c_t.discord_types.Channel, client))) self.config.register("error_color", factory(c_t.Color)) self.config.register("info_color", factory(c_t.Color)) self.config.register("warning_color", factory(c_t.Color)) self.config.load() async def send_splited_embed(self, content, channels, title, color): h = hex(abs(hash(content))) content = content.splitlines(keepends=True) contents = [] for c in content: while len(c) > 1900: contents.append(c[:1900]) c = c[1900:] contents.append(c) contents_list = [] while len(contents): contents_list.append("") while len(contents) and len(contents[0]) < 2000 - len(contents_list[-1]): contents_list[-1] += contents.pop(0) embeds = [] for i, content in enumerate(contents_list): embed = discord.Embed(title=f"{title.title()} - {h[2:8]} - {i + 1}/{len(contents_list)}", description=f"```py\n{content}\n```", color=color) embed.set_footer(text=h[2:]) embeds.append(embed) for channel in channels: try: await channel.send(f"New {title.lower()} - {h[2:8]}:") for embed in embeds: while not self.client.is_ready: await asyncio.sleep(1) await channel.send(embed=embed) except: # TODO: C'est pas beau mais il ne faut absolument pas lever une exception pour éviter les récursions infinies print(traceback.format_exc()) async def on_log_info(self, info, *args, **kwargs): channels = self.config["info_channels"] await self.send_splited_embed(info, channels, "info", self.config["info_color"]) async def on_log_error(self, error, *args, **kwargs): channels = self.config["error_channels"] await self.send_splited_embed(error, channels, "error", self.config["error_color"]) async def on_log_warning(self, warning, *args, **kwargs): channels = self.config["warning_channels"] await self.send_splited_embed(warning, channels, "warning", self.config["warning_color"]) __main_class__ = Logging