From 8cdb5c8ccfadafa3ca890b0a0969cb0a8d6304b8 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Mon, 29 May 2017 22:29:16 +0000 Subject: [PATCH] Webapp support for emoji for posting --- webapp/titanembeds/blueprints/api/api.py | 29 ++++++++++++------------ webapp/titanembeds/database/guilds.py | 4 +++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index 3fb25d7..847a613 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -100,29 +100,25 @@ def check_user_in_guild(guild_id): return dbUser is not None and not checkUserRevoke(guild_id) def parse_emoji(textToParse, guild_id): - _endpoint = "/guilds/{guild_id}".format(guild_id=guild_id) - _method = "GET" - response = discord_api.request(_method, _endpoint) - if not response.get("success", False): - return textToParse emojis = [] - emojis = re.findall("<:(.*?):(.*)?>", textToParse) + emojis = re.findall(":(.*?):", textToParse) + guild_emojis = get_guild_emojis(guild_id) newText = textToParse - for emoji in response['content']['emojis']: - name = emoji['name'] - emojiId = emoji['id'] - for emoji2 in emojis: - if name.lower() == emoji2[0].lower(): - newText = newText.replace("<:{}:{}>".format(name, emojiId), "".format(emojiId)) + for gemoji in guild_emojis: + emoji_name = gemoji["name"] + emoji_id = gemoji["id"] + for usremote in emojis: + if usremote == emoji_name: + newText = newText.replace(":{}:".format(emoji_name), "<:{}:{}>".format(emoji_name, emoji_id)) return newText def format_post_content(guild_id, message): illegal_post = False illegal_reasons = [] - message = parse_emoji(message, guild_id) message = message.replace("<", "\<") message = message.replace(">", "\>") + message = parse_emoji(message, guild_id) dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() @@ -299,6 +295,10 @@ def get_online_embed_users(guild_id): users['authenticated'].append(meta) 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) + @api.route("/fetch", methods=["GET"]) @valid_session_required(api=True) @rate_limiter.limit("2 per 2 second", key_func = channel_ratelimit_key) @@ -403,7 +403,8 @@ def query_guild(): channels = get_guild_channels(guild_id) discordmembers = get_online_discord_users(guild_id, widget) embedmembers = get_online_embed_users(guild_id) - return jsonify(channels=channels, discordmembers=discordmembers, embedmembers=embedmembers, instant_invite=widget.get("instant_invite")) + emojis = get_guild_emojis(guild_id) + return jsonify(channels=channels, discordmembers=discordmembers, embedmembers=embedmembers, emojis=emojis, instant_invite=widget.get("instant_invite")) abort(403) abort(404) diff --git a/webapp/titanembeds/database/guilds.py b/webapp/titanembeds/database/guilds.py index 6f3f588..3c2ce81 100644 --- a/webapp/titanembeds/database/guilds.py +++ b/webapp/titanembeds/database/guilds.py @@ -11,10 +11,11 @@ class Guilds(db.Model): mentions_limit = db.Column(db.Integer) # If there is a limit on the number of mentions in a msg roles = db.Column(db.Text()) # Guild Roles channels = db.Column(db.Text()) # Guild channels + emojis = db.Column(db.Text()) # Guild Emojis owner_id = db.Column(db.String(255)) # Snowflake of the owner icon = db.Column(db.String(255)) # The icon string, null if none - def __init__(self, guild_id, name, roles, channels, owner_id, icon): + def __init__(self, guild_id, name, roles, channels, emojis, owner_id, icon): self.guild_id = guild_id self.name = name self.unauth_users = True # defaults to true @@ -23,6 +24,7 @@ class Guilds(db.Model): self.mentions_limit = -1 # -1 = unlimited mentions self.roles = roles self.channels = channels + self.emojis = emojis self.owner_id = owner_id self.icon = icon