LBI/modules/errors.py

113 lines
4.4 KiB
Python
Raw Normal View History

2019-02-25 20:29:13 +01:00
import asyncio
import collections
2019-03-02 14:38:37 +01:00
import os.path
import pickle
import random
import traceback
2019-02-25 20:29:13 +01:00
from subprocess import call
2019-03-02 14:38:37 +01:00
import discord
moduleFiles = "errors"
2019-02-25 20:29:13 +01:00
class MainClass():
def saveObject(self, object, objectname):
2019-03-02 14:38:37 +01:00
with open("storage/%s/" % moduleFiles + objectname + "tmp", "wb") as pickleFile:
2019-02-25 20:29:13 +01:00
pickler = pickle.Pickler(pickleFile)
pickler.dump(object)
2019-03-02 14:38:37 +01:00
call(['mv', "storage/%s/" % moduleFiles + objectname + "tmp", "storage/%s/" % moduleFiles + objectname])
2019-02-25 20:29:13 +01:00
def loadObject(self, objectname):
if self.saveExists(objectname):
2019-03-02 14:38:37 +01:00
with open("storage/%s/" % moduleFiles + objectname, "rb") as pickleFile:
2019-02-25 20:29:13 +01:00
unpickler = pickle.Unpickler(pickleFile)
return unpickler.load()
def saveExists(self, objectname):
2019-03-02 14:38:37 +01:00
return os.path.isfile("storage/%s/" % moduleFiles + objectname)
2019-02-26 16:31:43 +01:00
def __init__(self, client, modules, owners, prefix):
2019-03-02 14:38:37 +01:00
if not os.path.isdir("storage/%s" % moduleFiles):
call(['mkdir', 'storage/%s' % moduleFiles])
self.errorsDeque = None
self.devchanids = [549662392120901633]
self.memes = [
"https://cdn.discordapp.com/avatars/436105272310759426/6e6850c03fba976f45295a76410a6699.png?size=64",
"https://cdn.discordapp.com/avatars/281166473102098433/6e92bff42c9f409334e1580c9f666228.png?size=64",
"https://cdn.discordapp.com/avatars/318866596502306816/8e1792132f5b9ceaca6f7aea1fd489f0.png?size=64"
2019-02-25 20:29:13 +01:00
]
2019-03-02 14:38:37 +01:00
self.icon = "https://moriya.zapto.org/avatars/5?s=140"
2019-02-25 20:29:13 +01:00
self.client = client
self.modules = modules
self.owners = owners
self.prefix = prefix
2019-03-02 14:38:37 +01:00
self.events = ['on_error', 'on_message', 'on_ready'] # events list
self.command = "%slicorne" % self.prefix # command prefix (can be empty to catch every single messages)
2019-02-25 20:29:13 +01:00
2019-03-02 14:38:37 +01:00
self.name = "Error Handling"
self.description = "Module de gestions des erreurs"
self.interactive = True
self.authlist = []
self.color = 0xdb1348
self.help = """\
2019-02-25 20:29:13 +01:00
</prefix>licorne
=> Crée une erreur.
"""
2019-03-02 14:38:37 +01:00
2019-02-25 20:29:13 +01:00
async def on_ready(self):
if self.saveExists('errorsDeque'):
2019-03-02 14:38:37 +01:00
self.errorsDeque = self.loadObject('errorsDeque')
2019-02-25 20:29:13 +01:00
else:
2019-03-02 14:38:37 +01:00
self.errorsDeque = collections.deque()
2019-02-25 20:29:13 +01:00
for i in range(len(self.errorsDeque)):
try:
2019-03-02 14:38:37 +01:00
messagelst = self.errorsDeque.popleft()
2019-02-25 20:29:13 +01:00
channel = self.client.get_channel(messagelst[0])
delete_message = await channel.get_message(messagelst[1])
await delete_message.delete()
except:
raise
self.saveObject(self.errorsDeque, 'errorsDeque')
2019-03-02 14:38:37 +01:00
2019-02-25 20:29:13 +01:00
async def on_message(self, message):
2019-03-02 14:38:37 +01:00
5 / 0
2019-02-25 20:29:13 +01:00
async def on_error(self, event, *args, **kwargs):
2019-03-02 14:38:37 +01:00
embed = discord.Embed(title="Aïe :/", description="```PYTHON\n{0}```".format(traceback.format_exc()),
color=self.color).set_image(url=random.choice(self.memes))
2019-02-25 20:29:13 +01:00
messagelst = None
try:
2019-03-02 14:38:37 +01:00
message = await args[0].channel.send(
embed=embed.set_footer(text="Ce message s'autodétruira dans une minute.", icon_url=self.icon))
messagelst = [message.channel.id, message.id]
2019-02-25 20:29:13 +01:00
self.errorsDeque.append(messagelst)
except:
try:
2019-03-02 14:38:37 +01:00
message = args[1].channel.send(
embed=embed.set_footer(text="Ce message s'autodétruira dans une minute.", icon_url=self.icon))
messagelst = [message.channel.id, message.id]
2019-02-25 20:29:13 +01:00
self.errorsDeque.append(messagelst)
except:
pass
for chanid in self.devchanids:
try:
2019-03-02 14:38:37 +01:00
await self.client.get_channel(chanid).send(
embed=embed.set_footer(text="Ce message ne s'autodétruira pas.", icon_url=self.icon))
2019-02-25 20:29:13 +01:00
except:
pass
self.saveObject(self.errorsDeque, 'errorsDeque')
await asyncio.sleep(60)
try:
channel = self.client.get_channel(messagelst[0])
delete_message = await channel.get_message(messagelst[1])
await delete_message.delete()
except:
raise
finally:
try:
self.errorsDeque.remove(messagelst)
except:
pass
self.saveObject(self.errorsDeque, 'errorsDeque')