Use discord name tag colours for bridged messages to matrix

legacy
Charlotte Som 2021-10-03 12:18:51 +01:00
parent 6032bf06d6
commit 19f28da49a
3 changed files with 19 additions and 4 deletions

View File

@ -25,9 +25,22 @@ struct DiscordHandler {
} }
async fn get_message_author(ctx: &Context, message: &Message) -> MessageAuthor { async fn get_message_author(ctx: &Context, message: &Message) -> MessageAuthor {
async fn user_tag_color(ctx: &Context, message: &Message) -> Option<String> {
Some(
message
.member(ctx)
.await
.ok()?
.colour(ctx)
.await?
.hex()
.to_ascii_lowercase(),
)
}
MessageAuthor { MessageAuthor {
display_name: message display_name: message
.author_nick(&ctx) .author_nick(ctx)
.await .await
.unwrap_or_else(|| message.author.name.clone()), .unwrap_or_else(|| message.author.name.clone()),
avatar_url: message avatar_url: message
@ -35,6 +48,7 @@ async fn get_message_author(ctx: &Context, message: &Message) -> MessageAuthor {
.static_avatar_url() .static_avatar_url()
.unwrap_or_else(|| message.author.default_avatar_url()), .unwrap_or_else(|| message.author.default_avatar_url()),
service_name: "discord".to_string(), service_name: "discord".to_string(),
display_color: user_tag_color(ctx, message).await,
} }
} }

View File

@ -104,6 +104,7 @@ impl MatrixHandler {
.to_string() .to_string()
}), }),
service_name: "matrix".to_string(), service_name: "matrix".to_string(),
display_color: None,
}) })
} else { } else {
None None
@ -250,10 +251,9 @@ fn generate_html_content(content: &[MessageComponent], author: &MessageAuthor) -
format_discord(content) 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!( let html_message = format!(
r##"<p><font data-mx-color="#9a9a9a" data-mx-bg-color="#000000">{} <small>({})</small></font>:</p> <p>{}</p>"##, r##"<strong><font data-mx-color="#{}">{} <small>({})</small></font>:</strong> {}"##,
author.display_color.as_deref().unwrap_or("ffffff"),
&author.display_name, &author.display_name,
&author.service_name, &author.service_name,
format_matrix(content), format_matrix(content),

View File

@ -12,6 +12,7 @@ pub struct MessageAuthor {
pub display_name: String, pub display_name: String,
pub avatar_url: String, pub avatar_url: String,
pub service_name: String, pub service_name: String,
pub display_color: Option<String>,
} }
pub struct SentMessage { pub struct SentMessage {