Started creating config file system

This commit is contained in:
2020-11-20 21:01:41 +01:00
parent ae2e63da56
commit 60d6bd2c40
3 changed files with 63 additions and 0 deletions

45
bots/discordbot.py Normal file
View File

@@ -0,0 +1,45 @@
import sys
from discord.ext import commands
import os
import yaml
class DiscordBot:
def __init__(self, config):
print("jajajah")
self.constants = ["token"]
self.modules = {"quiz": "quiz"}
prefixes = ["/"]
self.client = commands.Bot(command_prefix=prefixes, case_insensitive=True)
self.client.prefixes = prefixes
self.load_config(config)
# Load this cog first so the other cogs can use properties initialized here
self.client.load_extension("cogs.events")
# for file in os.listdir("./cogs"):
# if file.endswith(".py") and not (file.startswith(("events",))):
# self.client.load_extension(f"cogs.{file[:-3]}")
def run(self):
if "token" in self.client.config.keys():
print(self.client.config["token"])
self.client.run(self.client.config["token"])
def get_client(self):
return self.client
def load_config(self, config):
with open(config, 'r') as file:
try:
self.client.config = yaml.safe_load(file)
for cog in self.client.config:
if cog not in self.constants and self.loaded_cog(cog):
print(f"Loaded cog {cog}")
except yaml.YAMLError as exc:
print(exc)
def loaded_cog(self, module):
if os.path.isfile(f"/cogs/{self.modules[module]}.py"):
self.client.load_extension(f"cogs.{self.modules[module]}")
return True
return False