From 7f81eb142074811ea8ad57f58cfce7b14e87a059 Mon Sep 17 00:00:00 2001 From: fanyx Date: Sun, 23 May 2021 17:50:31 +0200 Subject: [PATCH] i did something --- .gitignore | 6 ++++-- main.py | 12 +++++++++--- src/database/migrations/__init__.py | 0 src/database/migrations/core.py | 10 ++++++++++ src/database/models.py | 17 +++++++++++++++++ src/ext/__init__.py | 2 ++ src/ext/events.py | 11 +++++++++-- src/ext/inventory.py | 10 +++++----- src/utils/config.py | 13 ------------- 9 files changed, 56 insertions(+), 25 deletions(-) create mode 100644 src/database/migrations/__init__.py create mode 100644 src/database/migrations/core.py create mode 100644 src/database/models.py delete mode 100644 src/utils/config.py diff --git a/.gitignore b/.gitignore index 499ca79..b32271a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -venv -__pycache__/ \ No newline at end of file +venv/ +env/ +__pycache__/ +bot.db \ No newline at end of file diff --git a/main.py b/main.py index 2a7d0c5..a261e79 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,16 @@ -import sys from os import listdir from discord.ext.commands import Bot -from src.utils.config import build_config +from configparser import ConfigParser +from peewee import SqliteDatabase # read config at configured location \\ default to 'config.ini' -config = build_config() +config = ConfigParser() +with open('config.ini', 'r') as configfile: + config.read_file(configfile) + +# init db +db = SqliteDatabase(config['DATABASE']['path']) +db.connect() # spawn discord bot instance # init token from config diff --git a/src/database/migrations/__init__.py b/src/database/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/database/migrations/core.py b/src/database/migrations/core.py new file mode 100644 index 0000000..8096dfb --- /dev/null +++ b/src/database/migrations/core.py @@ -0,0 +1,10 @@ +from src.database.models import Role, User, UserRole +from main import db + +import logging + +logger = logging.getLogger('database') + +def create_tables(): + logger.info('Creating core database tables if necessary.') + db.create_tables([User, Role, UserRole]) \ No newline at end of file diff --git a/src/database/models.py b/src/database/models.py new file mode 100644 index 0000000..caba828 --- /dev/null +++ b/src/database/models.py @@ -0,0 +1,17 @@ +from peewee import Model, CharField, ForeignKeyField +from main import config, db + +class BaseModel(Model): + class Meta: + database = db + +class User(BaseModel): + uuid = CharField(unique=True) + +class Role(BaseModel): + name = CharField() + +class UserRole(BaseModel): + user = ForeignKeyField(User, User.uuid) + role = ForeignKeyField(Role, Role.name) + diff --git a/src/ext/__init__.py b/src/ext/__init__.py index e69de29..cae5782 100644 --- a/src/ext/__init__.py +++ b/src/ext/__init__.py @@ -0,0 +1,2 @@ +def setup(_): + pass \ No newline at end of file diff --git a/src/ext/events.py b/src/ext/events.py index 0434217..5419e8f 100644 --- a/src/ext/events.py +++ b/src/ext/events.py @@ -1,6 +1,11 @@ import discord from discord.ext import commands -from peewee import * +import logging + +import src.database.migrations.core + +logger = logging.getLogger(__name__) +logger.setLevel(logging.INFO) class Events(commands.Cog): def __init__(self, bot): @@ -15,7 +20,9 @@ class Events(commands.Cog): - activity - presence """ - print(f"[INFO]: Startup complete | {self.bot.user}") + logger.info("Running database startup migrations.") + src.database.migrations.core.create_tables() + logger.info(f"Startup complete | {self.bot.user}") def setup(bot): bot.add_cog(Events(bot)) \ No newline at end of file diff --git a/src/ext/inventory.py b/src/ext/inventory.py index 6f682b9..0e2d80e 100644 --- a/src/ext/inventory.py +++ b/src/ext/inventory.py @@ -1,11 +1,10 @@ from discord.ext import commands -from src.utils.config import build_config - +from main import config class Inventory(commands.Cog): def __init__(self, bot): self.bot = bot - self.config = build_config() + # self.config = config @commands.group() @commands.guild_only() @@ -31,7 +30,7 @@ class Inventory(commands.Cog): @inventory.command(name="use") @commands.guild_only() - async def inventory_use(self, ctx, *, ): + async def inventory_use(self, ctx, **kwargs): """ Use an item in user's inventory @@ -40,4 +39,5 @@ class Inventory(commands.Cog): pass - +def setup(bot): + bot.add_cog(Inventory(bot)) \ No newline at end of file diff --git a/src/utils/config.py b/src/utils/config.py deleted file mode 100644 index 47afc4f..0000000 --- a/src/utils/config.py +++ /dev/null @@ -1,13 +0,0 @@ -from os import getenv - -from configparser import ConfigParser - -def build_config(): - config_path = getenv("AKWAH_CONFIG_PATH") - if config_path == None: - config_path = "config.ini" - - config = ConfigParser() - config.read_file(open(config_path)) - return config -