Use a more optimal primary key and integer to store snowflakes

This commit is contained in:
Jeremy Zhang 2018-01-23 21:08:55 +00:00
parent ff42f4195b
commit aa199ad818
24 changed files with 246 additions and 84 deletions

View File

@ -54,9 +54,9 @@ class DatabaseInterface(object):
edit_ts = str(edit_ts) edit_ts = str(edit_ts)
msg = Messages( msg = Messages(
message.server.id, int(message.server.id),
message.channel.id, int(message.channel.id),
message.id, int(message.id),
message.content, message.content,
json.dumps(get_message_author(message)), json.dumps(get_message_author(message)),
str(message.timestamp), str(message.timestamp),
@ -91,9 +91,9 @@ class DatabaseInterface(object):
async with threadpool(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
msg = session.query(Messages) \ msg = session.query(Messages) \
.filter(Messages.guild_id == message.server.id) \ .filter(Messages.guild_id == int(message.server.id)) \
.filter(Messages.channel_id == message.channel.id) \ .filter(Messages.channel_id == int(message.channel.id)) \
.filter(Messages.message_id == message.id).first() .filter(Messages.message_id == int(message.id)).first()
if msg: if msg:
session.delete(msg) session.delete(msg)
session.commit() session.commit()
@ -108,13 +108,13 @@ class DatabaseInterface(object):
gui = session.query(Guilds).filter(Guilds.guild_id == guild.id).first() gui = session.query(Guilds).filter(Guilds.guild_id == guild.id).first()
if not gui: if not gui:
gui = Guilds( gui = Guilds(
guild.id, int(guild.id),
guild.name, guild.name,
json.dumps(get_roles_list(guild.roles)), json.dumps(get_roles_list(guild.roles)),
json.dumps(get_channels_list(guild.channels)), json.dumps(get_channels_list(guild.channels)),
json.dumps(get_webhooks_list(server_webhooks)), json.dumps(get_webhooks_list(server_webhooks)),
json.dumps(get_emojis_list(guild.emojis)), json.dumps(get_emojis_list(guild.emojis)),
guild.owner_id, int(guild.owner_id),
guild.icon guild.icon
) )
session.add(gui) session.add(gui)
@ -124,7 +124,7 @@ class DatabaseInterface(object):
gui.channels = json.dumps(get_channels_list(guild.channels)) gui.channels = json.dumps(get_channels_list(guild.channels))
gui.webhooks = json.dumps(get_webhooks_list(server_webhooks)) gui.webhooks = json.dumps(get_webhooks_list(server_webhooks))
gui.emojis = json.dumps(get_emojis_list(guild.emojis)) gui.emojis = json.dumps(get_emojis_list(guild.emojis))
gui.owner_id = guild.owner_id gui.owner_id = int(guild.owner_id)
gui.icon = guild.icon gui.icon = guild.icon
session.commit() session.commit()
@ -137,7 +137,7 @@ class DatabaseInterface(object):
disguild = discord.utils.get(guilds, id=guild.guild_id) disguild = discord.utils.get(guilds, id=guild.guild_id)
if not disguild: if not disguild:
changed = True changed = True
dbmsgs = session.query(Messages).filter(Messages.guild_id == guild.guild_id).all() dbmsgs = session.query(Messages).filter(Messages.guild_id == int(guild.guild_id)).all()
for msg in dbmsgs: for msg in dbmsgs:
session.delete(msg) session.delete(msg)
session.delete(guild) session.delete(guild)
@ -147,9 +147,9 @@ class DatabaseInterface(object):
async def remove_guild(self, guild): async def remove_guild(self, guild):
async with threadpool(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
gui = session.query(Guilds).filter(Guilds.guild_id == guild.id).first() gui = session.query(Guilds).filter(Guilds.guild_id == int(guild.id)).first()
if gui: if gui:
dbmsgs = session.query(Messages).filter(Messages.guild_id == guild.id).all() dbmsgs = session.query(Messages).filter(Messages.guild_id == int(guild.id)).all()
for msg in dbmsgs: for msg in dbmsgs:
session.delete(msg) session.delete(msg)
session.delete(gui) session.delete(gui)
@ -159,13 +159,13 @@ class DatabaseInterface(object):
async with threadpool(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
dbmember = session.query(GuildMembers) \ dbmember = session.query(GuildMembers) \
.filter(GuildMembers.guild_id == member.server.id) \ .filter(GuildMembers.guild_id == int(member.server.id)) \
.filter(GuildMembers.user_id == member.id) \ .filter(GuildMembers.user_id == int(member.id)) \
.order_by(GuildMembers.id).all() .order_by(int(GuildMembers.id)).all()
if not dbmember: if not dbmember:
dbmember = GuildMembers( dbmember = GuildMembers(
member.server.id, int(member.server.id),
member.id, int(member.id),
member.name, member.name,
member.discriminator, member.discriminator,
member.nick, member.nick,
@ -193,8 +193,8 @@ class DatabaseInterface(object):
async with threadpool(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
dbmember = session.query(GuildMembers) \ dbmember = session.query(GuildMembers) \
.filter(GuildMembers.guild_id == server.id) \ .filter(GuildMembers.guild_id == int(server.id)) \
.filter(GuildMembers.user_id == user.id).first() .filter(GuildMembers.user_id == int(user.id)).first()
if dbmember: if dbmember:
dbmember.banned = False dbmember.banned = False
session.commit() session.commit()
@ -204,7 +204,7 @@ class DatabaseInterface(object):
with self.get_session() as session: with self.get_session() as session:
changed = False changed = False
dbmembers = session.query(GuildMembers) \ dbmembers = session.query(GuildMembers) \
.filter(GuildMembers.guild_id == guild_id) \ .filter(GuildMembers.guild_id == int(guild_id)) \
.filter(GuildMembers.active == True).all() .filter(GuildMembers.active == True).all()
for member in dbmembers: for member in dbmembers:
dismember = discord.utils.get(guild_members, id=member.user_id) dismember = discord.utils.get(guild_members, id=member.user_id)
@ -220,16 +220,16 @@ class DatabaseInterface(object):
changed = False changed = False
for usr in guildbans: for usr in guildbans:
dbusr = session.query(GuildMembers) \ dbusr = session.query(GuildMembers) \
.filter(GuildMembers.guild_id == guild_id) \ .filter(GuildMembers.guild_id == int(guild_id)) \
.filter(GuildMembers.user_id == usr.id) \ .filter(GuildMembers.user_id == int(usr.id)) \
.filter(GuildMembers.active == False).first() .filter(GuildMembers.active == False).first()
changed = True changed = True
if dbusr: if dbusr:
dbusr.banned = True dbusr.banned = True
else: else:
dbusr = GuildMembers( dbusr = GuildMembers(
guild_id, int(guild_id),
usr.id, int(usr.id),
usr.name, usr.name,
usr.discriminator, usr.discriminator,
None, None,
@ -248,26 +248,26 @@ class DatabaseInterface(object):
dbuser = None dbuser = None
if discriminator: if discriminator:
dbuser = session.query(UnauthenticatedUsers) \ dbuser = session.query(UnauthenticatedUsers) \
.filter(UnauthenticatedUsers.guild_id == guild_id) \ .filter(UnauthenticatedUsers.guild_id == int(guild_id)) \
.filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \ .filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \
.filter(UnauthenticatedUsers.discriminator == discriminator) \ .filter(UnauthenticatedUsers.discriminator == discriminator) \
.order_by(UnauthenticatedUsers.id.desc()).first() .order_by(UnauthenticatedUsers.id.desc()).first()
else: else:
dbuser = session.query(UnauthenticatedUsers) \ dbuser = session.query(UnauthenticatedUsers) \
.filter(UnauthenticatedUsers.guild_id == guild_id) \ .filter(UnauthenticatedUsers.guild_id == int(guild_id)) \
.filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \ .filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \
.order_by(UnauthenticatedUsers.id.desc()).first() .order_by(UnauthenticatedUsers.id.desc()).first()
if not dbuser: if not dbuser:
return "Ban error! Guest user cannot be found." return "Ban error! Guest user cannot be found."
dbban = session.query(UnauthenticatedBans) \ dbban = session.query(UnauthenticatedBans) \
.filter(UnauthenticatedBans.guild_id == guild_id) \ .filter(UnauthenticatedBans.guild_id == int(guild_id)) \
.filter(UnauthenticatedBans.last_username == dbuser.username) \ .filter(UnauthenticatedBans.last_username == dbuser.username) \
.filter(UnauthenticatedBans.last_discriminator == dbuser.discriminator).first() .filter(UnauthenticatedBans.last_discriminator == dbuser.discriminator).first()
if dbban is not None: if dbban is not None:
if dbban.lifter_id is None: if dbban.lifter_id is None:
return "Ban error! Guest user, **{}#{}**, has already been banned.".format(dbban.last_username, dbban.last_discriminator) return "Ban error! Guest user, **{}#{}**, has already been banned.".format(dbban.last_username, dbban.last_discriminator)
session.delete(dbban) session.delete(dbban)
dbban = UnauthenticatedBans(guild_id, dbuser.ip_address, dbuser.username, dbuser.discriminator, "", placer_id) dbban = UnauthenticatedBans(int(guild_id), dbuser.ip_address, dbuser.username, dbuser.discriminator, "", int(placer_id))
session.add(dbban) session.add(dbban)
session.commit() session.commit()
return "Guest user, **{}#{}**, has successfully been added to the ban list!".format(dbban.last_username, dbban.last_discriminator) return "Guest user, **{}#{}**, has successfully been added to the ban list!".format(dbban.last_username, dbban.last_discriminator)
@ -278,13 +278,13 @@ class DatabaseInterface(object):
dbuser = None dbuser = None
if discriminator: if discriminator:
dbuser = session.query(UnauthenticatedUsers) \ dbuser = session.query(UnauthenticatedUsers) \
.filter(UnauthenticatedUsers.guild_id == guild_id) \ .filter(UnauthenticatedUsers.guild_id == int(guild_id)) \
.filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \ .filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \
.filter(UnauthenticatedUsers.discriminator == discriminator) \ .filter(UnauthenticatedUsers.discriminator == discriminator) \
.order_by(UnauthenticatedUsers.id.desc()).first() .order_by(UnauthenticatedUsers.id.desc()).first()
else: else:
dbuser = session.query(UnauthenticatedUsers) \ dbuser = session.query(UnauthenticatedUsers) \
.filter(UnauthenticatedUsers.guild_id == guild_id) \ .filter(UnauthenticatedUsers.guild_id == int(guild_id)) \
.filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \ .filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \
.order_by(UnauthenticatedUsers.id.desc()).first() .order_by(UnauthenticatedUsers.id.desc()).first()
if not dbuser: if not dbuser:
@ -298,5 +298,5 @@ class DatabaseInterface(object):
async def delete_all_messages_from_channel(self, channel_id): async def delete_all_messages_from_channel(self, channel_id):
async with threadpool(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
session.query(Messages).filter(Messages.channel_id == channel_id).delete() session.query(Messages).filter(Messages.channel_id == int(channel_id)).delete()
session.commit() session.commit()

View File

@ -3,8 +3,8 @@ from titanembeds.database import db, Base
class GuildMembers(Base): class GuildMembers(Base):
__tablename__ = "guild_members" __tablename__ = "guild_members"
id = db.Column(db.Integer, primary_key=True) # Auto incremented id id = db.Column(db.Integer, primary_key=True) # Auto incremented id
guild_id = db.Column(db.String(255)) # Discord guild id guild_id = db.Column(db.BigInteger) # Discord guild id
user_id = db.Column(db.String(255)) # Discord user id user_id = db.Column(db.BigInteger) # Discord user id
username = db.Column(db.String(255)) # Name username = db.Column(db.String(255)) # Name
discriminator = db.Column(db.Integer) # User discriminator discriminator = db.Column(db.Integer) # User discriminator
nickname = db.Column(db.String(255)) # User nickname nickname = db.Column(db.String(255)) # User nickname

View File

@ -2,8 +2,7 @@ from titanembeds.database import db, Base
class Guilds(Base): class Guilds(Base):
__tablename__ = "guilds" __tablename__ = "guilds"
id = db.Column(db.Integer, primary_key=True) # Auto incremented id guild_id = db.Column(db.BigInteger, primary_key=True # Discord guild id
guild_id = db.Column(db.String(255)) # Discord guild id
name = db.Column(db.String(255)) # Name name = db.Column(db.String(255)) # Name
unauth_users = db.Column(db.Boolean()) # If allowed unauth users unauth_users = db.Column(db.Boolean()) # If allowed unauth users
visitor_view = db.Column(db.Boolean()) # If users are automatically "signed in" and can view chat visitor_view = db.Column(db.Boolean()) # If users are automatically "signed in" and can view chat
@ -17,7 +16,7 @@ class Guilds(Base):
channels = db.Column(db.Text().with_variant(db.Text(length=4294967295), 'mysql'))# Guild channels channels = db.Column(db.Text().with_variant(db.Text(length=4294967295), 'mysql'))# Guild channels
webhooks = db.Column(db.Text().with_variant(db.Text(length=4294967295), 'mysql'))# Guild webhooks webhooks = db.Column(db.Text().with_variant(db.Text(length=4294967295), 'mysql'))# Guild webhooks
emojis = db.Column(db.Text().with_variant(db.Text(length=4294967295), 'mysql')) # Guild Emojis emojis = db.Column(db.Text().with_variant(db.Text(length=4294967295), 'mysql')) # Guild Emojis
owner_id = db.Column(db.String(255)) # Snowflake of the owner owner_id = db.Column(db.BigInteger # Snowflake of the owner
icon = db.Column(db.String(255)) # The icon string, null if none icon = db.Column(db.String(255)) # The icon string, null if none
invite_link = db.Column(db.String(255)) # Custom Discord Invite Link invite_link = db.Column(db.String(255)) # Custom Discord Invite Link

View File

@ -2,10 +2,9 @@ from titanembeds.database import db, Base
class Messages(Base): class Messages(Base):
__tablename__ = "messages" __tablename__ = "messages"
id = db.Column(db.Integer, primary_key=True) # Auto incremented id message_id = db.Column(db.BigInteger, primary_key=True) # Message snowflake
guild_id = db.Column(db.String(255)) # Discord guild id guild_id = db.Column(db.BigInteger) # Discord guild id
channel_id = db.Column(db.String(255)) # Channel id channel_id = db.Column(db.BigInteger) # Channel id
message_id = db.Column(db.String(255)) # Message snowflake
content = db.Column(db.Text()) # Message contents content = db.Column(db.Text()) # Message contents
author = db.Column(db.Text()) # Author json author = db.Column(db.Text()) # Author json
timestamp = db.Column(db.TIMESTAMP) # Timestamp of when content is created timestamp = db.Column(db.TIMESTAMP) # Timestamp of when content is created

View File

@ -11,8 +11,8 @@ class UnauthenticatedBans(Base):
last_discriminator = db.Column(db.Integer) # The discrim when they got banned last_discriminator = db.Column(db.Integer) # The discrim when they got banned
timestamp = db.Column(db.TIMESTAMP) # The timestamp of when the user got banned timestamp = db.Column(db.TIMESTAMP) # The timestamp of when the user got banned
reason = db.Column(db.Text()) # The reason of the ban set by the guild moderators reason = db.Column(db.Text()) # The reason of the ban set by the guild moderators
lifter_id = db.Column(db.String(255)) # Discord Client ID of the user who lifted the ban lifter_id = db.Column(db.BigInteger) # Discord Client ID of the user who lifted the ban
placer_id = db.Column(db.String(255)) # The id of who placed the ban placer_id = db.Column(db.BigInteger) # The id of who placed the ban
def __init__(self, guild_id, ip_address, last_username, last_discriminator, reason, placer_id): def __init__(self, guild_id, ip_address, last_username, last_discriminator, reason, placer_id):
self.guild_id = guild_id self.guild_id = guild_id

View File

@ -7,7 +7,7 @@ import string
class UnauthenticatedUsers(Base): class UnauthenticatedUsers(Base):
__tablename__ = "unauthenticated_users" __tablename__ = "unauthenticated_users"
id = db.Column(db.Integer, primary_key=True) # Auto increment id id = db.Column(db.Integer, primary_key=True) # Auto increment id
guild_id = db.Column(db.String(255)) # Guild pretaining to the unauthenticated user guild_id = db.Column(db.BigInteger) # Guild pretaining to the unauthenticated user
username = db.Column(db.String(255)) # The username of the user username = db.Column(db.String(255)) # The username of the user
discriminator = db.Column(db.Integer) # The discriminator to distinguish unauth users with each other discriminator = db.Column(db.Integer) # The discriminator to distinguish unauth users with each other
user_key = db.Column(db.Text()) # The secret key used to identify the user holder user_key = db.Column(db.Text()) # The secret key used to identify the user holder

View File

@ -0,0 +1,171 @@
"""Updated Primary keys away from the auto increment and use bigints for ids
Revision ID: dffebf852b41
Revises: 5642232899cf
Create Date: 2018-01-23 19:32:40.846345
"""
# revision identifiers, used by Alembic.
revision = 'dffebf852b41'
down_revision = '5642232899cf'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.execute("ALTER TABLE administrators ALTER COLUMN user_id TYPE BIGINT USING user_id::bigint")
op.drop_column('administrators', 'id')
op.execute("ALTER TABLE administrators ADD PRIMARY KEY (user_id);")
op.execute("ALTER TABLE authenticated_users ALTER COLUMN client_id DROP DEFAULT")
op.execute("ALTER TABLE authenticated_users ALTER COLUMN client_id TYPE BIGINT USING client_id::bigint")
op.execute("ALTER TABLE authenticated_users ALTER COLUMN guild_id DROP DEFAULT")
op.execute("ALTER TABLE authenticated_users ALTER COLUMN guild_id TYPE BIGINT USING guild_id::bigint")
op.execute("ALTER TABLE cosmetics ALTER COLUMN user_id TYPE BIGINT USING user_id::bigint")
op.drop_column('cosmetics', 'id')
op.execute("ALTER TABLE cosmetics ADD PRIMARY KEY (user_id);")
op.execute("ALTER TABLE disabled_guilds ALTER COLUMN guild_id TYPE BIGINT USING guild_id::bigint")
op.drop_column('disabled_guilds', 'id')
op.execute("ALTER TABLE disabled_guilds ADD PRIMARY KEY (guild_id);")
op.execute("ALTER TABLE guild_members ALTER COLUMN guild_id DROP DEFAULT")
op.execute("ALTER TABLE guild_members ALTER COLUMN guild_id TYPE BIGINT USING guild_id::bigint")
op.execute("ALTER TABLE guild_members ALTER COLUMN user_id DROP DEFAULT")
op.execute("ALTER TABLE guild_members ALTER COLUMN user_id TYPE BIGINT USING user_id::bigint")
op.execute("ALTER TABLE guilds ALTER COLUMN guild_id DROP DEFAULT")
op.execute("ALTER TABLE guilds ALTER COLUMN guild_id TYPE BIGINT USING guild_id::bigint")
op.execute("ALTER TABLE guilds ALTER COLUMN owner_id DROP DEFAULT")
op.execute("ALTER TABLE guilds ALTER COLUMN owner_id TYPE BIGINT USING owner_id::bigint")
op.drop_column('guilds', 'id')
op.execute("ALTER TABLE guilds ADD PRIMARY KEY (guild_id);")
op.execute("ALTER TABLE messages ALTER COLUMN channel_id DROP DEFAULT")
op.execute("ALTER TABLE messages ALTER COLUMN channel_id TYPE BIGINT USING channel_id::bigint")
op.execute("ALTER TABLE messages ALTER COLUMN guild_id DROP DEFAULT")
op.execute("ALTER TABLE messages ALTER COLUMN guild_id TYPE BIGINT USING guild_id::bigint")
op.execute("ALTER TABLE messages ALTER COLUMN message_id DROP DEFAULT")
op.execute("ALTER TABLE messages ALTER COLUMN message_id TYPE BIGINT USING message_id::bigint")
op.drop_column('messages', 'id')
op.execute("ALTER TABLE messages ADD PRIMARY KEY (message_id);")
op.execute("ALTER TABLE patreon ALTER COLUMN user_id TYPE BIGINT USING user_id::bigint")
op.drop_column('patreon', 'id')
op.execute("ALTER TABLE patreon ADD PRIMARY KEY (user_id);")
op.execute("ALTER TABLE titan_tokens ALTER COLUMN user_id TYPE BIGINT USING user_id::bigint")
op.drop_column('titan_tokens', 'id')
op.execute("ALTER TABLE titan_tokens ADD PRIMARY KEY (user_id);")
op.execute("ALTER TABLE token_transactions ALTER COLUMN user_id TYPE BIGINT USING user_id::bigint")
op.execute("ALTER TABLE unauthenticated_bans ALTER COLUMN guild_id DROP DEFAULT")
op.execute("ALTER TABLE unauthenticated_bans ALTER COLUMN guild_id TYPE BIGINT USING guild_id::bigint")
op.execute("ALTER TABLE unauthenticated_bans ALTER COLUMN lifter_id TYPE BIGINT USING lifter_id::bigint")
op.execute("ALTER TABLE unauthenticated_bans ALTER COLUMN placer_id DROP DEFAULT")
op.execute("ALTER TABLE unauthenticated_bans ALTER COLUMN placer_id TYPE BIGINT USING placer_id::bigint")
op.execute("ALTER TABLE unauthenticated_users ALTER COLUMN guild_id DROP DEFAULT")
op.execute("ALTER TABLE unauthenticated_users ALTER COLUMN guild_id TYPE BIGINT USING guild_id::bigint")
op.execute("ALTER TABLE user_css ALTER COLUMN user_id TYPE BIGINT USING user_id::bigint")
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('user_css', 'user_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False)
op.alter_column('unauthenticated_users', 'guild_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
existing_server_default=sa.text("''::character varying"))
op.alter_column('unauthenticated_bans', 'placer_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
existing_server_default=sa.text("''::character varying"))
op.alter_column('unauthenticated_bans', 'lifter_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=True)
op.alter_column('unauthenticated_bans', 'guild_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
existing_server_default=sa.text("''::character varying"))
op.alter_column('token_transactions', 'user_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False)
op.add_column('titan_tokens', sa.Column('id', sa.INTEGER(), nullable=False))
op.alter_column('titan_tokens', 'user_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
autoincrement=False)
op.add_column('patreon', sa.Column('id', sa.INTEGER(), nullable=False))
op.alter_column('patreon', 'user_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
autoincrement=False)
op.add_column('messages', sa.Column('id', sa.INTEGER(), nullable=False))
op.alter_column('messages', 'message_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
autoincrement=False,
existing_server_default=sa.text("''::character varying"))
op.alter_column('messages', 'guild_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
existing_server_default=sa.text("''::character varying"))
op.alter_column('messages', 'channel_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
existing_server_default=sa.text("''::character varying"))
op.add_column('guilds', sa.Column('id', sa.INTEGER(), nullable=False))
op.alter_column('guilds', 'owner_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
existing_server_default=sa.text("''::character varying"))
op.alter_column('guilds', 'guild_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
autoincrement=False,
existing_server_default=sa.text("''::character varying"))
op.alter_column('guild_members', 'user_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
existing_server_default=sa.text("''::character varying"))
op.alter_column('guild_members', 'guild_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
existing_server_default=sa.text("''::character varying"))
op.add_column('disabled_guilds', sa.Column('id', sa.INTEGER(), nullable=False))
op.alter_column('disabled_guilds', 'guild_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
autoincrement=False)
op.add_column('cosmetics', sa.Column('id', sa.INTEGER(), nullable=False))
op.alter_column('cosmetics', 'user_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
autoincrement=False)
op.alter_column('authenticated_users', 'guild_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
existing_server_default=sa.text("''::character varying"))
op.alter_column('authenticated_users', 'client_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
existing_server_default=sa.text("''::character varying"))
op.add_column('administrators', sa.Column('id', sa.INTEGER(), nullable=False))
op.alter_column('administrators', 'user_id',
existing_type=sa.BigInteger(),
type_=sa.VARCHAR(length=255),
autoincrement=False)
# ### end Alembic commands ###

View File

@ -448,7 +448,7 @@ def user_info(guild_id, user_id):
} }
member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == user_id).first() member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == user_id).first()
if member: if member:
usr["id"] = member.user_id usr["id"] = str(member.user_id)
usr["username"] = member.username usr["username"] = member.username
usr["nickname"] = member.nickname usr["nickname"] = member.nickname
usr["discriminator"] = member.discriminator usr["discriminator"] = member.discriminator

View File

@ -32,14 +32,14 @@ class Gateway(Namespace):
if session.get("unauthenticated", True) and guild_id in session.get("user_keys", {}): if session.get("unauthenticated", True) and guild_id in session.get("user_keys", {}):
join_room("IP_"+get_client_ipaddr()) join_room("IP_"+get_client_ipaddr())
elif not session.get("unauthenticated", True): elif not session.get("unauthenticated", True):
join_room("USER_"+session["user_id"]) join_room("USER_"+str(session["user_id"]))
visitor_mode = data["visitor_mode"] visitor_mode = data["visitor_mode"]
if not visitor_mode: if not visitor_mode:
if session["unauthenticated"]: if session["unauthenticated"]:
emit("embed_user_connect", {"unauthenticated": True, "username": session["username"], "discriminator": session["user_id"]}, room="GUILD_"+guild_id) emit("embed_user_connect", {"unauthenticated": True, "username": session["username"], "discriminator": session["user_id"]}, room="GUILD_"+guild_id)
else: else:
nickname = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == session["user_id"]).first().nickname nickname = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == session["user_id"]).first().nickname
emit("embed_user_connect", {"unauthenticated": False, "id": session["user_id"], "nickname": nickname, "username": session["username"],"discriminator": session["discriminator"], "avatar_url": session["avatar"]}, room="GUILD_"+guild_id) emit("embed_user_connect", {"unauthenticated": False, "id": str(session["user_id"]), "nickname": nickname, "username": session["username"],"discriminator": session["discriminator"], "avatar_url": session["avatar"]}, room="GUILD_"+guild_id)
emit("identified") emit("identified")
self.teardown_db_session() self.teardown_db_session()
@ -51,7 +51,7 @@ class Gateway(Namespace):
if session["unauthenticated"]: if session["unauthenticated"]:
msg = {"unauthenticated": True, "username": session["username"], "discriminator": session["user_id"]} msg = {"unauthenticated": True, "username": session["username"], "discriminator": session["user_id"]}
else: else:
msg = {"unauthenticated": False, "id": session["user_id"]} msg = {"unauthenticated": False, "id": str(session["user_id"])}
emit("embed_user_disconnect", msg, room="GUILD_"+guild_id) emit("embed_user_disconnect", msg, room="GUILD_"+guild_id)
if guild_webhooks_enabled(guild_id): # Delete webhooks if guild_webhooks_enabled(guild_id): # Delete webhooks
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
@ -111,7 +111,7 @@ class Gateway(Namespace):
'username': dbMember.username, 'username': dbMember.username,
'nickname': dbMember.nickname, 'nickname': dbMember.nickname,
'discriminator': dbMember.discriminator, 'discriminator': dbMember.discriminator,
'user_id': session['user_id'], 'user_id': str(session['user_id']),
} }
emit("current_user_info", usr) emit("current_user_info", usr)
self.teardown_db_session() self.teardown_db_session()
@ -157,7 +157,7 @@ class Gateway(Namespace):
} }
member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.username == name, GuildMembers.discriminator == discriminator).first() member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.username == name, GuildMembers.discriminator == discriminator).first()
if member: if member:
usr["id"] = member.user_id usr["id"] = str(member.user_id)
usr["username"] = member.username usr["username"] = member.username
usr["nickname"] = member.nickname usr["nickname"] = member.nickname
usr["avatar"] = member.avatar usr["avatar"] = member.avatar
@ -169,7 +169,7 @@ class Gateway(Namespace):
else: else:
member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.nickname == name, GuildMembers.discriminator == discriminator).first() member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.nickname == name, GuildMembers.discriminator == discriminator).first()
if member: if member:
usr["id"] = member.user_id usr["id"] = str(member.user_id)
usr["username"] = member.username usr["username"] = member.username
usr["nickname"] = member.nickname usr["nickname"] = member.nickname
usr["avatar"] = member.avatar usr["avatar"] = member.avatar

View File

@ -41,7 +41,7 @@ def callback():
session['unauthenticated'] = False session['unauthenticated'] = False
session.permanent = True session.permanent = True
user = get_current_authenticated_user() user = get_current_authenticated_user()
session['user_id'] = user['id'] session['user_id'] = int(user['id'])
session['username'] = user['username'] session['username'] = user['username']
session['discriminator'] = user['discriminator'] session['discriminator'] = user['discriminator']
session['avatar'] = generate_avatar_url(user['id'], user['avatar'], user['discriminator']) session['avatar'] = generate_avatar_url(user['id'], user['avatar'], user['discriminator'])

View File

@ -2,8 +2,7 @@ from titanembeds.database import db
class Administrators(db.Model): class Administrators(db.Model):
__tablename__ = "administrators" __tablename__ = "administrators"
id = db.Column(db.Integer, primary_key=True) # Auto increment id user_id = db.Column(db.BigInteger, nullable=False, primary_key=True) # Discord user id of user of an administrator
user_id = db.Column(db.String(255), nullable=False) # Discord user id of user of an administrator
def get_administrators_list(): def get_administrators_list():
q = db.session.query(Administrators).all() q = db.session.query(Administrators).all()

View File

@ -5,8 +5,8 @@ import time
class AuthenticatedUsers(db.Model): class AuthenticatedUsers(db.Model):
__tablename__ = "authenticated_users" __tablename__ = "authenticated_users"
id = db.Column(db.Integer, primary_key=True) # Auto increment id id = db.Column(db.Integer, primary_key=True) # Auto increment id
guild_id = db.Column(db.String(255), nullable=False) # Guild pretaining to the authenticated user guild_id = db.Column(db.BigInteger, nullable=False) # Guild pretaining to the authenticated user
client_id = db.Column(db.String(255), nullable=False) # Client ID of the authenticated user client_id = db.Column(db.BigInteger, nullable=False) # Client ID of the authenticated user
def __init__(self, guild_id, client_id): def __init__(self, guild_id, client_id):
self.guild_id = guild_id self.guild_id = guild_id

View File

@ -3,8 +3,7 @@ import json
class Cosmetics(db.Model): class Cosmetics(db.Model):
__tablename__ = "cosmetics" __tablename__ = "cosmetics"
id = db.Column(db.Integer, primary_key=True) # Auto increment id user_id = db.Column(db.BigInteger, nullable=False, primary_key=True) # Discord user id of user of cosmetics
user_id = db.Column(db.String(255), nullable=False) # Discord user id of user of cosmetics
css = db.Column(db.Boolean(), nullable=False) # If they can create/edit custom CSS 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 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 guest_icon = db.Column(db.Boolean(), nullable=False, server_default=db.false()) # If they can set the guest icon for all guilds

View File

@ -1,9 +1,8 @@
from titanembeds.database import db from titanembeds.database import db
class DisabledGuilds(db.Model): class DisabledGuilds(db.Model):
__tablename__ = "disabled_guilds" __tablename__ = "disabled_guilds" # Auto increment id
id = db.Column(db.Integer, primary_key=True) # Auto increment id guild_id = db.Column(db.BigInteger, nullable=False, primary_key=True) # Server id that is disabled
guild_id = db.Column(db.String(255), nullable=False) # Server id that is disabled
def __init__(self, guild_id): def __init__(self, guild_id):
self.guild_id = guild_id self.guild_id = guild_id

View File

@ -4,8 +4,8 @@ import json
class GuildMembers(db.Model): class GuildMembers(db.Model):
__tablename__ = "guild_members" __tablename__ = "guild_members"
id = db.Column(db.Integer, primary_key=True) # Auto incremented id id = db.Column(db.Integer, primary_key=True) # Auto incremented id
guild_id = db.Column(db.String(255), nullable=False) # Discord guild id guild_id = db.Column(db.BigInteger, nullable=False) # Discord guild id
user_id = db.Column(db.String(255), nullable=False) # Discord user id user_id = db.Column(db.BigInteger, nullable=False) # Discord user id
username = db.Column(db.String(255), nullable=False) # Name username = db.Column(db.String(255), nullable=False) # Name
discriminator = db.Column(db.Integer, nullable=False) # User discriminator discriminator = db.Column(db.Integer, nullable=False) # User discriminator
nickname = db.Column(db.String(255)) # User nickname nickname = db.Column(db.String(255)) # User nickname

View File

@ -2,8 +2,7 @@ from titanembeds.database import db
class Guilds(db.Model): class Guilds(db.Model):
__tablename__ = "guilds" __tablename__ = "guilds"
id = db.Column(db.Integer, primary_key=True) # Auto incremented id guild_id = db.Column(db.BigInteger, nullable=False, primary_key=True) # Discord guild id
guild_id = db.Column(db.String(255), nullable=False) # Discord guild id
name = db.Column(db.String(255), nullable=False) # Name name = db.Column(db.String(255), nullable=False) # Name
unauth_users = db.Column(db.Boolean(), nullable=False, default=1) # If allowed unauth users unauth_users = db.Column(db.Boolean(), nullable=False, default=1) # If allowed unauth users
visitor_view = db.Column(db.Boolean(), nullable=False, default=0) # If users are automatically "signed in" and can view chat visitor_view = db.Column(db.Boolean(), nullable=False, default=0) # If users are automatically "signed in" and can view chat
@ -17,7 +16,7 @@ class Guilds(db.Model):
channels = db.Column(db.Text().with_variant(db.Text(4294967295), 'mysql'), nullable=False) # Guild channels channels = db.Column(db.Text().with_variant(db.Text(4294967295), 'mysql'), nullable=False) # Guild channels
webhooks = db.Column(db.Text().with_variant(db.Text(4294967295), 'mysql'), nullable=False) # Guild webhooks webhooks = db.Column(db.Text().with_variant(db.Text(4294967295), 'mysql'), nullable=False) # Guild webhooks
emojis = db.Column(db.Text().with_variant(db.Text(4294967295), 'mysql'), nullable=False) # Guild Emojis emojis = db.Column(db.Text().with_variant(db.Text(4294967295), 'mysql'), nullable=False) # Guild Emojis
owner_id = db.Column(db.String(255), nullable=False) # Snowflake of the owner owner_id = db.Column(db.BigInteger, nullable=False) # Snowflake of the owner
icon = db.Column(db.String(255)) # The icon string, null if none icon = db.Column(db.String(255)) # The icon string, null if none
invite_link = db.Column(db.String(255)) # Custom Discord Invite Link invite_link = db.Column(db.String(255)) # Custom Discord Invite Link

View File

@ -4,10 +4,9 @@ import json
class Messages(db.Model): class Messages(db.Model):
__tablename__ = "messages" __tablename__ = "messages"
id = db.Column(db.Integer, primary_key=True) # Auto incremented id message_id = db.Column(db.BigInteger, nullable=False, primary_key=True) # Message snowflake
guild_id = db.Column(db.String(255), nullable=False) # Discord guild id guild_id = db.Column(db.BigInteger, nullable=False) # Discord guild id
channel_id = db.Column(db.String(255), nullable=False) # Channel id channel_id = db.Column(db.BigInteger, nullable=False) # Channel id
message_id = db.Column(db.String(255), nullable=False) # Message snowflake
content = db.Column(db.Text(), nullable=False) # Message contents content = db.Column(db.Text(), nullable=False) # Message contents
author = db.Column(db.Text(), nullable=False) # Author author = db.Column(db.Text(), nullable=False) # Author
timestamp = db.Column(db.TIMESTAMP, nullable=False) # Timestamp of when content is created timestamp = db.Column(db.TIMESTAMP, nullable=False) # Timestamp of when content is created
@ -35,7 +34,7 @@ def get_channel_messages(guild_id, channel_id, after_snowflake=None):
if not after_snowflake: if not after_snowflake:
q = db.session.query(Messages).filter(Messages.channel_id == channel_id).order_by(Messages.timestamp.desc()).limit(50) q = db.session.query(Messages).filter(Messages.channel_id == channel_id).order_by(Messages.timestamp.desc()).limit(50)
else: else:
q = db.session.query(Messages).filter(cast(Messages.channel_id, db.BigInteger) == int(channel_id)).filter(cast(Messages.message_id, db.BigInteger) > after_snowflake).order_by(Messages.timestamp.desc()).limit(50) q = db.session.query(Messages).filter(Messages.channel_id == channel_id).filter(Messages.message_id > after_snowflake).order_by(Messages.timestamp.desc()).limit(50)
msgs = [] msgs = []
snowflakes = [] snowflakes = []
for x in q: for x in q:
@ -48,11 +47,11 @@ def get_channel_messages(guild_id, channel_id, after_snowflake=None):
message = { message = {
"attachments": json.loads(x.attachments), "attachments": json.loads(x.attachments),
"timestamp": x.timestamp, "timestamp": x.timestamp,
"id": x.message_id, "id": str(x.message_id),
"edited_timestamp": x.edited_timestamp, "edited_timestamp": x.edited_timestamp,
"author": json.loads(x.author), "author": json.loads(x.author),
"content": x.content, "content": x.content,
"channel_id": x.channel_id, "channel_id": str(x.channel_id),
"mentions": json.loads(x.mentions), "mentions": json.loads(x.mentions),
"embeds": json.loads(embeds), "embeds": json.loads(embeds),
} }

View File

@ -2,8 +2,7 @@ from titanembeds.database import db
class Patreon(db.Model): class Patreon(db.Model):
__tablename__ = "patreon" __tablename__ = "patreon"
id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.BigInteger, nullable=False, primary_key=True) # User ID from patreon
user_id = db.Column(db.String(255), nullable=False) # User ID from patreon
total_synced = db.Column(db.Integer, nullable=False) # Total cents synced on our end total_synced = db.Column(db.Integer, nullable=False) # Total cents synced on our end
def __init__(self, user_id, total_synced=0): def __init__(self, user_id, total_synced=0):

View File

@ -2,8 +2,7 @@ from titanembeds.database import db
class TitanTokens(db.Model): class TitanTokens(db.Model):
__tablename__ = "titan_tokens" __tablename__ = "titan_tokens"
id = db.Column(db.Integer, primary_key=True) # Auto increment id user_id = db.Column(db.BigInteger, nullable=False, primary_key=True) # Discord user id of user
user_id = db.Column(db.String(255), nullable=False) # Discord user id of user
tokens = db.Column(db.Integer, nullable=False, default=0) # Token amount tokens = db.Column(db.Integer, nullable=False, default=0) # Token amount
def __init__(self, user_id, tokens): def __init__(self, user_id, tokens):

View File

@ -5,7 +5,7 @@ import time
class TokenTransactions(db.Model): class TokenTransactions(db.Model):
__tablename__ = "token_transactions" __tablename__ = "token_transactions"
id = db.Column(db.Integer, primary_key=True) # Auto increment id id = db.Column(db.Integer, primary_key=True) # Auto increment id
user_id = db.Column(db.String(255), nullable=False) # Discord user id of user user_id = db.Column(db.BigInteger, nullable=False) # Discord user id of user
timestamp = db.Column(db.TIMESTAMP, nullable=False) # The timestamp of when the action took place timestamp = db.Column(db.TIMESTAMP, nullable=False) # The timestamp of when the action took place
action = db.Column(db.String(255), nullable=False) # Very short description of the action action = db.Column(db.String(255), nullable=False) # Very short description of the action
net_tokens = db.Column(db.Integer, nullable=False) # Net change of the token amount net_tokens = db.Column(db.Integer, nullable=False) # Net change of the token amount

View File

@ -5,14 +5,14 @@ import time
class UnauthenticatedBans(db.Model): class UnauthenticatedBans(db.Model):
__tablename__ = "unauthenticated_bans" __tablename__ = "unauthenticated_bans"
id = db.Column(db.Integer, primary_key=True) # Auto increment id id = db.Column(db.Integer, primary_key=True) # Auto increment id
guild_id = db.Column(db.String(255), nullable=False) # Guild pretaining to the unauthenticated user guild_id = db.Column(db.BigInteger, nullable=False) # Guild pretaining to the unauthenticated user
ip_address = db.Column(db.String(255), nullable=False) # The IP Address of the user ip_address = db.Column(db.String(255), nullable=False) # The IP Address of the user
last_username = db.Column(db.String(255), nullable=False) # The username when they got banned last_username = db.Column(db.String(255), nullable=False) # The username when they got banned
last_discriminator = db.Column(db.Integer, nullable=False) # The discrim when they got banned last_discriminator = db.Column(db.Integer, nullable=False) # The discrim when they got banned
timestamp = db.Column(db.TIMESTAMP, nullable=False) # The timestamp of when the user got banned timestamp = db.Column(db.TIMESTAMP, nullable=False) # The timestamp of when the user got banned
reason = db.Column(db.Text()) # The reason of the ban set by the guild moderators reason = db.Column(db.Text()) # The reason of the ban set by the guild moderators
lifter_id = db.Column(db.String(255)) # Discord Client ID of the user who lifted the ban lifter_id = db.Column(db.BigInteger) # Discord Client ID of the user who lifted the ban
placer_id = db.Column(db.String(255), nullable=False) # The id of who placed the ban placer_id = db.Column(db.BigInteger, nullable=False) # The id of who placed the ban
def __init__(self, guild_id, ip_address, last_username, last_discriminator, reason, placer_id): def __init__(self, guild_id, ip_address, last_username, last_discriminator, reason, placer_id):
self.guild_id = guild_id self.guild_id = guild_id

View File

@ -7,7 +7,7 @@ import string
class UnauthenticatedUsers(db.Model): class UnauthenticatedUsers(db.Model):
__tablename__ = "unauthenticated_users" __tablename__ = "unauthenticated_users"
id = db.Column(db.Integer, primary_key=True, nullable=False) # Auto increment id id = db.Column(db.Integer, primary_key=True, nullable=False) # Auto increment id
guild_id = db.Column(db.String(255), nullable=False) # Guild pretaining to the unauthenticated user guild_id = db.Column(db.BigInteger, nullable=False) # Guild pretaining to the unauthenticated user
username = db.Column(db.String(255), nullable=False) # The username of the user username = db.Column(db.String(255), nullable=False) # The username of the user
discriminator = db.Column(db.Integer, nullable=False) # The discriminator to distinguish unauth users with each other discriminator = db.Column(db.Integer, nullable=False) # The discriminator to distinguish unauth users with each other
user_key = db.Column(db.Text(), nullable=False) # The secret key used to identify the user holder user_key = db.Column(db.Text(), nullable=False) # The secret key used to identify the user holder

View File

@ -4,7 +4,7 @@ class UserCSS(db.Model):
__tablename__ = "user_css" __tablename__ = "user_css"
id = db.Column(db.Integer, primary_key=True) # Auto increment id id = db.Column(db.Integer, primary_key=True) # Auto increment id
name = db.Column(db.String(255), nullable=False) # CSS Name name = db.Column(db.String(255), nullable=False) # CSS Name
user_id = db.Column(db.String(255), nullable=False) # Discord client ID of the owner of the css (can edit) user_id = db.Column(db.BigInteger, nullable=False) # Discord client ID of the owner of the css (can edit)
css_var_bool = db.Column(db.Boolean(), nullable=False, server_default="0") # If css variables should be taken into consideration css_var_bool = db.Column(db.Boolean(), nullable=False, server_default="0") # If css variables should be taken into consideration
css_variables = db.Column(db.Text()) # Customizeable CSS Variables css_variables = db.Column(db.Text()) # Customizeable CSS Variables
css = db.Column(db.Text().with_variant(db.Text(4294967295), 'mysql')) # CSS contents css = db.Column(db.Text().with_variant(db.Text(4294967295), 'mysql')) # CSS contents

View File

@ -130,7 +130,7 @@ def update_user_status(guild_id, username, user_key=None):
'nickname': None, 'nickname': None,
'user_key': user_key, 'user_key': user_key,
'guild_id': guild_id, 'guild_id': guild_id,
'user_id': session['user_id'], 'user_id': str(session['user_id']),
'banned': checkUserBanned(guild_id, ip_address), 'banned': checkUserBanned(guild_id, ip_address),
'revoked': checkUserRevoke(guild_id, user_key), 'revoked': checkUserRevoke(guild_id, user_key),
} }
@ -151,7 +151,7 @@ def update_user_status(guild_id, username, user_key=None):
'nickname': None, 'nickname': None,
'discriminator': session['discriminator'], 'discriminator': session['discriminator'],
'guild_id': guild_id, 'guild_id': guild_id,
'user_id': session['user_id'], 'user_id': str(session['user_id']),
'banned': checkUserBanned(guild_id), 'banned': checkUserBanned(guild_id),
'revoked': checkUserRevoke(guild_id) 'revoked': checkUserRevoke(guild_id)
} }