[mod errors] Fix await on non async function

This commit is contained in:
Suwako Moriya 2020-03-30 02:29:19 +00:00
parent 7a34e57a2d
commit 2738508875

View File

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