This repository has been archived on 2021-03-28. You can view files and clone it, but cannot push or open issues/pull-requests.
frank/frank/frank.py

48 lines
1.2 KiB
Python
Raw Normal View History

2020-08-08 09:19:09 +02:00
import shlex
from typing import List
import discord
2020-08-08 09:29:32 +02:00
import yaml
2020-08-08 09:19:09 +02:00
class Frank(discord.Client):
PREFIX = "fr"
2020-08-08 09:29:32 +02:00
def __init__(self, modules: List["Module"], config_file: str = "frank.yaml"):
2020-08-08 09:19:09 +02:00
super().__init__()
self._modules = modules
self._loaded_modules = []
2020-08-08 09:29:32 +02:00
try:
with open(config_file, "r") as f:
2020-08-08 13:12:09 +02:00
self._config = yaml.load(f, Loader=yaml.FullLoader)
2020-08-08 09:29:32 +02:00
except FileNotFoundError:
2020-08-08 13:12:09 +02:00
self._config = None
2020-08-08 09:29:32 +02:00
2020-08-08 09:19:09 +02:00
async def on_ready(self):
print("Connected")
# Startup all modules
for module in self._modules:
2020-08-08 13:12:09 +02:00
if self._config and module.NAME in self._config:
loaded = module(self, config=self._config[module.NAME])
2020-08-08 09:29:32 +02:00
else:
loaded = module(self)
2020-08-08 09:19:09 +02:00
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] == "fr":
matched_mods = (mod for mod in self._loaded_modules if mod.PREFIX == cmd[1])
module = next(matched_mods, None)
if module:
await module.command(cmd[2:])