i did something

This commit is contained in:
fanyx 2021-05-23 17:50:31 +02:00
parent ec6ab3e5d2
commit 7f81eb1420
9 changed files with 56 additions and 25 deletions

4
.gitignore vendored
View File

@ -1,2 +1,4 @@
venv venv/
env/
__pycache__/ __pycache__/
bot.db

12
main.py
View File

@ -1,10 +1,16 @@
import sys
from os import listdir from os import listdir
from discord.ext.commands import Bot 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' # 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 # spawn discord bot instance
# init token from config # init token from config

View File

View File

@ -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])

17
src/database/models.py Normal file
View File

@ -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)

View File

@ -0,0 +1,2 @@
def setup(_):
pass

View File

@ -1,6 +1,11 @@
import discord import discord
from discord.ext import commands 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): class Events(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
@ -15,7 +20,9 @@ class Events(commands.Cog):
- activity - activity
- presence - 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): def setup(bot):
bot.add_cog(Events(bot)) bot.add_cog(Events(bot))

View File

@ -1,11 +1,10 @@
from discord.ext import commands from discord.ext import commands
from src.utils.config import build_config from main import config
class Inventory(commands.Cog): class Inventory(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.config = build_config() # self.config = config
@commands.group() @commands.group()
@commands.guild_only() @commands.guild_only()
@ -31,7 +30,7 @@ class Inventory(commands.Cog):
@inventory.command(name="use") @inventory.command(name="use")
@commands.guild_only() @commands.guild_only()
async def inventory_use(self, ctx, *, ): async def inventory_use(self, ctx, **kwargs):
""" """
Use an item in user's inventory Use an item in user's inventory
@ -40,4 +39,5 @@ class Inventory(commands.Cog):
pass pass
def setup(bot):
bot.add_cog(Inventory(bot))

View File

@ -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