2020-04-24 10:36:06 +02:00
|
|
|
import asyncio
|
2020-04-21 23:25:16 +02:00
|
|
|
import inspect
|
2020-04-24 10:36:06 +02:00
|
|
|
import traceback
|
2020-04-21 23:25:16 +02:00
|
|
|
|
|
|
|
from config import Config
|
|
|
|
import os
|
|
|
|
import toml
|
|
|
|
|
2020-04-24 10:36:06 +02:00
|
|
|
|
2020-04-27 13:36:35 +02:00
|
|
|
class BaseModule:
|
2020-04-24 10:36:06 +02:00
|
|
|
|
2020-04-21 23:25:16 +02:00
|
|
|
def __init__(self, client):
|
2020-04-24 10:36:06 +02:00
|
|
|
with open(os.path.join(os.path.dirname(inspect.getfile(self.__class__)), "infos.toml")) as f:
|
2020-04-21 23:25:16 +02:00
|
|
|
self.infos = toml.load(f)
|
|
|
|
self.name = self.infos["name"]
|
|
|
|
self.client = client
|
2020-07-04 11:13:51 +02:00
|
|
|
self.config = self.client.get_config(path=os.path.join(self.name.lower(), "config.toml"))
|
2020-04-24 10:36:06 +02:00
|
|
|
|
|
|
|
async def _run_event(self, coro, event_name, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
await coro(*args, **kwargs)
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
pass
|
|
|
|
except Exception:
|
|
|
|
try:
|
2020-04-24 12:02:03 +02:00
|
|
|
self.client.dispatch('error', event_name, traceback.format_exc(), *args, **kwargs)
|
2020-04-24 10:36:06 +02:00
|
|
|
except asyncio.CancelledError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __dispatch__(self, event, *args, **kwargs):
|
|
|
|
method = 'on_' + event
|
|
|
|
try:
|
|
|
|
coro = getattr(self, method)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
asyncio.ensure_future(self._run_event(coro, method, *args, **kwargs), loop=self.client.loop)
|
2020-04-27 18:37:44 +02:00
|
|
|
|
|
|
|
async def on_config_update(self):
|
|
|
|
self.config.load()
|