diff options
Diffstat (limited to 'src/commands/meme/history.rs')
| -rw-r--r-- | src/commands/meme/history.rs | 104 |
1 files changed, 57 insertions, 47 deletions
diff --git a/src/commands/meme/history.rs b/src/commands/meme/history.rs index 270ae9a..0fe809c 100644 --- a/src/commands/meme/history.rs +++ b/src/commands/meme/history.rs @@ -1,4 +1,8 @@ -use chrono::TimeZone; +use chrono::{ + NaiveTime, + TimeZone, + Utc, +}; use diesel::{ result::Error as DieselError, NotFound, @@ -8,15 +12,21 @@ use itertools::Itertools; use lazy_static::lazy_static; use serenity::{ all::{ - GuildId, + FormattedTimestamp, + FormattedTimestampStyle, Mentionable, + Timestamp, + UserId, }, futures::{ StreamExt, TryStreamExt, }, }; -use tap::Pipe; +use tap::{ + Conv, + Pipe, +}; use timeago::{ Formatter, TimeUnit, @@ -141,8 +151,8 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option<usize>) -> anyhow::Result< .then(|(i, rec)| async move { let mut conn = connection().await?; - let dt = chrono::Utc.from_utc_datetime(&rec.time); - let ago = TIME_FORMATTER.convert((chrono::Utc::now() - dt).to_std().unwrap()); + let dt = chrono::Utc.from_utc_datetime(&rec.time).conv::<Timestamp>(); + let ago = FormattedTimestamp::new(dt, Some(FormattedTimestampStyle::RelativeTime)); let rand = if rec.random { "R, " @@ -157,19 +167,9 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option<usize>) -> anyhow::Result< Err(e) => Err(e), }; - let invoker_name = guild_id - .member(&ctx, rec.user_id as u64) - .await - .map(|m| m.display_name().to_owned()) - .unwrap_or("???".to_owned()); - let result = match meme { Ok((metadata, meme)) => { - let author_name = guild_id - .member(&ctx, metadata.created_by as u64) - .await - .map(|m| m.display_name().to_owned()) - .unwrap_or("???".to_owned()); + let created_date = metadata.created.and_utc().conv::<Timestamp>(); format!( "{}. [{}{}] \"{}\" by {} ({}). invoked by {}.", @@ -177,9 +177,12 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option<usize>) -> anyhow::Result< rand, ago, meme.title, - author_name, - metadata.created.date().format(CLEAN_DATE_FORMAT), - invoker_name + UserId::from(metadata.created_by as u64).mention(), + FormattedTimestamp::new( + created_date, + Some(FormattedTimestampStyle::ShortDate) + ), + UserId::from(rec.user_id as u64).mention() ) }, Err(e) => { @@ -189,7 +192,13 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option<usize>) -> anyhow::Result< } } - format!("{}. [{}{}] not found. invoked by {}.", i + 1, rand, ago, invoker_name) + format!( + "{}. [{}{}] not found. invoked by {}.", + i + 1, + rand, + ago, + UserId::from(rec.user_id as u64).mention() + ) }, }; @@ -208,10 +217,7 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option<usize>) -> anyhow::Result< #[poise::command(prefix_command, guild_only, category = "memes", aliases("stat"))] pub async fn stats(ctx: PoiseContext<'_>) -> anyhow::Result<()> { use db; - use serenity::model::{ - id::UserId, - user::User, - }; + use serenity::model::id::UserId; let guild_id = util::guild_id(ctx)?; @@ -220,31 +226,33 @@ pub async fn stats(ctx: PoiseContext<'_>) -> anyhow::Result<()> { tracing::debug!("reporting stats"); - async fn username(ctx: PoiseContext<'_>, guild_id: GuildId, id: u64) -> anyhow::Result<String> { - let user: User = UserId::new(id).to_user(&ctx).await?; - Ok(user.nick_in(&ctx, guild_id).await.unwrap_or(user.name)) - } - let most_active_day = if let Some(ref most_active_day) = stats.most_active_day { - let fmt = most_active_day.format(CLEAN_DATE_FORMAT); + let fmt = Utc + .from_utc_datetime(&most_active_day.and_time(NaiveTime::default())) + .conv::<Timestamp>(); + let fmt = FormattedTimestamp::new(fmt, Some(FormattedTimestampStyle::LongDate)).to_string(); + let count = stats.most_active_day_count; - format!("the most active day was *{fmt}* with **{count}** memes\n") + format!("the most active day was {fmt} with **{count}** memes\n") } else { String::new() }; let loudest_day = if let Some(ref loudest_day) = stats.most_audio_active_day { - let fmt = loudest_day.format(CLEAN_DATE_FORMAT); + let fmt = + Utc.from_utc_datetime(&loudest_day.and_time(NaiveTime::default())).conv::<Timestamp>(); + let fmt = FormattedTimestamp::new(fmt, Some(FormattedTimestampStyle::LongDate)).to_string(); + let count = stats.most_audio_active_count; - format!("and the loudest day was *{fmt}* with **{count}** audio memes\n") + format!("and the loudest day was {fmt} with **{count}** audio memes\n") } else { String::new() }; let rand_user = if let Some(rand_user) = stats.most_random_meme_user { - let rand_user = username(ctx, guild_id, rand_user).await?; format!( - "**{rand_user}** has invoked the most random memes ({})\n", + "{} has memed randomly the most ({})\n", + UserId::from(rand_user).mention(), stats.most_random_meme_user_count ) } else { @@ -252,9 +260,9 @@ pub async fn stats(ctx: PoiseContext<'_>) -> anyhow::Result<()> { }; let direct_user = if let Some(direct_user) = stats.most_directly_named_meme_user { - let direct_user = username(ctx, guild_id, direct_user).await?; format!( - "**{direct_user}** has invoked the most memes by name ({})\n", + "{} has memed directly the most ({})\n", + UserId::from(direct_user).mention(), stats.most_directly_named_meme_count ) } else { @@ -262,17 +270,21 @@ pub async fn stats(ctx: PoiseContext<'_>) -> anyhow::Result<()> { }; let started_recording = if let Some(ref started_recording) = stats.started_recording { - let fst = started_recording.naive_local().date().format(CLEAN_DATE_FORMAT); - let snd = TIME_FORMATTER.convert((chrono::Utc::now() - started_recording).to_std()?); + let fmt = (*started_recording).conv::<Timestamp>(); + + let fmt_rel = + FormattedTimestamp::new(fmt, Some(FormattedTimestampStyle::RelativeTime)).to_string(); + + let fmt = FormattedTimestamp::new(fmt, Some(FormattedTimestampStyle::LongDate)).to_string(); - format!("started recording meme invocations on *{fst}* ({snd})\n") + format!("started recording meme invocations on {fmt} ({fmt_rel})\n") } else { String::new() }; let most_requested = if let Some(ref most_requested) = stats.most_popular_named_meme { format!( - "*{most_requested}* was the meme specifically requested the most ({})\n", + "*{most_requested}* was specifically requested the most ({})\n", stats.most_popular_named_meme_count ) } else { @@ -281,7 +293,7 @@ pub async fn stats(ctx: PoiseContext<'_>) -> anyhow::Result<()> { let most_random = if let Some(ref most_random) = stats.most_popular_random_meme { format!( - "*{most_random}* was the meme specifically requested the most ({})\n", + "*{most_random}* was specifically requested the most ({})\n", stats.most_popular_random_meme_count ) } else { @@ -339,11 +351,10 @@ pub async fn memers(ctx: PoiseContext<'_>) -> anyhow::Result<()> { .pipe(serenity::futures::stream::iter) .then(|info| async move { let user = UserId::new(info.user_id).to_user(&ctx).await?; - let username = user.nick_in(&ctx, guild_id).await.unwrap_or(user.name); let res = format!( - "**{}**: {} total, {} random, {} specific. favorite meme: *{}* ({})", - username, + "{}: {} total, {} random, {} specific. favorite meme: *{}* ({})", + user.mention(), info.random_memes + info.specific_memes, info.random_memes, info.specific_memes, @@ -432,12 +443,11 @@ pub async fn query(ctx: PoiseContext<'_>, rest: util::RestVec) -> anyhow::Result .pipe(serenity::futures::stream::iter) .then(|(meme, metadata)| async move { let user = UserId::new(metadata.created_by as u64).to_user(&ctx).await?; - let username = user.nick_in(&ctx, guild_id).await.unwrap_or(user.name); Ok(format!( "*{}* by **{}** ({}). text length: **{}**, image: **{}**, audio: **{}**", meme.title, - username, + user.mention(), metadata.created.date().format(CLEAN_DATE_FORMAT), meme.content.map_or(0, |s| s.len()), meme.image_id.map_or("NO", |_s| "YES"), |
