Move columns found in discord api on the guilds table into redis

This commit is contained in:
Jeremy Zhang 2018-07-20 23:10:24 +00:00
parent ee051074a7
commit 3783cf1152
14 changed files with 155 additions and 158 deletions

View File

@ -104,45 +104,45 @@ class Titan(discord.AutoShardedClient):
await self.socketio.on_message_delete(message) await self.socketio.on_message_delete(message)
async def on_guild_join(self, guild): async def on_guild_join(self, guild):
await self.database.update_guild(guild) await self.redisqueue.update_guild(guild)
await self.postStats() await self.postStats()
async def on_guild_remove(self, guild): async def on_guild_remove(self, guild):
await self.database.remove_guild(guild) await self.redisqueue.delete_guild(guild)
await self.postStats() await self.postStats()
async def on_guild_update(self, guildbefore, guildafter): async def on_guild_update(self, guildbefore, guildafter):
await self.database.update_guild(guildafter) await self.redisqueue.update_guild(guildafter)
await self.socketio.on_guild_update(guildafter) await self.socketio.on_guild_update(guildafter)
async def on_guild_role_create(self, role): async def on_guild_role_create(self, role):
if role.name == self.user.name and role.managed: if role.name == self.user.name and role.managed:
await asyncio.sleep(2) await asyncio.sleep(2)
await self.database.update_guild(role.guild) await self.redisqueue.update_guild(role.guild)
await self.socketio.on_guild_role_create(role) await self.socketio.on_guild_role_create(role)
async def on_guild_role_delete(self, role): async def on_guild_role_delete(self, role):
if role.guild.me not in role.guild.members: if role.guild.me not in role.guild.members:
return return
await self.database.update_guild(role.guild) await self.redisqueue.update_guild(role.guild)
await self.socketio.on_guild_role_delete(role) await self.socketio.on_guild_role_delete(role)
async def on_guild_role_update(self, rolebefore, roleafter): async def on_guild_role_update(self, rolebefore, roleafter):
await self.database.update_guild(roleafter.guild) await self.redisqueue.update_guild(roleafter.guild)
await self.socketio.on_guild_role_update(roleafter) await self.socketio.on_guild_role_update(roleafter)
async def on_channel_delete(self, channel): async def on_channel_delete(self, channel):
if channel.guild: if channel.guild:
await self.database.update_guild(channel.guild) await self.redisqueue.update_guild(channel.guild)
await self.socketio.on_channel_delete(channel) await self.socketio.on_channel_delete(channel)
async def on_channel_create(self, channel): async def on_channel_create(self, channel):
if channel.guild: if channel.guild:
await self.database.update_guild(channel.guild) await self.redisqueue.update_guild(channel.guild)
await self.socketio.on_channel_create(channel) await self.socketio.on_channel_create(channel)
async def on_guild_channel_update(self, channelbefore, channelafter): async def on_guild_channel_update(self, channelbefore, channelafter):
await self.database.update_guild(channelafter.guild) await self.redisqueue.update_guild(channelafter.guild)
await self.socketio.on_channel_update(channelafter) await self.socketio.on_channel_update(channelafter)
async def on_member_join(self, member): async def on_member_join(self, member):
@ -164,14 +164,14 @@ class Titan(discord.AutoShardedClient):
async def on_guild_emojis_update(self, guild, before, after): async def on_guild_emojis_update(self, guild, before, after):
if len(after) == 0: if len(after) == 0:
await self.database.update_guild(guild) await self.redisqueue.update_guild(guild)
await self.socketio.on_guild_emojis_update(before) await self.socketio.on_guild_emojis_update(before)
else: else:
await self.database.update_guild(guild) await self.redisqueue.update_guild(guild)
await self.socketio.on_guild_emojis_update(after) await self.socketio.on_guild_emojis_update(after)
async def on_webhooks_update(self, guild, channel): async def on_webhooks_update(self, guild, channel):
await self.database.update_guild(guild) await self.redisqueue.update_guild(guild)
async def on_raw_message_edit(self, payload): async def on_raw_message_edit(self, payload):
message_id = payload.message_id message_id = payload.message_id
@ -190,7 +190,7 @@ class Titan(discord.AutoShardedClient):
async def raw_bulk_message_delete(self, payload): async def raw_bulk_message_delete(self, payload):
message_ids = payload.message_ids message_ids = payload.message_ids
channel_ids = payload.channel_id channel_id = payload.channel_id
await asyncio.sleep(1) await asyncio.sleep(1)
for msgid in message_ids: for msgid in message_ids:
msgid = int(msgid) msgid = int(msgid)

View File

@ -5,7 +5,6 @@ import datetime
db = Gino() db = Gino()
from titanembeds.database.guilds import Guilds
from titanembeds.database.unauthenticated_users import UnauthenticatedUsers from titanembeds.database.unauthenticated_users import UnauthenticatedUsers
from titanembeds.database.unauthenticated_bans import UnauthenticatedBans from titanembeds.database.unauthenticated_bans import UnauthenticatedBans
@ -17,57 +16,6 @@ class DatabaseInterface(object):
async def connect(self, dburi): async def connect(self, dburi):
await db.set_bind(dburi) await db.set_bind(dburi)
async def update_guild(self, guild):
if guild.me.guild_permissions.manage_webhooks:
try:
server_webhooks = await guild.webhooks()
except:
server_webhooks = []
else:
server_webhooks = []
gui = await Guilds.get(guild.id)
if not gui:
await Guilds.create(
guild_id = int(guild.id),
name = guild.name,
unauth_users = True,
visitor_view = False,
webhook_messages = False,
guest_icon = None,
chat_links = True,
bracket_links = True,
unauth_captcha = True,
mentions_limit = -1,
roles = json.dumps(get_roles_list(guild.roles)),
channels = json.dumps(get_channels_list(guild.channels)),
webhooks = json.dumps(get_webhooks_list(server_webhooks)),
emojis = json.dumps(get_emojis_list(guild.emojis)),
owner_id = int(guild.owner_id),
icon = guild.icon
)
else:
await gui.update(
name = guild.name,
roles = json.dumps(get_roles_list(guild.roles)),
channels = json.dumps(get_channels_list(guild.channels)),
webhooks = json.dumps(get_webhooks_list(server_webhooks)),
emojis = json.dumps(get_emojis_list(guild.emojis)),
owner_id = int(guild.owner_id),
icon = guild.icon
).apply()
async def remove_unused_guilds(self, guilds):
dbguilds = await Guilds.query.gino.all()
for guild in dbguilds:
disguild = discord.utils.get(guilds, id=guild.guild_id)
if not disguild:
await Messages.delete.where(Messages.guild_id == int(guild.guild_id)).gino.status()
async def remove_guild(self, guild):
gui = await Guilds.get(int(guild.id))
if gui:
await gui.delete()
async def ban_unauth_user_by_query(self, guild_id, placer_id, username, discriminator): async def ban_unauth_user_by_query(self, guild_id, placer_id, username, discriminator):
dbuser = None dbuser = None

View File

@ -1,26 +0,0 @@
from titanembeds.database import db
class Guilds(db.Model):
__tablename__ = "guilds"
guild_id = db.Column(db.BigInteger, primary_key=True) # Discord guild id
name = db.Column(db.String(255)) # Name
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
webhook_messages = db.Column(db.Boolean()) # Use webhooks to send messages instead of the bot
guest_icon = db.Column(db.String(255), default=None) # Guest icon url, None if unset
chat_links = db.Column(db.Boolean()) # If users can post links
bracket_links = db.Column(db.Boolean()) # If appending brackets to links to prevent embed
unauth_captcha = db.Column(db.Boolean(), nullable=False, server_default="1") # Enforce captcha on guest users
mentions_limit = db.Column(db.Integer) # If there is a limit on the number of mentions in a msg
roles = db.Column(db.Text().with_variant(db.Text(length=4294967295), 'mysql')) # Guild Roles
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
emojis = db.Column(db.Text().with_variant(db.Text(length=4294967295), 'mysql')) # Guild Emojis
owner_id = db.Column(db.BigInteger) # Snowflake of the owner
icon = db.Column(db.String(255)) # The icon string, null if none
invite_link = db.Column(db.String(255)) # Custom Discord Invite Link
post_timeout = db.Column(db.Integer, nullable=False, server_default="5") # Seconds to elapse before another message can be posted from the widget
max_message_length = db.Column(db.Integer, nullable=False, server_default="300") # Chars length the message should be before being rejected by the server
banned_words_enabled = db.Column(db.Boolean(), nullable=False, server_default="0") # If banned words are enforced
banned_words_global_included = db.Column(db.Boolean(), nullable=False, server_default="0") # Add global banned words to the list
banned_words = db.Column(db.Text(), nullable=False, server_default="[]") # JSON list of strings to block from sending

View File

@ -1,4 +1,4 @@
from titanembeds.utils import get_formatted_message, get_formatted_user from titanembeds.utils import get_formatted_message, get_formatted_user, get_formatted_guild
from urllib.parse import urlparse from urllib.parse import urlparse
import asyncio_redis import asyncio_redis
import json import json
@ -70,6 +70,8 @@ class RedisQueue:
members = await self.connection.smembers(key) members = await self.connection.smembers(key)
for member in members: for member in members:
the_member = await member the_member = await member
if not the_member:
continue
parsed = json.loads(the_member) parsed = json.loads(the_member)
if re.match(str(dict_value_pattern), str(parsed[dict_key])): if re.match(str(dict_value_pattern), str(parsed[dict_key])):
unformatted_item = the_member unformatted_item = the_member
@ -94,7 +96,7 @@ class RedisQueue:
messages = [] messages = []
async for message in channel.history(limit=50): async for message in channel.history(limit=50):
formatted = get_formatted_message(message) formatted = get_formatted_message(message)
messages.append(json.dumps(formatted)) messages.append(json.dumps(formatted, separators=(',', ':')))
await self.connection.sadd(key, [""] + messages) await self.connection.sadd(key, [""] + messages)
async def push_message(self, message): async def push_message(self, message):
@ -103,7 +105,7 @@ class RedisQueue:
exists = await self.connection.exists(key) exists = await self.connection.exists(key)
if exists: if exists:
message = get_formatted_message(message) message = get_formatted_message(message)
await self.connection.sadd(key, [json.dumps(message)]) await self.connection.sadd(key, [json.dumps(message, separators=(',', ':'))])
async def delete_message(self, message): async def delete_message(self, message):
if message.guild: if message.guild:
@ -125,7 +127,7 @@ class RedisQueue:
return return
user = get_formatted_user(member) user = get_formatted_user(member)
await self.enforce_expiring_key(key) await self.enforce_expiring_key(key)
await self.connection.set(key, json.dumps(user)) await self.connection.set(key, json.dumps(user, separators=(',', ':')))
async def on_get_guild_member_named(self, key, params): async def on_get_guild_member_named(self, key, params):
guild = self.bot.get_guild(int(params["guild_id"])) guild = self.bot.get_guild(int(params["guild_id"]))
@ -141,7 +143,7 @@ class RedisQueue:
result = "" result = ""
else: else:
result_id = result.id result_id = result.id
result = json.dumps({"user_id": result_id}) result = json.dumps({"user_id": result_id}, separators=(',', ':'))
get_guild_member_key = "Queue/guilds/{}/members/{}".format(guild.id, result_id) get_guild_member_key = "Queue/guilds/{}/members/{}".format(guild.id, result_id)
get_guild_member_param = {"guild_id": guild.id, "user_id": result_id} get_guild_member_param = {"guild_id": guild.id, "user_id": result_id}
await self.on_get_guild_member(get_guild_member_key, get_guild_member_param) await self.on_get_guild_member(get_guild_member_key, get_guild_member_param)
@ -152,7 +154,7 @@ class RedisQueue:
members = guild.members members = guild.members
member_ids = [] member_ids = []
for member in members: for member in members:
member_ids.append(json.dumps({"user_id": member.id})) member_ids.append(json.dumps({"user_id": member.id}, separators=(',', ':')))
get_guild_member_key = "Queue/guilds/{}/members/{}".format(guild.id, member.id) get_guild_member_key = "Queue/guilds/{}/members/{}".format(guild.id, member.id)
get_guild_member_param = {"guild_id": guild.id, "user_id": member.id} get_guild_member_param = {"guild_id": guild.id, "user_id": member.id}
await self.on_get_guild_member(get_guild_member_key, get_guild_member_param) await self.on_get_guild_member(get_guild_member_key, get_guild_member_param)
@ -162,14 +164,14 @@ class RedisQueue:
key = "Queue/guilds/{}/members".format(member.guild.id) key = "Queue/guilds/{}/members".format(member.guild.id)
exists = await self.connection.exists(key) exists = await self.connection.exists(key)
if exists: if exists:
await self.connection.sadd(key, [json.dumps({"user_id": member.id})]) await self.connection.sadd(key, [json.dumps({"user_id": member.id}, separators=(',', ':'))])
async def remove_member(self, member, guild=None): async def remove_member(self, member, guild=None):
if not guild: if not guild:
guild = member.guild guild = member.guild
guild_member_key = "Queue/guilds/{}/members/{}".format(guild.id, member.id) guild_member_key = "Queue/guilds/{}/members/{}".format(guild.id, member.id)
list_members_key = "Queue/guilds/{}/members".format(guild.id) list_members_key = "Queue/guilds/{}/members".format(guild.id)
await self.connection.srem(list_members_key, [json.dumps({"user_id": member.id})]) await self.connection.srem(list_members_key, [json.dumps({"user_id": member.id}, separators=(',', ':'))])
await self.connection.delete([guild_member_key]) await self.connection.delete([guild_member_key])
async def update_member(self, member): async def update_member(self, member):
@ -177,4 +179,32 @@ class RedisQueue:
await self.add_member(member) await self.add_member(member)
async def ban_member(self, guild, user): async def ban_member(self, guild, user):
await self.remove_member(user, guild) await self.remove_member(user, guild)
async def on_get_guild(self, key, params):
guild = self.bot.get_guild(int(params["guild_id"]))
if not guild:
await self.connection.set(key, "")
return
if guild.me.guild_permissions.manage_webhooks:
try:
server_webhooks = await guild.webhooks()
except:
server_webhooks = []
else:
server_webhooks = []
guild_fmtted = get_formatted_guild(guild, server_webhooks)
await self.connection.set(key, json.dumps(guild_fmtted, separators=(',', ':')))
await self.enforce_expiring_key(key)
async def delete_guild(self, guild):
key = "Queue/guilds/{}".format(guild.id)
await self.connection.delete([key])
async def update_guild(self, guild):
key = "Queue/guilds/{}".format(guild.id)
exists = await self.connection.exists(key)
if exists:
await self.delete_guild(guild)
await self.on_get_guild(key, {"guild_id": guild.id})
await self.enforce_expiring_key(key)

View File

@ -100,12 +100,17 @@ def get_formatted_emojis(emojis):
}) })
return emotes return emotes
def get_formatted_guild(guild): def get_formatted_guild(guild, webhooks=[]):
guil = { guil = {
"id": str(guild.id), "id": str(guild.id),
"name": guild.name, "name": guild.name,
"icon": guild.icon, "icon": guild.icon,
"icon_url": guild.icon_url, "icon_url": guild.icon_url,
"owner_id": guild.owner_id,
"roles": get_roles_list(guild.roles),
"channels": get_channels_list(guild.channels),
"webhooks": get_webhooks_list(webhooks),
"emojis": get_emojis_list(guild.emojis)
} }
return guil return guil

View File

@ -0,0 +1,40 @@
"""Removed various guild columns that pretains to discordapi
Revision ID: 87d043d7917e
Revises: 1d2c2dc41e86
Create Date: 2018-07-20 23:04:28.509870
"""
# revision identifiers, used by Alembic.
revision = '87d043d7917e'
down_revision = '1d2c2dc41e86'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('guilds', 'emojis')
op.drop_column('guilds', 'channels')
op.drop_column('guilds', 'roles')
op.drop_column('guilds', 'icon')
op.drop_column('guilds', 'owner_id')
op.drop_column('guilds', 'name')
op.drop_column('guilds', 'webhooks')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('guilds', sa.Column('webhooks', sa.TEXT(), autoincrement=False, nullable=False))
op.add_column('guilds', sa.Column('name', sa.VARCHAR(length=255), autoincrement=False, nullable=False))
op.add_column('guilds', sa.Column('owner_id', sa.BIGINT(), autoincrement=False, nullable=False))
op.add_column('guilds', sa.Column('icon', sa.VARCHAR(length=255), server_default=sa.text("''::character varying"), autoincrement=False, nullable=True))
op.add_column('guilds', sa.Column('roles', sa.TEXT(), autoincrement=False, nullable=False))
op.add_column('guilds', sa.Column('channels', sa.TEXT(), autoincrement=False, nullable=False))
op.add_column('guilds', sa.Column('emojis', sa.TEXT(), autoincrement=False, nullable=False))
# ### end Alembic commands ###

View File

@ -155,10 +155,15 @@ def prepare_guild_members_list(members, bans):
@admin.route("/administrate_guild/<guild_id>", methods=["GET"]) @admin.route("/administrate_guild/<guild_id>", methods=["GET"])
@is_admin @is_admin
def administrate_guild(guild_id): def administrate_guild(guild_id):
guild = redisqueue.get_guild(guild_id)
if not guild:
abort(404)
return
db_guild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() db_guild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
if not db_guild: if not db_guild:
abort(500) db_guild = Guilds(guild["id"])
return db.session.add(db_guild)
db.session.commit()
session["redirect"] = None session["redirect"] = None
cosmetics = db.session.query(Cosmetics).filter(Cosmetics.user_id == session['user_id']).first() cosmetics = db.session.query(Cosmetics).filter(Cosmetics.user_id == session['user_id']).first()
permissions=[] permissions=[]
@ -169,8 +174,8 @@ def administrate_guild(guild_id):
all_bans = db.session.query(UnauthenticatedBans).filter(UnauthenticatedBans.guild_id == guild_id).all() all_bans = db.session.query(UnauthenticatedBans).filter(UnauthenticatedBans.guild_id == guild_id).all()
users = prepare_guild_members_list(all_members, all_bans) users = prepare_guild_members_list(all_members, all_bans)
dbguild_dict = { dbguild_dict = {
"id": db_guild.guild_id, "id": guild["id"],
"name": db_guild.name, "name": guild["name"],
"unauth_users": db_guild.unauth_users, "unauth_users": db_guild.unauth_users,
"visitor_view": db_guild.visitor_view, "visitor_view": db_guild.visitor_view,
"webhook_messages": db_guild.webhook_messages, "webhook_messages": db_guild.webhook_messages,
@ -178,7 +183,7 @@ def administrate_guild(guild_id):
"bracket_links": db_guild.bracket_links, "bracket_links": db_guild.bracket_links,
"mentions_limit": db_guild.mentions_limit, "mentions_limit": db_guild.mentions_limit,
"unauth_captcha": db_guild.unauth_captcha, "unauth_captcha": db_guild.unauth_captcha,
"icon": db_guild.icon, "icon": guild["icon"],
"invite_link": db_guild.invite_link if db_guild.invite_link != None else "", "invite_link": db_guild.invite_link if db_guild.invite_link != None else "",
"guest_icon": db_guild.guest_icon if db_guild.guest_icon != None else "", "guest_icon": db_guild.guest_icon if db_guild.guest_icon != None else "",
"post_timeout": db_guild.post_timeout, "post_timeout": db_guild.post_timeout,

View File

@ -104,7 +104,7 @@ def get_online_discord_users(guild_id, embed):
apimembers_filtered = {} apimembers_filtered = {}
for member in apimembers: for member in apimembers:
apimembers_filtered[int(member["id"])] = member apimembers_filtered[int(member["id"])] = member
guild_roles = json.loads(db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().roles) guild_roles = redisqueue.get_guild(guild_id)["roles"]
guildroles_filtered = {} guildroles_filtered = {}
for role in guild_roles: for role in guild_roles:
guildroles_filtered[role["id"]] = role guildroles_filtered[role["id"]] = role
@ -144,19 +144,17 @@ def get_online_embed_users(guild_id):
return users return users
def get_guild_emojis(guild_id): def get_guild_emojis(guild_id):
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() return redisqueue.get_guild(guild_id)["emojis"]
return json.loads(dbguild.emojis)
def get_guild_roles(guild_id): def get_guild_roles(guild_id):
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() return redisqueue.get_guild(guild_id)["roles"]
return json.loads(dbguild.roles)
# Returns webhook url if exists and can post w/webhooks, otherwise None # Returns webhook url if exists and can post w/webhooks, otherwise None
def get_channel_webhook_url(guild_id, channel_id): def get_channel_webhook_url(guild_id, channel_id):
if not guild_webhooks_enabled(guild_id): if not guild_webhooks_enabled(guild_id):
return None return None
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() guild = redisqueue.get_guild(guild_id)
guild_webhooks = json.loads(dbguild.webhooks) guild_webhooks = guild["webhooks"]
name = "[Titan] " name = "[Titan] "
username = session["username"] username = session["username"]
if len(username) > 19: if len(username) > 19:
@ -517,8 +515,7 @@ def user_info(guild_id, user_id):
usr["avatar"] = member["avatar"] usr["avatar"] = member["avatar"]
usr["avatar_url"] = generate_avatar_url(usr["id"], usr["avatar"], usr["discriminator"], True) usr["avatar_url"] = generate_avatar_url(usr["id"], usr["avatar"], usr["discriminator"], True)
roles = get_member_roles(guild_id, user_id) roles = get_member_roles(guild_id, user_id)
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() guild_roles = redisqueue.get_guild(guild_id)["roles"]
guild_roles = json.loads(dbguild.roles)
for r in roles: for r in roles:
for gr in guild_roles: for gr in guild_roles:
if gr["id"] == r: if gr["id"] == r:

View File

@ -1,6 +1,6 @@
from flask import Blueprint, render_template, abort, redirect, url_for, session, request from flask import Blueprint, render_template, abort, redirect, url_for, session, request
from flask_babel import gettext from flask_babel import gettext
from titanembeds.utils import check_guild_existance, guild_query_unauth_users_bool, guild_accepts_visitors, guild_unauthcaptcha_enabled, is_int from titanembeds.utils import check_guild_existance, guild_query_unauth_users_bool, guild_accepts_visitors, guild_unauthcaptcha_enabled, is_int, redisqueue
from titanembeds.oauth import generate_guild_icon_url, generate_avatar_url from titanembeds.oauth import generate_guild_icon_url, generate_avatar_url
from titanembeds.database import db, Guilds, UserCSS, list_disabled_guilds from titanembeds.database import db, Guilds, UserCSS, list_disabled_guilds
from config import config from config import config
@ -64,15 +64,16 @@ def parse_url_domain(url):
@embed.route("/<int:guild_id>") @embed.route("/<int:guild_id>")
def guild_embed(guild_id): def guild_embed(guild_id):
if check_guild_existance(guild_id): if check_guild_existance(guild_id):
guild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() guild = redisqueue.get_guild(guild_id)
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
guild_dict = { guild_dict = {
"id": guild.guild_id, "id": guild["id"],
"name": guild.name, "name": guild["name"],
"unauth_users": guild.unauth_users, "unauth_users": dbguild.unauth_users,
"icon": guild.icon, "icon": guild["icon"],
"invite_link": guild.invite_link, "invite_link": dbguild.invite_link,
"invite_domain": parse_url_domain(guild.invite_link), "invite_domain": parse_url_domain(dbguild.invite_link),
"post_timeout": guild.post_timeout, "post_timeout": dbguild.post_timeout,
} }
customcss = get_custom_css() customcss = get_custom_css()
return render_template("embed.html.j2", return render_template("embed.html.j2",

View File

@ -1,5 +1,5 @@
from titanembeds.utils import socketio, guild_accepts_visitors, get_client_ipaddr, discord_api, check_user_in_guild, get_guild_channels, update_user_status, guild_webhooks_enabled, redis_store, redisqueue from titanembeds.utils import socketio, guild_accepts_visitors, get_client_ipaddr, discord_api, check_user_in_guild, get_guild_channels, update_user_status, guild_webhooks_enabled, redis_store, redisqueue
from titanembeds.database import db, Guilds from titanembeds.database import db
from flask_socketio import Namespace, emit, disconnect, join_room, leave_room from flask_socketio import Namespace, emit, disconnect, join_room, leave_room
import functools import functools
from flask import request, session from flask import request, session
@ -57,8 +57,7 @@ class Gateway(Namespace):
msg = {"unauthenticated": False, "id": str(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() guild_webhooks = redisqueue.get_guild(guild_id)["webhooks"]
guild_webhooks = json.loads(dbguild.webhooks)
name = "[Titan] " name = "[Titan] "
username = session["username"] username = session["username"]
if len(username) > 19: if len(username) > 19:
@ -128,7 +127,7 @@ class Gateway(Namespace):
member = redisqueue.get_guild_member(guild_id, user_id) member = redisqueue.get_guild_member(guild_id, user_id)
if not member: if not member:
return None return None
guild_roles = json.loads(db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().roles) guild_roles = redisqueue.get_guild(guild_id)["roles"]
guildroles_filtered = {} guildroles_filtered = {}
for role in guild_roles: for role in guild_roles:
guildroles_filtered[role["id"]] = role guildroles_filtered[role["id"]] = role

View File

@ -5,6 +5,7 @@ from config import config
from titanembeds.decorators import discord_users_only from titanembeds.decorators import discord_users_only
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, Cosmetics, UserCSS, Patreon, set_titan_token, get_titan_token, add_badge, list_disabled_guilds from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, Cosmetics, UserCSS, Patreon, set_titan_token, get_titan_token, add_badge, list_disabled_guilds
from titanembeds.oauth import authorize_url, token_url, make_authenticated_session, get_current_authenticated_user, get_user_managed_servers, check_user_can_administrate_guild, check_user_permission, generate_avatar_url, generate_guild_icon_url, generate_bot_invite_url from titanembeds.oauth import authorize_url, token_url, make_authenticated_session, get_current_authenticated_user, get_user_managed_servers, check_user_can_administrate_guild, check_user_permission, generate_avatar_url, generate_guild_icon_url, generate_bot_invite_url
from titanembeds.utils import redisqueue
import time import time
import datetime import datetime
import paypalrestsdk import paypalrestsdk
@ -188,11 +189,16 @@ def edit_custom_css_delete(css_id):
def administrate_guild(guild_id): def administrate_guild(guild_id):
if not check_user_can_administrate_guild(guild_id): if not check_user_can_administrate_guild(guild_id):
return redirect(url_for("user.dashboard")) return redirect(url_for("user.dashboard"))
db_guild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() guild = redisqueue.get_guild(guild_id)
if not db_guild: if not guild:
session["redirect"] = url_for("user.administrate_guild", guild_id=guild_id, _external=True) session["redirect"] = url_for("user.administrate_guild", guild_id=guild_id, _external=True)
return redirect(url_for("user.add_bot", guild_id=guild_id)) return redirect(url_for("user.add_bot", guild_id=guild_id))
session["redirect"] = None session["redirect"] = None
db_guild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
if not db_guild:
db_guild = Guilds(guild["id"])
db.session.add(db_guild)
db.session.commit()
permissions=[] permissions=[]
if check_user_permission(guild_id, 5): if check_user_permission(guild_id, 5):
permissions.append("Manage Embed Settings") permissions.append("Manage Embed Settings")
@ -206,7 +212,7 @@ def administrate_guild(guild_id):
users = prepare_guild_members_list(all_members, all_bans) users = prepare_guild_members_list(all_members, all_bans)
dbguild_dict = { dbguild_dict = {
"id": db_guild.guild_id, "id": db_guild.guild_id,
"name": db_guild.name, "name": guild["name"],
"unauth_users": db_guild.unauth_users, "unauth_users": db_guild.unauth_users,
"visitor_view": db_guild.visitor_view, "visitor_view": db_guild.visitor_view,
"webhook_messages": db_guild.webhook_messages, "webhook_messages": db_guild.webhook_messages,
@ -214,7 +220,7 @@ def administrate_guild(guild_id):
"bracket_links": db_guild.bracket_links, "bracket_links": db_guild.bracket_links,
"mentions_limit": db_guild.mentions_limit, "mentions_limit": db_guild.mentions_limit,
"unauth_captcha": db_guild.unauth_captcha, "unauth_captcha": db_guild.unauth_captcha,
"icon": db_guild.icon, "icon": guild["icon"],
"invite_link": db_guild.invite_link if db_guild.invite_link != None else "", "invite_link": db_guild.invite_link if db_guild.invite_link != None else "",
"guest_icon": db_guild.guest_icon if db_guild.guest_icon != None else "", "guest_icon": db_guild.guest_icon if db_guild.guest_icon != None else "",
"post_timeout": db_guild.post_timeout, "post_timeout": db_guild.post_timeout,

View File

@ -3,7 +3,6 @@ from titanembeds.database import db
class Guilds(db.Model): class Guilds(db.Model):
__tablename__ = "guilds" __tablename__ = "guilds"
guild_id = db.Column(db.BigInteger, nullable=False, primary_key=True) # Discord guild id guild_id = db.Column(db.BigInteger, nullable=False, primary_key=True) # Discord guild id
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
webhook_messages = db.Column(db.Boolean(), nullable=False, default=0) # Use webhooks to send messages instead of the bot webhook_messages = db.Column(db.Boolean(), nullable=False, default=0) # Use webhooks to send messages instead of the bot
@ -12,12 +11,6 @@ class Guilds(db.Model):
bracket_links = db.Column(db.Boolean(), nullable=False, default=1) # If appending brackets to links to prevent embed bracket_links = db.Column(db.Boolean(), nullable=False, default=1) # If appending brackets to links to prevent embed
unauth_captcha = db.Column(db.Boolean(), nullable=False, server_default="1")# Enforce captcha on guest users unauth_captcha = db.Column(db.Boolean(), nullable=False, server_default="1")# Enforce captcha on guest users
mentions_limit = db.Column(db.Integer, nullable=False, default=11) # If there is a limit on the number of mentions in a msg mentions_limit = db.Column(db.Integer, nullable=False, default=11) # If there is a limit on the number of mentions in a msg
roles = db.Column(db.Text().with_variant(db.Text(4294967295), 'mysql'), nullable=False) # Guild Roles
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
emojis = db.Column(db.Text().with_variant(db.Text(4294967295), 'mysql'), nullable=False) # Guild Emojis
owner_id = db.Column(db.BigInteger, nullable=False) # Snowflake of the owner
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
post_timeout = db.Column(db.Integer, nullable=False, server_default="5") # Seconds to elapse before another message can be posted from the widget post_timeout = db.Column(db.Integer, nullable=False, server_default="5") # Seconds to elapse before another message can be posted from the widget
max_message_length = db.Column(db.Integer, nullable=False, server_default="300") # Chars length the message should be before being rejected by the server max_message_length = db.Column(db.Integer, nullable=False, server_default="300") # Chars length the message should be before being rejected by the server
@ -25,9 +18,8 @@ class Guilds(db.Model):
banned_words_global_included = db.Column(db.Boolean(), nullable=False, server_default="0") # Add global banned words to the list banned_words_global_included = db.Column(db.Boolean(), nullable=False, server_default="0") # Add global banned words to the list
banned_words = db.Column(db.Text(), nullable=False, server_default="[]") # JSON list of strings to block from sending banned_words = db.Column(db.Text(), nullable=False, server_default="[]") # JSON list of strings to block from sending
def __init__(self, guild_id, name, roles, channels, webhooks, emojis, owner_id, icon): def __init__(self, guild_id):
self.guild_id = guild_id self.guild_id = guild_id
self.name = name
self.unauth_users = True # defaults to true self.unauth_users = True # defaults to true
self.visitor_view = False self.visitor_view = False
self.webhook_messages = False self.webhook_messages = False
@ -36,12 +28,6 @@ class Guilds(db.Model):
self.bracket_links = True self.bracket_links = True
self.unauth_captcha = True self.unauth_captcha = True
self.mentions_limit = -1 # -1 = unlimited mentions self.mentions_limit = -1 # -1 = unlimited mentions
self.roles = roles
self.channels = channels
self.webhooks = webhooks
self.emojis = emojis
self.owner_id = owner_id
self.icon = icon
def __repr__(self): def __repr__(self):
return '<Guilds {0} {1}>'.format(self.id, self.guild_id) return '<Guilds {0} {1}>'.format(self.id, self.guild_id)

View File

@ -108,4 +108,9 @@ class RedisQueue:
member = self.get_guild_member(guild_id, usr_id) member = self.get_guild_member(guild_id, usr_id)
if member: if member:
members.append(member) members.append(member)
return members return members
def get_guild(self, guild_id):
key = "/guilds/{}".format(guild_id)
q = self.get(key, "get_guild", {"guild_id": guild_id})
return q

View File

@ -70,8 +70,8 @@ def guild_ratelimit_key():
return (ip + guild_id) return (ip + guild_id)
def check_guild_existance(guild_id): def check_guild_existance(guild_id):
dbGuild = Guilds.query.filter_by(guild_id=guild_id).first() guild = redisqueue.get_guild(guild_id)
if not dbGuild: if not guild:
return False return False
else: else:
return True return True
@ -210,10 +210,10 @@ def get_guild_channels(guild_id, force_everyone=False):
bot_member_roles = get_member_roles(guild_id, config["client-id"]) bot_member_roles = get_member_roles(guild_id, config["client-id"])
if guild_id not in bot_member_roles: if guild_id not in bot_member_roles:
bot_member_roles.append(guild_id) bot_member_roles.append(guild_id)
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() guild = redisqueue.get_guild(guild_id)
guild_channels = json.loads(dbguild.channels) guild_channels = guild["channels"]
guild_roles = json.loads(dbguild.roles) guild_roles = guild["roles"]
guild_owner = str(dbguild.owner_id) guild_owner = guild["owner_id"]
result_channels = [] result_channels = []
for channel in guild_channels: for channel in guild_channels:
if channel['type'] in ["text", "category"]: if channel['type'] in ["text", "category"]:
@ -293,13 +293,13 @@ def get_channel_permission(channel, guild_id, guild_owner, guild_roles, member_r
def bot_can_create_webhooks(guild): def bot_can_create_webhooks(guild):
perm = 0 perm = 0
guild_roles = json.loads(guild.roles) guild_roles = guild["roles"]
# @everyone # @everyone
for role in guild_roles: for role in guild_roles:
if role["id"] == guild.guild_id: if role["id"] == guild["id"]:
perm |= role["permissions"] perm |= role["permissions"]
continue continue
member_roles = get_member_roles(guild.guild_id, config["client-id"]) member_roles = get_member_roles(guild["id"], config["client-id"])
# User Guild Roles # User Guild Roles
for m_role in member_roles: for m_role in member_roles:
for g_role in guild_roles: for g_role in guild_roles:
@ -314,7 +314,8 @@ def guild_webhooks_enabled(guild_id):
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
if not dbguild.webhook_messages: if not dbguild.webhook_messages:
return False return False
return bot_can_create_webhooks(dbguild) guild = redisqueue.get_guild(guild_id)
return bot_can_create_webhooks(guild)
def guild_unauthcaptcha_enabled(guild_id): def guild_unauthcaptcha_enabled(guild_id):
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()