diff options
| author | Nathan Perry <np@nathanperry.dev> | 2024-05-08 10:28:04 -0400 |
|---|---|---|
| committer | Nathan Perry <np@nathanperry.dev> | 2024-05-08 14:16:01 -0400 |
| commit | fe467f60d99efa54f2ef64606e7d39b9b06d7294 (patch) | |
| tree | a62bb50fedb1959d1a155878f0ff0ab7b1f699b6 /src/commands/meme/history.rs | |
| parent | 48aa684dece2696e21fd871eb6f3825f28fe0200 (diff) | |
update all deps
Diffstat (limited to 'src/commands/meme/history.rs')
| -rw-r--r-- | src/commands/meme/history.rs | 188 |
1 files changed, 123 insertions, 65 deletions
diff --git a/src/commands/meme/history.rs b/src/commands/meme/history.rs index f9e8851..5e200b1 100644 --- a/src/commands/meme/history.rs +++ b/src/commands/meme/history.rs @@ -1,6 +1,6 @@ use diesel::{ - NotFound, result::Error as DieselError, + NotFound, }; use log::{ debug, @@ -9,8 +9,8 @@ use log::{ }; use serenity::{ framework::standard::{ - Args, macros::command, + Args, }, model::channel::Message, prelude::*, @@ -22,6 +22,13 @@ use timeago::{ use anyhow::anyhow; use lazy_static::lazy_static; +use serenity::{ + framework::standard::{ + CommandError, + CommandResult, + }, + futures::TryFutureExt, +}; use crate::{ db::{ @@ -31,9 +38,9 @@ use crate::{ Meme, Metadata, }, - CONFIG, + util, Result, - util::CtxExt, + CONFIG, }; lazy_static! { @@ -50,7 +57,7 @@ static CLEAN_DATE_FORMAT: &'static str = "%b %-e %Y"; #[command] #[aliases("what")] -pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { +pub async fn wat(ctx: &Context, msg: &Message, _: Args) -> CommandResult { let mut conn = connection()?; let record = match InvocationRecord::last(&mut conn) { @@ -58,11 +65,13 @@ pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { Err(e) => { if let Some(NotFound) = e.downcast_ref::<DieselError>() { info!("found no memes in history"); - return ctx.send(msg.channel_id, "no one has ever memed before", msg.tts); + return util::send(ctx, msg.channel_id, "no one has ever memed before", msg.tts) + .map_err(CommandError::from) + .await; } - ctx.send(msg.channel_id, "BAD MEME BAD MEME", msg.tts)?; - return Err(e); + util::send(ctx, msg.channel_id, "BAD MEME BAD MEME", msg.tts).await?; + return Err(e.into()); }, }; @@ -73,17 +82,27 @@ pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { let metadata = Metadata::find(&mut conn, meme.metadata_id)?; let author = CONFIG.discord.guild().member(&ctx, metadata.created_by as u64)?; - ctx.send(msg.channel_id, - &format!("that was \"{}\" by {} ({})", - meme.title, author.mention(), metadata.created.date().format(CLEAN_DATE_FORMAT)), msg.tts)? + util::send( + ctx, + msg.channel_id, + &format!( + "that was \"{}\" by {} ({})", + meme.title, + author.mention(), + metadata.created.date().format(CLEAN_DATE_FORMAT) + ), + msg.tts, + ) + .await? }, Err(e) => { if let Some(NotFound) = e.downcast_ref::<DieselError>() { info!("last meme not found in database"); - return ctx.send(msg.channel_id, "heuueueeeeh?", msg.tts); + return util::send(ctx, msg.channel_id, "heuueueeeeh?", msg.tts).await; } - ctx.send(msg.channel_id, "do i look like i know what a jpeg is", msg.tts)?; + util::send(ctx, msg.channel_id, "do i look like i know what a jpeg is", msg.tts) + .await?; return Err(e); }, }; @@ -92,7 +111,7 @@ pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { } #[command] -pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { +pub async fn history(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { use itertools::Itertools; let mut conn = connection()?; @@ -101,7 +120,7 @@ pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { if n > CONFIG.max_hist { debug!("user requested more than MAX_HIST ({}) items from history", CONFIG.max_hist); - ctx.send(msg.channel_id, "YER PUSHIN ME OVER THE FUCKIN LINE", true)?; + util::send(ctx, msg.channel_id, "YER PUSHIN ME OVER THE FUCKIN LINE", true).await?; } let n = n.min(CONFIG.max_hist); @@ -110,7 +129,9 @@ pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { if records.len() == 0 { info!("no memes in history"); - return ctx.send(msg.channel_id, "i don't remember anything :(", msg.tts); + return util::send(ctx, msg.channel_id, "i don't remember anything :(", msg.tts) + .map_err(CommandError::from) + .await; } info!("reporting meme history (len {})", n); @@ -119,18 +140,41 @@ pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { .enumerate() .rev() .map(|(i, rec)| { - let dt = chrono::DateTime::from_utc(rec.time, chrono::Utc{}); + let dt = chrono::DateTime::from_utc(rec.time, chrono::Utc {}); let ago = TIME_FORMATTER.convert((chrono::Utc::now() - dt).to_std().unwrap()); - let rand = if rec.random { "R, " } else { "" }; + let rand = if rec.random { + "R, " + } else { + "" + }; Meme::find(&mut conn, rec.meme_id) .and_then(|meme| { Metadata::find(&mut conn, meme.metadata_id).map(|metadata| (metadata, meme)) }) .map(|(metadata, meme)| { - let author_name = CONFIG.discord.guild().member(&ctx, metadata.created_by as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); - let invoker_name = CONFIG.discord.guild().member(&ctx, rec.user_id as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); - format!("{}. [{}{}] \"{}\" by {} ({}). invoked by {}.", i + 1, rand, ago, meme.title, author_name, metadata.created.date().format(CLEAN_DATE_FORMAT), invoker_name) + let author_name = CONFIG + .discord + .guild() + .member(&ctx, metadata.created_by as u64) + .map(|m| m.display_name().into_owned()) + .unwrap_or("???".to_owned()); + let invoker_name = CONFIG + .discord + .guild() + .member(&ctx, rec.user_id as u64) + .map(|m| m.display_name().into_owned()) + .unwrap_or("???".to_owned()); + format!( + "{}. [{}{}] \"{}\" by {} ({}). invoked by {}.", + i + 1, + rand, + ago, + meme.title, + author_name, + metadata.created.date().format(CLEAN_DATE_FORMAT), + invoker_name + ) }) .unwrap_or_else(|e| { if let Some(variant) = e.downcast_ref::<DieselError>() { @@ -139,18 +183,23 @@ pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { } } - let invoker_name = CONFIG.discord.guild().member(&ctx, rec.user_id as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); + let invoker_name = CONFIG + .discord + .guild() + .member(&ctx, rec.user_id as u64) + .map(|m| m.display_name().into_owned()) + .unwrap_or("???".to_owned()); format!("{}. [{}{}] not found. invoked by {}.", i + 1, rand, ago, invoker_name) }) }) .join("\n"); - ctx.send(msg.channel_id, &resp, false) + util::send(ctx, msg.channel_id, &resp, false).await } #[command] #[aliases("stat")] -pub fn stats(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { +pub async fn stats(ctx: &Context, msg: &Message, _: Args) -> CommandResult { use db; use serenity::model::{ id::UserId, @@ -162,8 +211,8 @@ pub fn stats(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { debug!("reporting stats"); - let rand_user: User = UserId(stats.most_random_meme_user).to_user(&ctx)?; - let direct_user: User = UserId(stats.most_directly_named_meme_user).to_user(&ctx)?; + let rand_user: User = UserId::new(stats.most_random_meme_user).to_user(&ctx)?; + let direct_user: User = UserId::new(stats.most_directly_named_meme_user).to_user(&ctx)?; let rand_user = rand_user.nick_in(&ctx, CONFIG.discord.guild()).unwrap_or(rand_user.name); let direct_user = direct_user.nick_in(&ctx, CONFIG.discord.guild()).unwrap_or(direct_user.name); @@ -200,29 +249,35 @@ and *{}* was the most-memed overall ({})"#, (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, + 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, ); - ctx.send(msg.channel_id, s, msg.tts) + + util::send(ctx, msg.channel_id, s, msg.tts).map_err(CommandError::from).await } #[command] -pub fn memers(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { +pub async fn memers(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { use db; use itertools::Itertools; - use serenity::model::{ - id::UserId, - }; + use serenity::model::id::UserId; let s = db::memers()? .into_iter() .map(|info| { - let user = UserId(info.user_id).to_user(&ctx)?; + let user = UserId::new(info.user_id).to_user(&ctx)?; let username = user.nick_in(&ctx, CONFIG.discord.guild()).unwrap_or(user.name); let res = format!( @@ -241,11 +296,11 @@ pub fn memers(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { .into_iter() .join("\n"); - ctx.send(msg.channel_id, &s, msg.tts) + util::send(ctx, msg.channel_id, &s, msg.tts).map_err(CommandError::from).await } #[command] -pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { +pub async fn query(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { use std::borrow::Borrow; use itertools::Itertools; @@ -253,8 +308,8 @@ pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { use serenity::model::id::UserId; use crate::{ - game::get_user_id, db, + game::get_user_id, CONFIG, }; @@ -263,24 +318,21 @@ pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { static ref AGE_REGEX: Regex = Regex::new(r"(?i)(?:age|order)=(.*)").unwrap(); } - let guild = msg.channel_id.to_channel(&ctx)? - .guild() - .ok_or(anyhow!("couldn't find guild"))?; + let guild = msg.channel_id.to_channel(&ctx)?.guild().ok_or(anyhow!("couldn't find guild"))?; - let guild = guild.read() - .guild(&ctx) - .ok_or(anyhow!("couldn't find guild"))?; + let guild = guild.read().guild(&ctx).ok_or(anyhow!("couldn't find guild"))?; - let guild = guild - .read(); + let guild = guild.read(); let creator: Option<u64> = { let creator = args.quoted().current().map(|s| CREATOR_REGEX.is_match(s)).unwrap_or(false); if creator { args.single_quoted::<String>() .ok() - .and_then(|s| CREATOR_REGEX.captures(&s).and_then(|c| c.get(1)).map(|x| x.as_str().to_owned())) - .and_then(|s| get_user_id(guild.borrow(), s).ok().map(|s| s.0)) + .and_then(|s| { + CREATOR_REGEX.captures(&s).and_then(|c| c.get(1)).map(|x| x.as_str().to_owned()) + }) + .and_then(|s| get_user_id(guild.borrow(), s).ok().map(UserId::get)) } else { None } @@ -290,8 +342,11 @@ pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { let order = args.quoted().current().map(|s| AGE_REGEX.is_match(s)).unwrap_or(false); if order { - args.single_quoted::<String>().ok() - .and_then(|s| AGE_REGEX.captures(&s).and_then(|c| c.get(1)).map(|x| x.as_str().to_owned())) + args.single_quoted::<String>() + .ok() + .and_then(|s| { + AGE_REGEX.captures(&s).and_then(|c| c.get(1)).map(|x| x.as_str().to_owned()) + }) .map(|s: String| s.contains("new")) .unwrap_or(true) } else { @@ -302,16 +357,17 @@ pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { let result = db::query_meme(args.rest(), creator, order)? .into_iter() .map(|(meme, metadata)| { - let user = UserId(metadata.created_by as u64).to_user(&ctx)?; + let user = UserId::new(metadata.created_by as u64).to_user(&ctx)?; let username = user.nick_in(&ctx, CONFIG.discord.guild()).unwrap_or(user.name); - Ok(format!("*{}* by **{}** ({}). text length: **{}**, image: **{}**, audio: **{}**", - meme.title, - username, - metadata.created.date().format(CLEAN_DATE_FORMAT), - meme.content.map_or(0, |s| s.len()), - meme.image_id.map_or("NO", |_s| "YES"), - meme.audio_id.map_or("NO", |_s| "YES"), + Ok(format!( + "*{}* by **{}** ({}). text length: **{}**, image: **{}**, audio: **{}**", + meme.title, + username, + metadata.created.date().format(CLEAN_DATE_FORMAT), + meme.content.map_or(0, |s| s.len()), + meme.image_id.map_or("NO", |_s| "YES"), + meme.audio_id.map_or("NO", |_s| "YES"), )) }) .collect::<Result<Vec<_>>>()? @@ -329,9 +385,11 @@ pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { if result.len() == 0 { info!("no memes matched query"); - return ctx.send(msg.channel_id, "no match".to_owned(), msg.tts); - } + return util::send(ctx, msg.channel_id, "no match".to_owned(), msg.tts) + .map_err(CommandError::from) + .await; + } - ctx.send(msg.channel_id, &result, msg.tts) + util::send(ctx, msg.channel_id, &result, msg.tts).map_err(CommandError::from).await } |
