From cf831fae8712f1e83ccf6b400c98fac3a6823a71 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Sat, 23 Oct 2021 14:33:32 -0400 Subject: [PATCH] osu! best minor fixes (#16) - Bump serenity version - Now no longer pings unless the play is within top 100 - Parse numbers in bold as well - Use relative time syntax in embeds --- Cargo.lock | 10 ++++++---- youmubot-osu/src/discord/announcer.rs | 26 ++++++++++++++++++++++++-- youmubot-osu/src/discord/embeds.rs | 19 ++++++------------- youmubot-osu/src/models/mod.rs | 14 ++++++++++++-- youmubot/Cargo.toml | 2 +- 5 files changed, 49 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4661ed2..b0aae03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "Inflector" version = "0.11.4" @@ -228,9 +230,9 @@ dependencies = [ [[package]] name = "command_attr" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fe1f0b69fde68f40ea2ee6ca8db23bc40d2e593db884659a65d8486032cc65b" +checksum = "8a6c3666f685cb1efc0628b8c984dbad9c372d080450736c7732089c385ed81d" dependencies = [ "proc-macro2", "quote", @@ -1485,9 +1487,9 @@ dependencies = [ [[package]] name = "serenity" -version = "0.10.5" +version = "0.10.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "deead3f7ecbbbe4c249e07af17686937ccb9d7fa24ca3accd1d223e369a75272" +checksum = "6275d443266aedf2be507a245ddc23db0c07b1b99774e16f3c879e96a78b067a" dependencies = [ "async-trait", "async-tungstenite", diff --git a/youmubot-osu/src/discord/announcer.rs b/youmubot-osu/src/discord/announcer.rs index 4696a13..fff0da9 100644 --- a/youmubot-osu/src/discord/announcer.rs +++ b/youmubot-osu/src/discord/announcer.rs @@ -262,14 +262,36 @@ impl<'a> CollectedScore<'a> { bm: &BeatmapWithMode, content: &BeatmapContent, ) -> Result { + let guild = match channel.to_channel(&ctx.c).await?.guild() { + Some(gc) => gc.guild_id, + None => { + eprintln!("Not a guild channel: {}", channel); + return Err(Error::msg("Trying to announce to a non-server channel")); + } + }; + + let member = match guild.member(&ctx.c, self.discord_user).await { + Ok(mem) => mem, + Err(e) => { + eprintln!("Cannot get member {}: {}", self.discord_user, e); + return Err(e.into()); + } + }; let m = channel .send_message(ctx.c.http(), |c| { c.content(match self.kind { ScoreType::TopRecord(_) => { format!("New top record from {}!", self.discord_user.mention()) } - ScoreType::WorldRecord(_) => { - format!("New best score from {}!", self.discord_user.mention()) + ScoreType::WorldRecord(rank) => { + if rank <= 100 { + format!( + "New leaderboard record from {}!", + self.discord_user.mention() + ) + } else { + format!("New leaderboard record from **{}**!", member.distinct()) + } } }) .embed(|e| { diff --git a/youmubot-osu/src/discord/embeds.rs b/youmubot-osu/src/discord/embeds.rs index 1b6467c..b4323a9 100644 --- a/youmubot-osu/src/discord/embeds.rs +++ b/youmubot-osu/src/discord/embeds.rs @@ -3,7 +3,6 @@ use crate::{ discord::oppai_cache::{BeatmapContent, BeatmapInfo, BeatmapInfoWithPP, OppaiAccuracy}, models::{Beatmap, Mode, Mods, Rank, Score, User}, }; -use chrono::Utc; use serenity::{builder::CreateEmbed, utils::MessageBuilder}; use youmubot_prelude::*; @@ -331,7 +330,7 @@ impl<'a> ScoreEmbedBuilder<'a> { .description(format!( r#"**Beatmap**: {} - {} [{}]**{} ** **Links**: [[Listing]]({}) [[Download]]({}) [[Bloodcat]]({}) -**Played on**: {} +**Played**: {} {}"#, b.artist, b.title, @@ -340,7 +339,7 @@ impl<'a> ScoreEmbedBuilder<'a> { b.link(), b.download_link(false), b.download_link(true), - s.date.format("%F %T"), + s.date.format(""), pp_gained.as_ref().map(|v| &v[..]).unwrap_or(""), )) .image(b.cover_url()) @@ -384,7 +383,7 @@ pub(crate) fn user_embed( .url(format!("https://osu.ppy.sh/users/{}", u.id)) .color(0xffb6c1) .thumbnail(format!("https://a.ppy.sh/{}", u.id)) - .description(format!("Member since **{}**", u.joined.format("%F %T"))) + .description(format!("Member since **{}**", u.joined.format(""))) .field( "Performance Points", u.pp.map(|v| format!("{:.2}pp", v)) @@ -405,8 +404,9 @@ pub(crate) fn user_embed( .field( "Play count / Play time", format!( - "{} ({})", + "{} / {} hours ({})", grouped_number(u.play_count), + u.played_time.as_secs() / 3600, Duration(u.played_time) ), false, @@ -442,14 +442,7 @@ pub(crate) fn user_embed( v.pp.unwrap() /*Top record should have pp*/ )) .push(" - ") - .push_line(format!( - "{:.1} ago", - Duration( - (Utc::now() - v.date) - .to_std() - .unwrap_or_else(|_| std::time::Duration::from_secs(1)) - ) - )) + .push_line(v.date.format("")) .push("on ") .push_line(format!( "[{} - {} [{}]]({})**{} **", diff --git a/youmubot-osu/src/models/mod.rs b/youmubot-osu/src/models/mod.rs index 98ebefd..959d9b4 100644 --- a/youmubot-osu/src/models/mod.rs +++ b/youmubot-osu/src/models/mod.rs @@ -13,7 +13,7 @@ pub use mods::Mods; use serenity::utils::MessageBuilder; lazy_static::lazy_static! { - static ref EVENT_RANK_REGEX: Regex = Regex::new(r#"^.+achieved rank #(\d+) on .+\((.+)\)$"#).unwrap(); + static ref EVENT_RANK_REGEX: Regex = Regex::new(r#"^.+achieved .*rank #(\d+).* on .+\((.+)\)$"#).unwrap(); } #[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] @@ -30,7 +30,7 @@ pub enum ApprovalStatus { impl fmt::Display for ApprovalStatus { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if let ApprovalStatus::Ranked(ref d) = self { - write!(f, "Ranked on {}", d.format("%F %T")) + write!(f, "Ranked on {}", d.format("")) } else { write!(f, "{:?}", self) } @@ -371,6 +371,11 @@ impl Beatmap { } } + /// Returns a direct link to the download (if you have supporter!) + pub fn osu_direct_link(&self) -> String { + format!("osu://b/{}", self.beatmapset_id) + } + /// Return a parsable short link. pub fn short_link(&self, override_mode: Option, mods: Option) -> String { format!( @@ -392,6 +397,11 @@ impl Beatmap { self.beatmapset_id ) } + + /// Link to the cover thumbnail of the beatmap. + pub fn thumbnail_url(&self) -> String { + format!("https://b.ppy.sh/thumb/{}l.jpg", self.beatmapset_id) + } } #[derive(Clone, Debug, Serialize, Deserialize)] diff --git a/youmubot/Cargo.toml b/youmubot/Cargo.toml index b8c796d..2ee64ac 100644 --- a/youmubot/Cargo.toml +++ b/youmubot/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["core", "osu", "codeforces"] +default = ["core", "osu"] # codeforces disabled for now core = [] osu = ["youmubot-osu"] codeforces = ["youmubot-cf"]