2019-04-28 18:59:08 +02:00
|
|
|
import asyncio
|
2019-08-16 19:11:21 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
import datetime
|
2019-04-28 18:59:08 +02:00
|
|
|
import random
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
import collections
|
|
|
|
import discord
|
2019-08-16 19:11:21 +02:00
|
|
|
from discord import Message
|
2019-04-28 18:59:08 +02:00
|
|
|
|
2019-06-05 02:23:11 +02:00
|
|
|
from modules.base import BaseClassPython
|
2019-04-28 18:59:08 +02:00
|
|
|
|
|
|
|
|
2019-06-05 02:23:11 +02:00
|
|
|
class MainClass(BaseClassPython):
|
2019-04-28 18:59:08 +02:00
|
|
|
name = "errors"
|
|
|
|
description = "Error handling"
|
|
|
|
interactive = True
|
2019-08-10 09:30:46 +02:00
|
|
|
authorized_users = [431043517217898496]
|
|
|
|
authorized_roles = []
|
2019-04-28 18:59:08 +02:00
|
|
|
color = 0xdb1348
|
|
|
|
help = {
|
|
|
|
"description": "Montre toutes les erreurs du bot dans discord.",
|
|
|
|
"commands": {
|
|
|
|
"`{prefix}{command}`": "Renvoie une erreur de test.",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
command_text = "unicorn"
|
|
|
|
|
|
|
|
def __init__(self, client):
|
|
|
|
super().__init__(client)
|
2019-08-16 19:11:21 +02:00
|
|
|
self.config.init({"dev_chan":[], "memes":[""], "icon":""})
|
2019-04-28 18:59:08 +02:00
|
|
|
self.errorsDeque = None
|
|
|
|
|
|
|
|
async def on_ready(self):
|
2019-06-10 17:46:33 +02:00
|
|
|
if self.objects.save_exists('errorsDeque'):
|
|
|
|
self.errorsDeque = self.objects.load_object('errorsDeque')
|
2019-04-28 18:59:08 +02:00
|
|
|
else:
|
|
|
|
self.errorsDeque = collections.deque()
|
|
|
|
for i in range(len(self.errorsDeque)):
|
|
|
|
try:
|
2019-08-16 19:11:21 +02:00
|
|
|
msg_id = self.errorsDeque.popleft()
|
|
|
|
channel = self.client.get_channel(msg_id["channel_id"])
|
|
|
|
to_delete = await channel.fetch_message(msg_id["msg_id"])
|
|
|
|
await to_delete.delete()
|
2019-04-28 18:59:08 +02:00
|
|
|
except:
|
|
|
|
raise
|
2019-08-08 22:43:46 +02:00
|
|
|
self.objects.save_object('errorsDeque', self.errorsDeque)
|
2019-04-28 18:59:08 +02:00
|
|
|
|
|
|
|
async def command(self, message, args, kwargs):
|
2019-08-08 22:43:46 +02:00
|
|
|
raise Exception("KERNEL PANIC!!!")
|
2019-04-28 18:59:08 +02:00
|
|
|
|
|
|
|
async def on_error(self, event, *args, **kwargs):
|
2019-08-16 19:11:21 +02:00
|
|
|
"""Send error message"""
|
|
|
|
# Search first channel instance found in arg, then search in kwargs
|
|
|
|
channel = None
|
|
|
|
for arg in args:
|
|
|
|
if type(arg) == Message:
|
|
|
|
channel = arg.channel
|
|
|
|
break
|
|
|
|
if type(arg) == discord.TextChannel:
|
|
|
|
channel = arg
|
|
|
|
break
|
|
|
|
if channel is None:
|
|
|
|
for _,v in kwargs.items():
|
|
|
|
if type(v) == discord.Message:
|
|
|
|
channel = v.channel
|
|
|
|
break
|
|
|
|
if type(v) == discord.TextChannel:
|
|
|
|
channel = v
|
|
|
|
break# Create embed
|
|
|
|
embed = discord.Embed(
|
|
|
|
title="[Erreur] Aïe :/",
|
|
|
|
description="```python\n{0}```".format(traceback.format_exc()),
|
|
|
|
color=self.color)
|
|
|
|
embed.set_image(url=random.choice(self.config["memes"]))
|
2019-04-28 18:59:08 +02:00
|
|
|
message_list = None
|
2019-08-16 19:11:21 +02:00
|
|
|
|
|
|
|
# Send message to dev channels
|
2019-08-10 17:58:54 +02:00
|
|
|
for chanid in self.config["dev_chan"]:
|
2019-04-28 18:59:08 +02:00
|
|
|
try:
|
|
|
|
await self.client.get_channel(chanid).send(
|
|
|
|
embed=embed.set_footer(text="Ce message ne s'autodétruira pas.", icon_url=self.icon))
|
|
|
|
except:
|
|
|
|
pass
|
2019-08-16 19:11:21 +02:00
|
|
|
# Send message to current channel if exists
|
|
|
|
if channel is not None:
|
|
|
|
message = await channel.send(embed=embed.set_footer(text="Ce message va s'autodétruire dans une minute",
|
|
|
|
icon_url=self.config["icon"]))
|
|
|
|
msg_id = {"chan_id": message.channel.id, "msg_id": message.id}
|
|
|
|
self.errorsDeque.append(msg_id)
|
|
|
|
# Save message in errorsDeque now to keep them if a reboot happend during next 60 seconds
|
|
|
|
self.objects.save_object('errorsDeque', self.errorsDeque)
|
|
|
|
|
|
|
|
# Wait 60 seconds and delete message
|
|
|
|
await asyncio.sleep(60)
|
2019-04-28 18:59:08 +02:00
|
|
|
try:
|
2019-08-16 19:11:21 +02:00
|
|
|
channel = self.client.get_channel(msg_id["chan_id"])
|
|
|
|
delete_message = await channel.fetch_message(msg_id["msg_id"])
|
|
|
|
await delete_message.delete()
|
|
|
|
except:
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
self.errorsDeque.remove(msg_id)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
# Save now to avoid deleting unkown message
|
|
|
|
self.objects.save_object('errorsDeque', self.errorsDeque)
|