diff options
Diffstat (limited to 'src/commands/meme/history.rs')
| -rw-r--r-- | src/commands/meme/history.rs | 178 |
1 files changed, 117 insertions, 61 deletions
diff --git a/src/commands/meme/history.rs b/src/commands/meme/history.rs index d6ccdcf..1acb019 100644 --- a/src/commands/meme/history.rs +++ b/src/commands/meme/history.rs @@ -1,3 +1,16 @@ +use crate::{ + commands::game::get_user_id, + db::{ + self, + connection, + InvocationRecord, + Meme, + Metadata, + }, + util, + PoiseContext, + CONFIG, +}; use chrono::TimeZone; use diesel::{ result::Error as DieselError, @@ -7,31 +20,21 @@ use grate::tracing; use itertools::Itertools; use lazy_static::lazy_static; use serenity::{ + all::{ + GuildId, + Mentionable, + }, futures::{ StreamExt, TryStreamExt, }, - prelude::*, }; use tap::Pipe; use timeago::{ Formatter, TimeUnit, }; - -use crate::{ - commands::game::get_user_id, - db::{ - self, - connection, - InvocationRecord, - Meme, - Metadata, - }, - util, - PoiseContext, - CONFIG, -}; +use windows::core::s; lazy_static! { static ref TIME_FORMATTER: Formatter = { @@ -48,9 +51,11 @@ static CLEAN_DATE_FORMAT: &str = "%b %-e %Y"; /// Print info about the last meme. #[poise::command(prefix_command, guild_only, category = "memes", aliases("what", "hwaet", "hwæt"))] pub async fn wat(ctx: PoiseContext<'_>) -> anyhow::Result<()> { + let guild_id = util::guild_id(ctx)?; + let mut conn = connection().await?; - let record = match InvocationRecord::last(&mut conn).await { + let record = match InvocationRecord::last(&mut conn, guild_id.get()).await { Ok(x) => x, Err(e) => { if let Some(NotFound) = e.downcast_ref::<DieselError>() { @@ -70,7 +75,7 @@ pub async fn wat(ctx: PoiseContext<'_>) -> anyhow::Result<()> { match meme { Ok(ref meme) => { let metadata = Metadata::find(&mut conn, meme.metadata_id).await?; - let author = CONFIG.discord.guild().member(&ctx, metadata.created_by as u64).await?; + let author = guild_id.member(&ctx, metadata.created_by as u64).await?; util::reply( ctx, @@ -116,9 +121,11 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option<usize>) -> anyhow::Result< let n = n.min(CONFIG.max_hist); + let guild_id = util::guild_id(ctx)?; + let records = { let mut conn = connection().await?; - InvocationRecord::last_n(&mut conn, n).await? + InvocationRecord::last_n(&mut conn, n, guild_id.get()).await? }; if records.is_empty() { @@ -150,9 +157,7 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option<usize>) -> anyhow::Result< Err(e) => Err(e), }; - let invoker_name = CONFIG - .discord - .guild() + let invoker_name = guild_id .member(&ctx, rec.user_id as u64) .await .map(|m| m.display_name().to_owned()) @@ -160,9 +165,7 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option<usize>) -> anyhow::Result< let result = match meme { Ok((metadata, meme)) => { - let author_name = CONFIG - .discord - .guild() + let author_name = guild_id .member(&ctx, metadata.created_by as u64) .await .map(|m| m.display_name().to_owned()) @@ -210,17 +213,89 @@ pub async fn stats(ctx: PoiseContext<'_>) -> anyhow::Result<()> { user::User, }; + let guild_id = util::guild_id(ctx)?; + let mut conn = connection().await?; - let stats = db::stats(&mut conn).await?; + let stats = db::stats(&mut conn, guild_id.get()).await?; tracing::debug!("reporting stats"); - let rand_user: User = UserId::new(stats.most_random_meme_user).to_user(&ctx).await?; - let direct_user: User = UserId::new(stats.most_directly_named_meme_user).to_user(&ctx).await?; + 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 count = stats.most_active_day_count; + + 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 count = stats.most_audio_active_count; + 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", + stats.most_random_meme_user_count + ) + } else { + String::new() + }; + + 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", + stats.most_directly_named_meme_count + ) + } else { + String::new() + }; + + 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 rand_user = rand_user.nick_in(&ctx, CONFIG.discord.guild()).await.unwrap_or(rand_user.name); - let direct_user = - direct_user.nick_in(&ctx, CONFIG.discord.guild()).await.unwrap_or(direct_user.name); + format!("started recording meme invocations on *{fst}* ({snd})\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", + stats.most_popular_named_meme_count + ) + } else { + String::new() + }; + + 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", + stats.most_popular_random_meme_count + ) + } else { + String::new() + }; + + let most_overall = if let Some(ref most_overall) = stats.most_popular_meme_overall { + format!( + "*{most_overall}* was the most-memed overall({})\n", + stats.most_popular_meme_overall_count + ) + } else { + String::new() + }; let s = format!( r#" @@ -228,46 +303,23 @@ pub async fn stats(ctx: PoiseContext<'_>) -> anyhow::Result<()> { **{}** memes with audio ({:0.1}%) **{}** memes with images ({:0.1}%) -started recording meme invocations on *{}* ({}) -**{}** total meme invocations recorded +{started_recording}**{}** total meme invocations recorded **{}** of which were random ({:0.1}%) and **{}** were audio ({:0.1}%) -the most active day was *{}* with **{}** memes -and the loudest day was *{}* with **{}** audio memes - -**{}** has invoked the most random memes ({}) -**{}** has invoked the most memes by name ({}) - -*{}* was the meme specifically requested the most ({}) -*{}* was the meme randomly invoked the most ({}) -and *{}* was the most-memed overall ({})"#, +{most_active_day}{loudest_day} +{rand_user}{direct_user} +{most_requested}{most_random}{most_overall}"#, stats.memes_overall, stats.audio_memes, (stats.audio_memes as f64) / (stats.memes_overall as f64) * 100., stats.image_memes, (stats.image_memes as f64) / (stats.memes_overall as f64) * 100., - stats.started_recording.naive_local().date().format(CLEAN_DATE_FORMAT), - TIME_FORMATTER.convert((chrono::Utc::now() - stats.started_recording).to_std().unwrap()), stats.total_meme_invocations, stats.random_meme_invocations, (stats.random_meme_invocations as f64) / (stats.total_meme_invocations as f64) * 100., stats.audio_meme_invocations, (stats.audio_meme_invocations as f64) / (stats.total_meme_invocations as f64) * 100., - stats.most_active_day.format(CLEAN_DATE_FORMAT), - stats.most_active_day_count, - stats.most_audio_active_day.format(CLEAN_DATE_FORMAT), - stats.most_audio_active_count, - rand_user, - stats.most_random_meme_user_count, - direct_user, - stats.most_directly_named_meme_count, - stats.most_popular_named_meme, - stats.most_popular_named_meme_count, - stats.most_popular_random_meme, - stats.most_popular_random_meme_count, - stats.most_popular_meme_overall, - stats.most_popular_meme_overall_count, ); util::reply(ctx, s).await?; @@ -279,13 +331,15 @@ and *{}* was the most-memed overall ({})"#, pub async fn memers(ctx: PoiseContext<'_>) -> anyhow::Result<()> { use serenity::model::id::UserId; - let s = db::memers() + let guild_id = util::guild_id(ctx)?; + + let s = db::memers(guild_id.get()) .await? .into_iter() .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, CONFIG.discord.guild()).await.unwrap_or(user.name); + let username = user.nick_in(&ctx, guild_id).await.unwrap_or(user.name); let res = format!( "**{}**: {} total, {} random, {} specific. favorite meme: *{}* ({})", @@ -349,6 +403,8 @@ pub async fn query(ctx: PoiseContext<'_>, rest: util::RestVec) -> anyhow::Result static ref AGE_REGEX: Regex = Regex::new(r"(?i)(?:age|order)=(.*)").unwrap(); } + let guild_id = util::guild_id(ctx)?; + let mut rest = rest.into_inner(); let creator: Option<u64> = try { @@ -373,13 +429,13 @@ pub async fn query(ctx: PoiseContext<'_>, rest: util::RestVec) -> anyhow::Result let order = order.is_some_and(|o| o.contains("new")); - let iter = db::query_meme(rest.join(" "), creator, order).await?.into_iter(); + let iter = db::query_meme(rest.join(" "), creator, order, guild_id.get()).await?.into_iter(); let result = iter .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, CONFIG.discord.guild()).await.unwrap_or(user.name); + let username = user.nick_in(&ctx, guild_id).await.unwrap_or(user.name); Ok(format!( "*{}* by **{}** ({}). text length: **{}**, image: **{}**, audio: **{}**", |
