Skip to content

Commit

Permalink
Parse role mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
EndenDragon committed Mar 4, 2018
1 parent d438b6d commit 487f575
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
5 changes: 5 additions & 0 deletions discordbot/titanembeds/socketio/socketiointerface.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ def get_formatted_role(self, role):
rol = {
"id": role.id,
"guild_id": role.server.id,
"name": role.name,
"color": role.color.value,
"hoist": role.hoist,
"position": role.position,
"permissions": role.permissions.value,
}
return rol

Expand Down
7 changes: 6 additions & 1 deletion webapp/titanembeds/blueprints/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def get_guild_emojis(guild_id):
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
return json.loads(dbguild.emojis)

def get_guild_roles(guild_id):
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
return json.loads(dbguild.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):
Expand Down Expand Up @@ -387,11 +391,12 @@ def process_query_guild(guild_id, visitor=False):
discordmembers = [{"id": 0, "color": "FFD6D6", "status": "dnd", "username": "Discord Server Widget is Currently Disabled"}]
embedmembers = get_online_embed_users(guild_id)
emojis = get_guild_emojis(guild_id)
roles = get_guild_roles(guild_id)
guest_icon = get_guild_guest_icon(guild_id)
if visitor:
for channel in channels:
channel["write"] = False
return jsonify(channels=channels, discordmembers=discordmembers, embedmembers=embedmembers, emojis=emojis, guest_icon=guest_icon, instant_invite=widget.get("instant_invite", None))
return jsonify(channels=channels, discordmembers=discordmembers, embedmembers=embedmembers, emojis=emojis, roles=roles, guest_icon=guest_icon, instant_invite=widget.get("instant_invite", None))

@api.route("/query_guild", methods=["GET"])
@valid_session_required(api=True)
Expand Down
4 changes: 2 additions & 2 deletions webapp/titanembeds/static/css/embedstyle.css
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,14 @@ p.mentioned span.chatmessage {
margin-bottom: 3px;
}

.chatmessage .channellink, .chatmessage .discordmention {
.chatmessage .channellink, .chatmessage .discordmention, .rolemention {
border-radius: 5px;
background-color: rgba(0, 0, 0, 0.2);
color: #82b1ff;
cursor: pointer;
}

.chatmessage .channellink:hover, .chatmessage .discordmention:hover {
.chatmessage .channellink:hover, .chatmessage .discordmention:hover, .rolemention:hover {
background-color: rgba(0, 0, 0, 0.5);
color: white;
}
Expand Down
37 changes: 34 additions & 3 deletions webapp/titanembeds/static/js/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
var notification_sound = null; // Sound Manager 2 demonstrative.mp3 object https://notificationsounds.com/message-tones/demonstrative-516
var notification_sound_setting; // nothing, mentions, newmsgs - to control what new sound it makes
var display_richembeds; // true/false - if rich embeds should be displayed
var guild_roles_list = []; // List of all guild roles

function element_in_view(element, fullyInView) {
var pageTop = $(window).scrollTop();
Expand Down Expand Up @@ -519,6 +520,7 @@
function prepare_guild(guildobj) {
global_guest_icon = guildobj.guest_icon;
emoji_store = guildobj.emojis;
guild_roles_list = guildobj.roles;
fill_channels(guildobj.channels);
fill_discord_members(guildobj.discordmembers);
fill_authenticated_users(guildobj.embedmembers.authenticated);
Expand Down Expand Up @@ -889,7 +891,18 @@
var rendered = Mustache.render(template, {"username": username, "discriminator": zeroPad(mention.discriminator)}).trim();
message.content = message.content.replace(new RegExp("<@" + mention.id + ">", 'g'), rendered);
message.content = message.content.replace(new RegExp("<@!" + mention.id + ">", 'g'), rendered);
message.content = message.content.replace("<@&" + guild_id + ">", "@everyone");
}

var template = $("#mustache_rolemention").html();
Mustache.parse(template);
for (var i = 0; i < guild_roles_list.length; i++) {
var role = guild_roles_list[i];
var roleobj = {"rolename": role.name};
if (role.color) {
roleobj.color = "#" + role.color.toString(16);
}
var rendered = Mustache.render(template, roleobj).trim();
message.content = message.content.replace("&lt;@&amp;" + role.id + "&gt;", rendered);
}
return message;
}
Expand Down Expand Up @@ -1665,12 +1678,30 @@
update_socket_channels();
});

socket.on("GUILD_ROLE_UPDATE", function (chan) {
socket.on("GUILD_ROLE_CREATE", function (role) {
update_socket_channels();
guild_roles_list.push(role);
});

socket.on("GUILD_ROLE_DELETE", function (chan) {
socket.on("GUILD_ROLE_UPDATE", function (role) {
update_socket_channels();
for (var i = 0; i < guild_roles_list.length; i++) {
if (guild_roles_list[i].id == role.id) {
guild_roles_list.splice(i, 1);
guild_roles_list.push(role);
return;
}
}
});

socket.on("GUILD_ROLE_DELETE", function (role) {
update_socket_channels();
for (var i = 0; i < guild_roles_list.length; i++) {
if (guild_roles_list[i].id == role.id) {
guild_roles_list.splice(i, 1);
return;
}
}
});

socket.on("channel_list", function (chans) {
Expand Down
4 changes: 4 additions & 0 deletions webapp/titanembeds/templates/embed.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@
<span class="discordmention"><span class="atsign">@</span><span class="username">{{username}}</span><span class="hash">#</span><span class="discriminator">{{discriminator}}</span></span>
</script>

<script id="mustache_rolemention" type="text/template">
<span class="rolemention" {{#color}}style="color: {{color}}"{{/color}}><span class="atsign">@</span><span class="rolename">{{rolename}}</span></span>
</script>

<script id="mustache_richembed" type="text/template">
<div class="richembed">
{{#color}}
Expand Down

0 comments on commit 487f575

Please sign in to comment.