Added config system

master
jef 2020-08-08 09:29:32 +02:00
parent 2f9d1a75a0
commit 18471abe13
5 changed files with 25 additions and 5 deletions

2
frank.yaml 100644
View File

@ -0,0 +1,2 @@
test:
channel_id: 740301700606197918

View File

@ -1,22 +1,35 @@
import shlex import shlex
from typing import List from typing import List
import discord import discord
import yaml
class Frank(discord.Client): class Frank(discord.Client):
PREFIX = "fr" PREFIX = "fr"
def __init__(self, modules: List["Module"]): def __init__(self, modules: List["Module"], config_file: str = "frank.yaml"):
super().__init__() super().__init__()
self._modules = modules self._modules = modules
self._loaded_modules = [] self._loaded_modules = []
try:
with open(config_file, "r") as f:
self._settings = yaml.load(f, Loader=yaml.FullLoader)
except FileNotFoundError:
self._settings = None
async def on_ready(self): async def on_ready(self):
print("Connected") print("Connected")
# Startup all modules # Startup all modules
for module in self._modules: for module in self._modules:
loaded = module(self) if self._settings and module.NAME in self._settings:
loaded = module(self, settings=self._settings[module.NAME])
else:
loaded = module(self)
await loaded.start() await loaded.start()
self._loaded_modules.append(loaded) self._loaded_modules.append(loaded)

View File

@ -1,11 +1,13 @@
from typing import List from typing import List, Dict
class Module: class Module:
PREFIX = None PREFIX = None
NAME = None
def __init__(self, client: "Frank"): def __init__(self, client: "Frank", settings: Dict = None):
self._client = client self._client = client
self._settings = settings
async def start(self): async def start(self):
pass pass

View File

@ -4,7 +4,9 @@ from .. import Module
class TestMod(Module): class TestMod(Module):
PREFIX = "test" PREFIX = "test"
NAME = "test"
async def command(self, cmd: List[str]): async def command(self, cmd: List[str]):
if cmd[0] == "test": if cmd[0] == "test":
await self._client.get_channel(740301700606197918).send("this is frank speaking") channel = self._client.get_channel(self._settings["channel_id"])
await channel.send("psycho frank is in the house")

View File

@ -2,3 +2,4 @@ discord.py
pylint pylint
jedi jedi
python-dotenv python-dotenv
pyyaml