Parse channel ids to names representation in messages

This commit is contained in:
Jeremy Zhang 2017-04-13 06:05:04 +00:00
parent b9b644b094
commit a7469f2940
2 changed files with 14 additions and 2 deletions

View File

@ -186,8 +186,8 @@ def get_guild_channels(guild_id):
result["read"] = False result["read"] = False
result["write"] = False result["write"] = False
if result["read"]: #if result["read"]:
result_channels.append(result) result_channels.append(result)
return sorted(result_channels, key=lambda k: k['channel']['position']) return sorted(result_channels, key=lambda k: k['channel']['position'])
def filter_guild_channel(guild_id, channel_id): def filter_guild_channel(guild_id, channel_id):

View File

@ -10,6 +10,7 @@
var fetchtimeout; // fetch routine timer var fetchtimeout; // fetch routine timer
var last_message_id; // last message tracked var last_message_id; // last message tracked
var selected_channel = guild_id; // user selected channel, defaults to #general channel 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) { function element_in_view(element, fullyInView) {
var pageTop = $(window).scrollTop(); var pageTop = $(window).scrollTop();
@ -138,6 +139,7 @@
$("#channels-list").empty(); $("#channels-list").empty();
for (var i = 0; i < channels.length; i++) { for (var i = 0; i < channels.length; i++) {
var chan = channels[i]; var chan = channels[i];
guild_channels[chan.channel.id] = chan;
if (chan.read) { if (chan.read) {
var rendered = Mustache.render(template, {"channelid": chan.channel.id, "channelname": chan.channel.name}); var rendered = Mustache.render(template, {"channelid": chan.channel.id, "channelname": chan.channel.name});
$("#channels-list").append(rendered); $("#channels-list").append(rendered);
@ -306,6 +308,15 @@
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2'); 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) { function fill_discord_messages(messages, jumpscroll) {
if (messages.length == 0) { if (messages.length == 0) {
return last_message_id; return last_message_id;
@ -319,6 +330,7 @@
message = format_bot_message(message); message = format_bot_message(message);
message = parse_message_time(message); message = parse_message_time(message);
message = parse_message_attachments(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))}); 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); $("#chatcontent").append(rendered);
last = message.id; last = message.id;