mod-logging/__init__.py

63 lines
2.7 KiB
Python
Raw Normal View History

2020-04-21 23:26:00 +02:00
from config.config_types import factory
import config.config_types as c_t
2020-04-24 12:03:04 +02:00
import discord
2020-04-27 13:37:28 +02:00
from mod_base import BaseModule
2020-04-21 23:26:00 +02:00
2020-04-24 12:03:04 +02:00
import traceback
2020-04-27 13:37:28 +02:00
class Logging(BaseModule):
2020-04-21 23:26:00 +02:00
def __init__(self, client):
super().__init__(client)
2020-04-24 22:05:53 +02:00
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)))
2020-04-25 01:02:34 +02:00
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))
2020-04-24 12:03:04 +02:00
self.config.load()
2020-04-25 01:02:34 +02:00
async def send_splited_embed(self, content, channels, title, color):
2020-04-24 22:05:53 +02:00
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)}",
2020-04-25 01:02:34 +02:00
description=f"```py\n{content}\n```", color=color)
2020-04-24 22:05:53 +02:00
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:
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"]
2020-04-25 01:02:34 +02:00
await self.send_splited_embed(info, channels, "info", self.config["info_color"])
2020-04-24 22:05:53 +02:00
2020-04-24 12:03:04 +02:00
async def on_log_error(self, error, *args, **kwargs):
2020-04-24 22:05:53 +02:00
channels = self.config["error_channels"]
2020-04-25 01:02:34 +02:00
await self.send_splited_embed(error, channels, "error", self.config["error_color"])
2020-04-24 22:05:53 +02:00
async def on_log_warning(self, warning, *args, **kwargs):
channels = self.config["warning_channels"]
2020-04-25 01:02:34 +02:00
await self.send_splited_embed(warning, channels, "warning", self.config["warning_color"])
2020-04-21 23:26:00 +02:00
__main_class__ = Logging