39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
import asyncio
|
|
import inspect
|
|
import traceback
|
|
|
|
from config import Config
|
|
import os
|
|
import toml
|
|
|
|
|
|
class BasePython:
|
|
|
|
def __init__(self, client):
|
|
with open(os.path.join(os.path.dirname(inspect.getfile(self.__class__)), "infos.toml")) as f:
|
|
self.infos = toml.load(f)
|
|
self.name = self.infos["name"]
|
|
self.client = client
|
|
self.config = Config(path=os.path.join(client.config["data_folder"], self.name.lower(), "config.toml"))
|
|
|
|
async def _run_event(self, coro, event_name, *args, **kwargs):
|
|
try:
|
|
await coro(*args, **kwargs)
|
|
except asyncio.CancelledError:
|
|
pass
|
|
except Exception:
|
|
traceback.print_exc()
|
|
try:
|
|
self.client.dispatch('error', event_name, *args, **kwargs)
|
|
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)
|