Ajout des warns et des infos

This commit is contained in:
Louis Chauvet 2020-04-24 22:05:53 +02:00
parent 2f1dc1f4dc
commit ed56e5ee3a
Signed by: fomys
GPG Key ID: 1ECA046A9615ABA0

View File

@ -9,36 +9,51 @@ import traceback
class Logging(BasePython):
def __init__(self, client):
super().__init__(client)
self.config.register("log_channel", factory(c_t.discord_types.Channel, 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.load()
async def on_log_error(self, error, *args, **kwargs):
try:
channel = self.config["log_channel"]
error = error.splitlines(keepends=True)
errors = []
for e in error:
if len(e) < 1900:
errors.append(e)
a = e
while len(a) > 1900:
errors.append(a[:1900])
a = a[1900:]
errors.append(a)
async def send_splited_embed(self, content, channels, title):
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)
error_contents = []
while len(errors):
error_contents.append("")
while len(errors) and len(errors[0]) < 2000 - len(error_contents[-1]):
error_contents[-1] += errors.pop(0)
await channel.send("There was an error:")
for i, content in enumerate(error_contents):
embed = discord.Embed(title=f"Error {i+1}/{len(error_contents)}",
description=f"```py\n{content}\n```")
await channel.send(embed=embed)
except: # TODO: C'est pas beau mais il ne faut absolument pas que on_log_error lève une exception pour éviter
# TODO: les récursions infinies
print(traceback.format_exc())
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```")
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"]
await self.send_splited_embed(info, channels, "info")
async def on_log_error(self, error, *args, **kwargs):
channels = self.config["error_channels"]
await self.send_splited_embed(error, channels, "error")
async def on_log_warning(self, warning, *args, **kwargs):
channels = self.config["warning_channels"]
await self.send_splited_embed(warning, channels, "warning")
__main_class__ = Logging