Chat username discord colors!

This commit is contained in:
Jeremy Zhang 2017-09-15 06:25:03 +00:00
parent 286f395b72
commit 80d89b49f8
3 changed files with 122 additions and 9 deletions

View File

@ -241,9 +241,8 @@ def post():
userid = session["user_id"] userid = session["user_id"]
content = format_everyone_mention(chan, content) content = format_everyone_mention(chan, content)
webhook = get_channel_webhook_url(guild_id, channel_id) webhook = get_channel_webhook_url(guild_id, channel_id)
if userid in get_administrators_list(): # if userid in get_administrators_list():
oldcontent = content # content = "(Titan Dev) " + content
content = "(Titan Dev) " + oldcontent
if webhook: if webhook:
if (session['unauthenticated']): if (session['unauthenticated']):
username = session["username"] + "#" + str(session["user_id"]) username = session["username"] + "#" + str(session["user_id"])
@ -258,10 +257,9 @@ def post():
if dbUser: if dbUser:
if dbUser.nickname: if dbUser.nickname:
username = dbUser.nickname username = dbUser.nickname
if content.startswith("(Titan Dev) "): # if content.startswith("(Titan Dev) "):
content = content[12:] # content = content[12:]
username = "(Titan Dev) " + username # username = "(Titan Dev) " + username
else:
username = username + "#" + str(session['discriminator']) username = username + "#" + str(session['discriminator'])
avatar = session['avatar'] avatar = session['avatar']
message = discord_api.execute_webhook(webhook.get("id"), webhook.get("token"), username, avatar, content) message = discord_api.execute_webhook(webhook.get("id"), webhook.get("token"), username, avatar, content)

View File

@ -106,3 +106,62 @@ class Gateway(Namespace):
'user_id': session['user_id'], 'user_id': session['user_id'],
} }
emit("current_user_info", usr) emit("current_user_info", usr)
def get_user_color(self, guild_id, user_id):
color = None
member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == user_id).first()
if not member:
return None
guild_roles = json.loads(db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().roles)
guildroles_filtered = {}
for role in guild_roles:
guildroles_filtered[role["id"]] = role
member_roleids = json.loads(member.roles)
member_roles = []
for roleid in member_roleids:
role = guildroles_filtered.get(roleid)
if not role:
continue
member_roles.append(role)
member_roles = sorted(member_roles, key=lambda k: k['position'])
for role in member_roles:
if role["color"] != 0:
color = '{0:02x}'.format(role["color"])
while len(color) < 6:
color = "0" + color
return color
def on_lookup_user_info(self, data):
guild_id = data["guild_id"]
name = data["name"]
discriminator = data["discriminator"]
usr = {
"name": name,
"id": None,
"username": None,
"nickname": None,
"discriminator": discriminator,
"avatar": None,
"color": None,
"avatar_url": None,
}
member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.username == name, GuildMembers.discriminator == discriminator).first()
if member:
usr["id"] = member.user_id
usr["username"] = member.username
usr["nickname"] = member.nickname
usr["avatar"] = member.avatar
usr["color"] = self.get_user_color(guild_id, usr["id"])
if (usr["avatar"]):
usr["avatar_url"] = "https://cdn.discordapp.com/avatars/{}/{}.jpg".format(usr["id"], usr["avatar"])
else:
member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.nickname == name, GuildMembers.discriminator == discriminator).first()
if member:
usr["id"] = member.user_id
usr["username"] = member.username
usr["nickname"] = member.nickname
usr["avatar"] = member.avatar
usr["color"] = self.get_user_color(guild_id, usr["id"])
if (usr["avatar"]):
usr["avatar_url"] = "https://cdn.discordapp.com/avatars/{}/{}.jpg".format(usr["id"], usr["avatar"])
emit("lookup_user_info", usr)

View File

@ -10,6 +10,7 @@
/* global location */ /* global location */
/* global io */ /* global io */
/* global twemoji */ /* global twemoji */
/* global jQuery */
(function () { (function () {
const theme_options = ["DiscordDark", "BetterTitan"]; // All the avaliable theming names const theme_options = ["DiscordDark", "BetterTitan"]; // All the avaliable theming names
@ -29,6 +30,7 @@
var unauthenticated_users_list = []; // List of all guest users var unauthenticated_users_list = []; // List of all guest users
var discord_users_list = []; // List of all discord users that are probably online var discord_users_list = []; // List of all discord users that are probably online
var guild_channels_list = []; // guild channels, but as a list of them var guild_channels_list = []; // guild channels, but as a list of them
var message_users_cache = {}; // {"name#discrim": {"data": {}, "msgs": []} Cache of the users fetched from websockets to paint the messages
var shift_pressed = false; // Track down if shift pressed on messagebox var shift_pressed = false; // Track down if shift pressed on messagebox
function element_in_view(element, fullyInView) { function element_in_view(element, fullyInView) {
@ -797,6 +799,12 @@
replace.html($(rendered).html()); replace.html($(rendered).html());
replace.find(".blockcode").find("br").remove(); replace.find(".blockcode").find("br").remove();
} }
var usrcachekey = username + "#" + message.author.discriminator;
if (!(usrcachekey in message_users_cache)) {
message_users_cache[usrcachekey] = {"data": {}, "msgs": []};
}
message_users_cache[usrcachekey]["msgs"].push(message.id);
last = message.id; last = message.id;
} }
if (replace == null) { if (replace == null) {
@ -806,6 +814,7 @@
target: "_blank" target: "_blank"
}); });
$('.tooltipped').tooltip(); $('.tooltipped').tooltip();
process_message_users_cache();
return last; return last;
} }
@ -862,6 +871,42 @@
}); });
} }
function process_message_users_cache() {
var keys = Object.keys(message_users_cache);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var hashpos = key.lastIndexOf("#");
var name = key.substring(0, hashpos);
var discriminator = key.substring(hashpos+1);
if (name.startsWith("(Titan Dev) ")) {
name = name.substring(12);
}
var key_helper = name + "#" + discriminator;
if (jQuery.isEmptyObject(message_users_cache[key_helper]["data"])) {
if (socket) {
socket.emit("lookup_user_info", {"guild_id": guild_id, "name": name, "discriminator": discriminator});
}
} else {
process_message_users_cache_helper(key_helper, message_users_cache[key_helper]["data"]);
}
}
}
function process_message_users_cache_helper(key, usr) {
var msgs = message_users_cache[key]["msgs"];
while (msgs.length > 0) {
var element = $("#discordmessage_"+msgs.pop());
var parent = element.parent();
if (usr.color) {
parent.find(".chatusername").css("color", "#"+usr.color);
} else {
parent.find(".chatusername").css("color", null);
}
parent.attr("discord_userid", usr.id);
}
}
function update_embed_userchip(authenticated, avatar, username, nickname, userid, discrim=null) { function update_embed_userchip(authenticated, avatar, username, nickname, userid, discrim=null) {
if (authenticated) { if (authenticated) {
$("#currentuserimage").show(); $("#currentuserimage").show();
@ -1032,6 +1077,7 @@
socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + "/gateway", {path: '/gateway', transports: ['websocket']}); socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + "/gateway", {path: '/gateway', transports: ['websocket']});
socket.on('connect', function () { socket.on('connect', function () {
socket.emit('identify', {"guild_id": guild_id, "visitor_mode": visitor_mode}); socket.emit('identify', {"guild_id": guild_id, "visitor_mode": visitor_mode});
process_message_users_cache();
}); });
socket.on("disconnect", function () { socket.on("disconnect", function () {
@ -1207,6 +1253,16 @@
socket.on("current_user_info", function (usr) { socket.on("current_user_info", function (usr) {
update_embed_userchip(true, usr.avatar, usr.username, usr.nickname, usr.userid, usr.discriminator); update_embed_userchip(true, usr.avatar, usr.username, usr.nickname, usr.userid, usr.discriminator);
}); });
socket.on("lookup_user_info", function (usr) {
var key = usr.name + "#" + usr.discriminator;
var cache = message_users_cache[key];
if (!cache) {
return;
}
cache["data"] = usr;
process_message_users_cache_helper(key, usr);
});
} }
function update_socket_channels() { function update_socket_channels() {