Added config system
parent
2f9d1a75a0
commit
18471abe13
|
@ -0,0 +1,2 @@
|
|||
test:
|
||||
channel_id: 740301700606197918
|
|
@ -1,22 +1,35 @@
|
|||
import shlex
|
||||
from typing import List
|
||||
import discord
|
||||
import yaml
|
||||
|
||||
|
||||
class Frank(discord.Client):
|
||||
PREFIX = "fr"
|
||||
|
||||
def __init__(self, modules: List["Module"]):
|
||||
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._settings = yaml.load(f, Loader=yaml.FullLoader)
|
||||
|
||||
except FileNotFoundError:
|
||||
self._settings = None
|
||||
|
||||
async def on_ready(self):
|
||||
print("Connected")
|
||||
|
||||
# Startup all modules
|
||||
for module in self._modules:
|
||||
if self._settings and module.NAME in self._settings:
|
||||
loaded = module(self, settings=self._settings[module.NAME])
|
||||
|
||||
else:
|
||||
loaded = module(self)
|
||||
|
||||
await loaded.start()
|
||||
self._loaded_modules.append(loaded)
|
||||
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
from typing import List
|
||||
from typing import List, Dict
|
||||
|
||||
|
||||
class Module:
|
||||
PREFIX = None
|
||||
NAME = None
|
||||
|
||||
def __init__(self, client: "Frank"):
|
||||
def __init__(self, client: "Frank", settings: Dict = None):
|
||||
self._client = client
|
||||
self._settings = settings
|
||||
|
||||
async def start(self):
|
||||
pass
|
||||
|
|
|
@ -4,7 +4,9 @@ from .. import Module
|
|||
|
||||
class TestMod(Module):
|
||||
PREFIX = "test"
|
||||
NAME = "test"
|
||||
|
||||
async def command(self, cmd: List[str]):
|
||||
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")
|
||||
|
|
|
@ -2,3 +2,4 @@ discord.py
|
|||
pylint
|
||||
jedi
|
||||
python-dotenv
|
||||
pyyaml
|
||||
|
|
Reference in New Issue