Browse Source

db now has a system to auto generate guild settings for each cog. Admin updated to use this.

tags/v2.2.0
Roxie Gibson 5 years ago
parent
commit
3177ee1d0e
4 changed files with 21 additions and 13 deletions
  1. +2
    -0
      .gitignore
  2. +1
    -2
      roxbot.py
  3. +5
    -11
      roxbot/cogs/admin.py
  4. +13
    -0
      roxbot/db.py

+ 2
- 0
.gitignore View File

@@ -22,3 +22,5 @@ site/
roxbot/settings/roxbot\.conf

roxbot/settings/db\.sqlite

roxbot/settings/db.sqlite

+ 1
- 2
roxbot.py View File

@@ -35,7 +35,6 @@ from discord.ext import commands
import roxbot
from roxbot import guild_settings as gs
from roxbot import core
from roxbot import db


class term:
@@ -177,7 +176,7 @@ if __name__ == "__main__":
print("{} FAILED TO LOAD. MISSING REQUIREMENTS".format(cog.split(".")[2]))

# This commits all the entities defined by the cogs. These are loaded above. Do Not Remove.
db.db.generate_mapping(create_tables=True)
bot.loop.create_task(roxbot.db.populate_db(bot))

print(term.seperator)
print("Client logging in...", end="\r")

+ 5
- 11
roxbot/cogs/admin.py View File

@@ -34,7 +34,7 @@ from roxbot.db import *

class AdminSingle(db.Entity):
warning_limit = Required(int, default=0)
guild = Required(int, unique=True, size=64)
guild_id = Required(int, unique=True, size=64)


class AdminWarnings(db.Entity):
@@ -82,13 +82,7 @@ class Admin:

def __init__(self, bot_client):
self.bot = bot_client
self.settings = {
"admin": {
"convert": {"warnings": "hide"},
"warning_limit": 0,
"warnings": {},
}
}
self.autogen_db = AdminSingle

@commands.guild_only()
@commands.has_permissions(manage_messages=True)
@@ -176,7 +170,7 @@ class Admin:

# Warning in the settings is a dictionary of user ids. The user ids are equal to a list of dictionaries.
with db_session:
warning_limit = AdminSingle.get(guild=ctx.guild.id).warning_limit
warning_limit = AdminSingle.get(guild_id=ctx.guild.id).warning_limit
user_warnings = select(w for w in AdminWarnings if w.user_id == user.id and w.guild_id == ctx.guild.id)[:]
amount_warnings = len(user_warnings)

@@ -233,7 +227,7 @@ class Admin:
return await ctx.send(page)
else:
with db_session:
user_warnings = select(w for w in AdminWarnings if w.user_id == user.id and w.guild_id == ctx.guild.id)[:]
user_warnings = select(w for w in AdminWarnings if w.user_id == user.id and w.guild_id == ctx.guild.id).order_by(AdminWarnings.date)[:]

if not user_warnings:
embed = discord.Embed(description=self.OK_WARN_LIST_USER_NO_WARNINGS, colour=roxbot.EmbedColours.orange)
@@ -341,7 +335,7 @@ class Admin:
raise commands.BadArgument(self.ERROR_WARN_SL_NEG)

with db_session:
guild_settings = AdminSingle.get(guild=ctx.guild.id)
guild_settings = AdminSingle.get(guild_id=ctx.guild.id)
guild_settings.warning_limit = number_of_warnings
if number_of_warnings == 0:
embed = discord.Embed(description=self.OK_WARN_SL_SET_ZERO, colour=roxbot.EmbedColours.pink)

+ 13
- 0
roxbot/db.py View File

@@ -34,3 +34,16 @@ class Guilds(db.Entity):

class Blacklist(db.Entity):
user_id = Required(int, size=64)


async def populate_db(bot):
db.generate_mapping(create_tables=True)
await bot.wait_for("ready")
for name, cog in bot.cogs.items():
try:
if cog.autogen_db:
for guild in bot.guilds:
with db_session:
cog.autogen_db(guild_id=guild.id)
except (AttributeError, TransactionIntegrityError):
pass # No DB settings or already in place

Loading…
Cancel
Save