Titan/webapp/titanembeds/database/cosmetics.py

58 lines
1.9 KiB
Python
Raw Permalink Normal View History

from titanembeds.database import db
2017-12-07 07:49:32 +01:00
import json
class Cosmetics(db.Model):
__tablename__ = "cosmetics"
user_id = db.Column(db.BigInteger, nullable=False, primary_key=True) # Discord user id of user of cosmetics
2017-07-22 04:06:45 +02:00
css = db.Column(db.Boolean(), nullable=False) # If they can create/edit custom CSS
css_limit = db.Column(db.Integer, nullable=False, server_default="0") # Custom CSS Limit
guest_icon = db.Column(db.Boolean(), nullable=False, server_default=db.false()) # If they can set the guest icon for all guilds
2017-12-07 07:49:32 +01:00
badges = db.Column(db.String(255), nullable=False, server_default="[]") # JSON list of all the badges the user has
2017-07-22 04:06:45 +02:00
def __init__(self, user_id, **kwargs):
self.user_id = user_id
if "css" in kwargs:
self.css = kwargs["css"]
else:
self.css = False
if "css_limit" in kwargs:
self.css_limit = kwargs["css_limit"]
else:
self.css_limit = 0
if "guest_icon" in kwargs:
self.guest_icon = kwargs["guest_icon"]
else:
2017-12-07 07:49:32 +01:00
self.guest_icon = False
if "badges" in kwargs:
self.badges = json.dumps(kwargs["badges"])
else:
self.badges = "[]"
def set_badges(user_id, badges):
usr = db.session.query(Cosmetics).filter(Cosmetics.user_id == user_id).first()
if not usr:
usr = Cosmetics(user_id)
usr.badges = json.dumps(badges)
db.session.add(usr)
def get_badges(user_id):
usr = db.session.query(Cosmetics).filter(Cosmetics.user_id == user_id).first()
if usr:
return json.loads(usr.badges)
return []
def add_badge(user_id, name):
bgs = get_badges(user_id)
if name not in bgs:
bgs.append(name)
set_badges(user_id, bgs)
def remove_badge(user_id, name):
bgs = get_badges(user_id)
if name in bgs:
bgs.remove(name)
set_badges(user_id, bgs)