From a7469f29404fc906bf4b85cc20ebd1160e56ca06 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Thu, 13 Apr 2017 06:05:04 +0000 Subject: [PATCH] Parse channel ids to names representation in messages --- titanembeds/blueprints/api/api.py | 4 ++-- titanembeds/static/js/embed.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/titanembeds/blueprints/api/api.py b/titanembeds/blueprints/api/api.py index 98cdb64..32d6ac4 100644 --- a/titanembeds/blueprints/api/api.py +++ b/titanembeds/blueprints/api/api.py @@ -186,8 +186,8 @@ def get_guild_channels(guild_id): result["read"] = False result["write"] = False - if result["read"]: - result_channels.append(result) + #if result["read"]: + result_channels.append(result) return sorted(result_channels, key=lambda k: k['channel']['position']) def filter_guild_channel(guild_id, channel_id): diff --git a/titanembeds/static/js/embed.js b/titanembeds/static/js/embed.js index fed556a..7b12074 100644 --- a/titanembeds/static/js/embed.js +++ b/titanembeds/static/js/embed.js @@ -10,6 +10,7 @@ var fetchtimeout; // fetch routine timer var last_message_id; // last message tracked var selected_channel = guild_id; // user selected channel, defaults to #general channel + var guild_channels = {}; // all server channels used to highlight channels in messages function element_in_view(element, fullyInView) { var pageTop = $(window).scrollTop(); @@ -138,6 +139,7 @@ $("#channels-list").empty(); for (var i = 0; i < channels.length; i++) { var chan = channels[i]; + guild_channels[chan.channel.id] = chan; if (chan.read) { var rendered = Mustache.render(template, {"channelid": chan.channel.id, "channelname": chan.channel.name}); $("#channels-list").append(rendered); @@ -306,6 +308,15 @@ return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2'); } + function parse_channels_in_message(message) { + var channelids = Object.keys(guild_channels); + for (var i = 0; i < channelids.length; i++) { + var pattern = "<#" + channelids[i] + ">"; + message.content = message.content.replace(new RegExp(pattern, "g"), "#" + guild_channels[channelids[i]].channel.name); + } + return message; + } + function fill_discord_messages(messages, jumpscroll) { if (messages.length == 0) { return last_message_id; @@ -319,6 +330,7 @@ message = format_bot_message(message); message = parse_message_time(message); message = parse_message_attachments(message); + message = parse_channels_in_message(message); var rendered = Mustache.render(template, {"id": message.id, "full_timestamp": message.formatted_timestamp, "time": message.formatted_time, "username": message.author.username, "discriminator": message.author.discriminator, "content": nl2br(escapeHtml(message.content))}); $("#chatcontent").append(rendered); last = message.id;