Webapp support for emoji for posting

This commit is contained in:
Jeremy Zhang 2017-05-29 22:29:16 +00:00
parent f25669e1bc
commit 8cdb5c8ccf
2 changed files with 18 additions and 15 deletions

View File

@ -100,29 +100,25 @@ def check_user_in_guild(guild_id):
return dbUser is not None and not checkUserRevoke(guild_id) return dbUser is not None and not checkUserRevoke(guild_id)
def parse_emoji(textToParse, 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 = []
emojis = re.findall("<:(.*?):(.*)?>", textToParse) emojis = re.findall(":(.*?):", textToParse)
guild_emojis = get_guild_emojis(guild_id)
newText = textToParse newText = textToParse
for emoji in response['content']['emojis']: for gemoji in guild_emojis:
name = emoji['name'] emoji_name = gemoji["name"]
emojiId = emoji['id'] emoji_id = gemoji["id"]
for emoji2 in emojis: for usremote in emojis:
if name.lower() == emoji2[0].lower(): if usremote == emoji_name:
newText = newText.replace("<:{}:{}>".format(name, emojiId), "<img src='https://cdn.discordapp.com/emojis/{}.png'></img>".format(emojiId)) newText = newText.replace(":{}:".format(emoji_name), "<:{}:{}>".format(emoji_name, emoji_id))
return newText return newText
def format_post_content(guild_id, message): def format_post_content(guild_id, message):
illegal_post = False illegal_post = False
illegal_reasons = [] illegal_reasons = []
message = parse_emoji(message, guild_id)
message = message.replace("<", "\<") message = message.replace("<", "\<")
message = message.replace(">", "\>") message = message.replace(">", "\>")
message = parse_emoji(message, 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()
@ -299,6 +295,10 @@ def get_online_embed_users(guild_id):
users['authenticated'].append(meta) users['authenticated'].append(meta)
return users 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"]) @api.route("/fetch", methods=["GET"])
@valid_session_required(api=True) @valid_session_required(api=True)
@rate_limiter.limit("2 per 2 second", key_func = channel_ratelimit_key) @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) channels = get_guild_channels(guild_id)
discordmembers = get_online_discord_users(guild_id, widget) discordmembers = get_online_discord_users(guild_id, widget)
embedmembers = get_online_embed_users(guild_id) 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(403)
abort(404) abort(404)

View File

@ -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 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 roles = db.Column(db.Text()) # Guild Roles
channels = db.Column(db.Text()) # Guild channels 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 owner_id = db.Column(db.String(255)) # 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
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.guild_id = guild_id
self.name = name self.name = name
self.unauth_users = True # defaults to true self.unauth_users = True # defaults to true
@ -23,6 +24,7 @@ class Guilds(db.Model):
self.mentions_limit = -1 # -1 = unlimited mentions self.mentions_limit = -1 # -1 = unlimited mentions
self.roles = roles self.roles = roles
self.channels = channels self.channels = channels
self.emojis = emojis
self.owner_id = owner_id self.owner_id = owner_id
self.icon = icon self.icon = icon