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

roxbot/settings/roxbot\.conf roxbot/settings/roxbot\.conf


roxbot/settings/db\.sqlite roxbot/settings/db\.sqlite

roxbot/settings/db.sqlite

+ 1
- 2
roxbot.py View File

import roxbot import roxbot
from roxbot import guild_settings as gs from roxbot import guild_settings as gs
from roxbot import core from roxbot import core
from roxbot import db




class term: class term:
print("{} FAILED TO LOAD. MISSING REQUIREMENTS".format(cog.split(".")[2])) 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. # 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(term.seperator)
print("Client logging in...", end="\r") print("Client logging in...", end="\r")

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



class AdminSingle(db.Entity): class AdminSingle(db.Entity):
warning_limit = Required(int, default=0) 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): class AdminWarnings(db.Entity):


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


@commands.guild_only() @commands.guild_only()
@commands.has_permissions(manage_messages=True) @commands.has_permissions(manage_messages=True)


# Warning in the settings is a dictionary of user ids. The user ids are equal to a list of dictionaries. # Warning in the settings is a dictionary of user ids. The user ids are equal to a list of dictionaries.
with db_session: 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)[:] 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) amount_warnings = len(user_warnings)


return await ctx.send(page) return await ctx.send(page)
else: else:
with db_session: 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: if not user_warnings:
embed = discord.Embed(description=self.OK_WARN_LIST_USER_NO_WARNINGS, colour=roxbot.EmbedColours.orange) embed = discord.Embed(description=self.OK_WARN_LIST_USER_NO_WARNINGS, colour=roxbot.EmbedColours.orange)
raise commands.BadArgument(self.ERROR_WARN_SL_NEG) raise commands.BadArgument(self.ERROR_WARN_SL_NEG)


with db_session: 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 guild_settings.warning_limit = number_of_warnings
if number_of_warnings == 0: if number_of_warnings == 0:
embed = discord.Embed(description=self.OK_WARN_SL_SET_ZERO, colour=roxbot.EmbedColours.pink) embed = discord.Embed(description=self.OK_WARN_SL_SET_ZERO, colour=roxbot.EmbedColours.pink)

+ 13
- 0
roxbot/db.py View File



class Blacklist(db.Entity): class Blacklist(db.Entity):
user_id = Required(int, size=64) 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