Use trait for username display, fix suggestion username

main
Elnu 1 year ago
parent 58dbb425ba
commit 6a0a99c193

@ -149,11 +149,7 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
break;
}
}
let username = if author.discriminator == 0 { // new usernames
author.name.clone()
} else {
format!("{}#{}", author.name, author.discriminator)
};
let username = author.username();
let username = &username;
if !existing_submitter {
let mut submitter_data = Map::new();
@ -470,7 +466,6 @@ pub async fn suggest(
am
});
m.embed(|e| {
let username = format!("{}#{}", author.name, author.discriminator);
e.title("New suggestion");
e.description(if let Context::Prefix(ctx) = ctx {
format!("{suggestion}\n\n[See original message]({})", ctx.msg.link(),)
@ -480,7 +475,7 @@ pub async fn suggest(
let mut embed_author = serenity::builder::CreateEmbedAuthor::default();
embed_author
.icon_url(get_avatar(author))
.name(username)
.name(author.username())
.url(format!("https://discord.com/users/{}", author.id));
e.set_author(embed_author);
if let Some(accent_color) = accent_color {

@ -13,6 +13,19 @@ use std::io::Read;
use std::io::Write;
use std::process::Command;
pub trait User {
fn username(&self) -> String;
}
impl User for serenity::User {
fn username(&self) -> String {
if self.discriminator == 0 {
return self.name.clone();
}
format!("{}#{}", self.name, self.discriminator)
}
}
pub fn get_challenge_number() -> i32 {
let challenge_dir = format!("{}/content/challenges", env::var("HUGO").unwrap());
let paths = fs::read_dir(challenge_dir).unwrap();
@ -386,6 +399,7 @@ pub async fn leaderboard(ctx: &Context<'_>) -> Result<(), Error> {
let discriminator = user.discriminator;
leaderboard_html.push_str(&format!("<tr><td>{place}</td><td><a href=\"{profile}\" target=\"_blank\" class=\"no-underline\"><img src=\"{avatar}\" onerror=\"this.src='/default.png'\"> <span class=\"underline\">{name}"));
// New usernames have #0000 discriminator
// Can't use User trait user.display_name() because of muted span
if discriminator != 0 {
leaderboard_html.push_str(&format!("<span class=\"muted\">#{:0>4}</span>", discriminator));
}

Loading…
Cancel
Save