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)
async def on_guild_join(self, guild):
await self.database.update_guild(guild)
await self.redisqueue.update_guild(guild)
await self.postStats()
async def on_guild_remove(self, guild):
await self.database.remove_guild(guild)
await self.redisqueue.delete_guild(guild)
await self.postStats()
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)
async def on_guild_role_create(self, role):
if role.name == self.user.name and role.managed:
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)
async def on_guild_role_delete(self, role):
if role.guild.me not in role.guild.members:
return
await self.database.update_guild(role.guild)
await self.redisqueue.update_guild(role.guild)
await self.socketio.on_guild_role_delete(role)
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)
async def on_channel_delete(self, channel):
if channel.guild:
await self.database.update_guild(channel.guild)
await self.redisqueue.update_guild(channel.guild)
await self.socketio.on_channel_delete(channel)
async def on_channel_create(self, channel):
if channel.guild:
await self.database.update_guild(channel.guild)
await self.redisqueue.update_guild(channel.guild)
await self.socketio.on_channel_create(channel)
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)
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):
if len(after) == 0:
await self.database.update_guild(guild)
await self.redisqueue.update_guild(guild)
await self.socketio.on_guild_emojis_update(before)
else:
await self.database.update_guild(guild)
await self.redisqueue.update_guild(guild)
await self.socketio.on_guild_emojis_update(after)
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):
message_id = payload.message_id
@ -190,7 +190,7 @@ class Titan(discord.AutoShardedClient):
async def raw_bulk_message_delete(self, payload):
message_ids = payload.message_ids
channel_ids = payload.channel_id
channel_id = payload.channel_id
await asyncio.sleep(1)
for msgid in message_ids:
msgid = int(msgid)

View File

@ -5,7 +5,6 @@ import datetime
db = Gino()
from titanembeds.database.guilds import Guilds
from titanembeds.database.unauthenticated_users import UnauthenticatedUsers
from titanembeds.database.unauthenticated_bans import UnauthenticatedBans
@ -18,57 +17,6 @@ class DatabaseInterface(object):
async def connect(self, 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):
dbuser = None
if discriminator:

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
import asyncio_redis
import json
@ -70,6 +70,8 @@ class RedisQueue:
members = await self.connection.smembers(key)
for member in members:
the_member = await member
if not the_member:
continue
parsed = json.loads(the_member)
if re.match(str(dict_value_pattern), str(parsed[dict_key])):
unformatted_item = the_member
@ -94,7 +96,7 @@ class RedisQueue:
messages = []
async for message in channel.history(limit=50):
formatted = get_formatted_message(message)
messages.append(json.dumps(formatted))
messages.append(json.dumps(formatted, separators=(',', ':')))
await self.connection.sadd(key, [""] + messages)
async def push_message(self, message):
@ -103,7 +105,7 @@ class RedisQueue:
exists = await self.connection.exists(key)
if exists:
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):
if message.guild:
@ -125,7 +127,7 @@ class RedisQueue:
return
user = get_formatted_user(member)
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):
guild = self.bot.get_guild(int(params["guild_id"]))
@ -141,7 +143,7 @@ class RedisQueue:
result = ""
else:
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_param = {"guild_id": guild.id, "user_id": result_id}
await self.on_get_guild_member(get_guild_member_key, get_guild_member_param)
@ -152,7 +154,7 @@ class RedisQueue:
members = guild.members
member_ids = []
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_param = {"guild_id": guild.id, "user_id": member.id}
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)
exists = await self.connection.exists(key)
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):
if not guild:
guild = member.guild
guild_member_key = "Queue/guilds/{}/members/{}".format(guild.id, member.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])
async def update_member(self, member):
@ -178,3 +180,31 @@ class RedisQueue:
async def ban_member(self, guild, user):
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
def get_formatted_guild(guild):
def get_formatted_guild(guild, webhooks=[]):
guil = {
"id": str(guild.id),
"name": guild.name,
"icon": guild.icon,
"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

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"])
@is_admin
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()
if not db_guild:
abort(500)
return
db_guild = Guilds(guild["id"])
db.session.add(db_guild)
db.session.commit()
session["redirect"] = None
cosmetics = db.session.query(Cosmetics).filter(Cosmetics.user_id == session['user_id']).first()
permissions=[]
@ -169,8 +174,8 @@ def administrate_guild(guild_id):
all_bans = db.session.query(UnauthenticatedBans).filter(UnauthenticatedBans.guild_id == guild_id).all()
users = prepare_guild_members_list(all_members, all_bans)
dbguild_dict = {
"id": db_guild.guild_id,
"name": db_guild.name,
"id": guild["id"],
"name": guild["name"],
"unauth_users": db_guild.unauth_users,
"visitor_view": db_guild.visitor_view,
"webhook_messages": db_guild.webhook_messages,
@ -178,7 +183,7 @@ def administrate_guild(guild_id):
"bracket_links": db_guild.bracket_links,
"mentions_limit": db_guild.mentions_limit,
"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 "",
"guest_icon": db_guild.guest_icon if db_guild.guest_icon != None else "",
"post_timeout": db_guild.post_timeout,

View File

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

View File

@ -1,6 +1,6 @@
from flask import Blueprint, render_template, abort, redirect, url_for, session, request
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.database import db, Guilds, UserCSS, list_disabled_guilds
from config import config
@ -64,15 +64,16 @@ def parse_url_domain(url):
@embed.route("/<int:guild_id>")
def guild_embed(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 = {
"id": guild.guild_id,
"name": guild.name,
"unauth_users": guild.unauth_users,
"icon": guild.icon,
"invite_link": guild.invite_link,
"invite_domain": parse_url_domain(guild.invite_link),
"post_timeout": guild.post_timeout,
"id": guild["id"],
"name": guild["name"],
"unauth_users": dbguild.unauth_users,
"icon": guild["icon"],
"invite_link": dbguild.invite_link,
"invite_domain": parse_url_domain(dbguild.invite_link),
"post_timeout": dbguild.post_timeout,
}
customcss = get_custom_css()
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.database import db, Guilds
from titanembeds.database import db
from flask_socketio import Namespace, emit, disconnect, join_room, leave_room
import functools
from flask import request, session
@ -57,8 +57,7 @@ class Gateway(Namespace):
msg = {"unauthenticated": False, "id": str(session["user_id"])}
emit("embed_user_disconnect", msg, room="GUILD_"+guild_id)
if guild_webhooks_enabled(guild_id): # Delete webhooks
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
guild_webhooks = json.loads(dbguild.webhooks)
guild_webhooks = redisqueue.get_guild(guild_id)["webhooks"]
name = "[Titan] "
username = session["username"]
if len(username) > 19:
@ -128,7 +127,7 @@ class Gateway(Namespace):
member = redisqueue.get_guild_member(guild_id, user_id)
if not member:
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 = {}
for role in guild_roles:
guildroles_filtered[role["id"]] = role

View File

@ -5,6 +5,7 @@ from config import config
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.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 datetime
import paypalrestsdk
@ -188,11 +189,16 @@ def edit_custom_css_delete(css_id):
def administrate_guild(guild_id):
if not check_user_can_administrate_guild(guild_id):
return redirect(url_for("user.dashboard"))
db_guild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
if not db_guild:
guild = redisqueue.get_guild(guild_id)
if not guild:
session["redirect"] = url_for("user.administrate_guild", guild_id=guild_id, _external=True)
return redirect(url_for("user.add_bot", guild_id=guild_id))
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=[]
if check_user_permission(guild_id, 5):
permissions.append("Manage Embed Settings")
@ -206,7 +212,7 @@ def administrate_guild(guild_id):
users = prepare_guild_members_list(all_members, all_bans)
dbguild_dict = {
"id": db_guild.guild_id,
"name": db_guild.name,
"name": guild["name"],
"unauth_users": db_guild.unauth_users,
"visitor_view": db_guild.visitor_view,
"webhook_messages": db_guild.webhook_messages,
@ -214,7 +220,7 @@ def administrate_guild(guild_id):
"bracket_links": db_guild.bracket_links,
"mentions_limit": db_guild.mentions_limit,
"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 "",
"guest_icon": db_guild.guest_icon if db_guild.guest_icon != None else "",
"post_timeout": db_guild.post_timeout,

View File

@ -3,7 +3,6 @@ from titanembeds.database import db
class Guilds(db.Model):
__tablename__ = "guilds"
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
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
@ -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
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
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
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
@ -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 = 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.name = name
self.unauth_users = True # defaults to true
self.visitor_view = False
self.webhook_messages = False
@ -36,12 +28,6 @@ class Guilds(db.Model):
self.bracket_links = True
self.unauth_captcha = True
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):
return '<Guilds {0} {1}>'.format(self.id, self.guild_id)

View File

@ -109,3 +109,8 @@ class RedisQueue:
if member:
members.append(member)
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)
def check_guild_existance(guild_id):
dbGuild = Guilds.query.filter_by(guild_id=guild_id).first()
if not dbGuild:
guild = redisqueue.get_guild(guild_id)
if not guild:
return False
else:
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"])
if guild_id not in bot_member_roles:
bot_member_roles.append(guild_id)
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
guild_channels = json.loads(dbguild.channels)
guild_roles = json.loads(dbguild.roles)
guild_owner = str(dbguild.owner_id)
guild = redisqueue.get_guild(guild_id)
guild_channels = guild["channels"]
guild_roles = guild["roles"]
guild_owner = guild["owner_id"]
result_channels = []
for channel in guild_channels:
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):
perm = 0
guild_roles = json.loads(guild.roles)
guild_roles = guild["roles"]
# @everyone
for role in guild_roles:
if role["id"] == guild.guild_id:
if role["id"] == guild["id"]:
perm |= role["permissions"]
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
for m_role in member_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()
if not dbguild.webhook_messages:
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):
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()