51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
import shlex
|
|
from typing import List
|
|
import discord
|
|
import yaml
|
|
|
|
|
|
class Frank(discord.Client):
|
|
PREFIX = "fr"
|
|
|
|
def __init__(self, modules: List["Module"],
|
|
config_file: str = "frank.yaml"):
|
|
super().__init__()
|
|
self._modules = modules
|
|
self._loaded_modules = []
|
|
|
|
try:
|
|
with open(config_file, "r") as f:
|
|
self._config = yaml.load(f, Loader=yaml.FullLoader)
|
|
|
|
except FileNotFoundError:
|
|
self._config = None
|
|
|
|
async def on_ready(self):
|
|
print("Connected")
|
|
|
|
# Startup all modules
|
|
for module in self._modules:
|
|
if self._config and module.NAME in self._config:
|
|
loaded = module(self, config=self._config[module.NAME])
|
|
|
|
else:
|
|
loaded = module(self)
|
|
|
|
await loaded.start()
|
|
self._loaded_modules.append(loaded)
|
|
|
|
print("All modules loaded")
|
|
|
|
async def on_message(self, message: str):
|
|
cmd = shlex.split(message.content.strip())
|
|
|
|
if cmd[0] == self.PREFIX:
|
|
matched_mods = (
|
|
mod for mod in self._loaded_modules if mod.match(cmd[1])
|
|
)
|
|
|
|
module = next(matched_mods, None)
|
|
|
|
if module:
|
|
await module.command(cmd[2:])
|