From 19f28da49abe2105acc562e5bf3214f061482c4a Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Sun, 3 Oct 2021 12:18:51 +0100 Subject: [PATCH] Use discord name tag colours for bridged messages to matrix --- src/discord.rs | 16 +++++++++++++++- src/matrix.rs | 6 +++--- src/messages.rs | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/discord.rs b/src/discord.rs index 127f8cd..1a7f45b 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -25,9 +25,22 @@ struct DiscordHandler { } async fn get_message_author(ctx: &Context, message: &Message) -> MessageAuthor { + async fn user_tag_color(ctx: &Context, message: &Message) -> Option { + Some( + message + .member(ctx) + .await + .ok()? + .colour(ctx) + .await? + .hex() + .to_ascii_lowercase(), + ) + } + MessageAuthor { display_name: message - .author_nick(&ctx) + .author_nick(ctx) .await .unwrap_or_else(|| message.author.name.clone()), avatar_url: message @@ -35,6 +48,7 @@ async fn get_message_author(ctx: &Context, message: &Message) -> MessageAuthor { .static_avatar_url() .unwrap_or_else(|| message.author.default_avatar_url()), service_name: "discord".to_string(), + display_color: user_tag_color(ctx, message).await, } } diff --git a/src/matrix.rs b/src/matrix.rs index 57b131d..e87803e 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -104,6 +104,7 @@ impl MatrixHandler { .to_string() }), service_name: "matrix".to_string(), + display_color: None, }) } else { None @@ -250,10 +251,9 @@ fn generate_html_content(content: &[MessageComponent], author: &MessageAuthor) - format_discord(content) ); - // TODO: Do we want to do something with different users' discord name colors? That could be a neat way - // to differentiate people easily without being able to see avatars let html_message = format!( - r##"

{} ({}):

{}

"##, + r##"{} ({}): {}"##, + author.display_color.as_deref().unwrap_or("ffffff"), &author.display_name, &author.service_name, format_matrix(content), diff --git a/src/messages.rs b/src/messages.rs index cab7692..cc065ac 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -12,6 +12,7 @@ pub struct MessageAuthor { pub display_name: String, pub avatar_url: String, pub service_name: String, + pub display_color: Option, } pub struct SentMessage {