aboutsummaryrefslogtreecommitdiff
path: root/src/commands/meme/history.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/meme/history.rs')
-rw-r--r--src/commands/meme/history.rs124
1 files changed, 60 insertions, 64 deletions
diff --git a/src/commands/meme/history.rs b/src/commands/meme/history.rs
index ed50e27..33a2de2 100644
--- a/src/commands/meme/history.rs
+++ b/src/commands/meme/history.rs
@@ -2,7 +2,6 @@ use anyhow::anyhow;
use diesel::{
result::Error as DieselError,
NotFound,
- PgConnection,
};
use itertools::Itertools;
use lazy_static::lazy_static;
@@ -115,8 +114,6 @@ pub async fn wat(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
#[command]
pub async fn history(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
- let mut conn = connection()?;
-
let n = args.single_quoted::<usize>().unwrap_or(CONFIG.default_hist);
if n > CONFIG.max_hist {
@@ -126,7 +123,10 @@ pub async fn history(ctx: &Context, msg: &Message, mut args: Args) -> CommandRes
let n = n.min(CONFIG.max_hist);
- let records = InvocationRecord::last_n(&mut conn, n)?;
+ let records = {
+ let mut conn = connection()?;
+ InvocationRecord::last_n(&mut conn, n)?
+ };
if records.len() == 0 {
info!("no memes in history");
@@ -138,74 +138,70 @@ pub async fn history(ctx: &Context, msg: &Message, mut args: Args) -> CommandRes
info!("reporting meme history (len {})", n);
let resp = serenity::futures::stream::iter(records.into_iter().enumerate().rev())
- .then(|(i, rec)| ir_info(ctx, i, rec, &mut conn))
- .try_collect::<Vec<String>>()
- .await?;
-
- let resp = resp.join("\n");
-
- util::send(ctx, msg.channel_id, &resp, false).await.map_err(CommandError::from)
-}
+ .then(|(i, rec)| async move {
+ let mut conn = connection()?;
-async fn ir_info(
- ctx: &Context,
- i: usize,
- rec: InvocationRecord,
- conn: &mut PgConnection,
-) -> Result<String, CommandError> {
- 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 dt = chrono::DateTime::from_utc(rec.time, chrono::Utc {});
+ let ago = TIME_FORMATTER.convert((chrono::Utc::now() - dt).to_std().unwrap());
- let meme = Meme::find(conn, rec.meme_id)
- .and_then(|meme| Metadata::find(conn, meme.metadata_id).map(|metadata| (metadata, meme)));
+ let rand = if rec.random {
+ "R, "
+ } else {
+ ""
+ };
- let invoker_name = CONFIG
- .discord
- .guild()
- .member(&ctx, rec.user_id as u64)
- .await
- .map(|m| m.display_name().to_owned())
- .unwrap_or("???".to_owned());
+ let meme = Meme::find(&mut conn, rec.meme_id).and_then(|meme| {
+ Metadata::find(&mut conn, meme.metadata_id).map(|metadata| (metadata, meme))
+ });
- let result = match meme {
- Ok((metadata, meme)) => {
- let author_name = CONFIG
+ let invoker_name = CONFIG
.discord
.guild()
- .member(&ctx, metadata.created_by as u64)
+ .member(&ctx, rec.user_id as u64)
.await
.map(|m| m.display_name().to_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
- )
- },
- Err(e) => {
- if let Some(variant) = e.downcast_ref::<DieselError>() {
- if *variant != NotFound {
- error!("error encountered loading meme history: {}", e);
- }
- }
+ let result = match meme {
+ Ok((metadata, meme)) => {
+ let author_name = CONFIG
+ .discord
+ .guild()
+ .member(&ctx, metadata.created_by as u64)
+ .await
+ .map(|m| m.display_name().to_owned())
+ .unwrap_or("???".to_owned());
- format!("{}. [{}{}] not found. invoked by {}.", i + 1, rand, ago, invoker_name)
- },
- };
+ format!(
+ "{}. [{}{}] \"{}\" by {} ({}). invoked by {}.",
+ i + 1,
+ rand,
+ ago,
+ meme.title,
+ author_name,
+ metadata.created.date().format(CLEAN_DATE_FORMAT),
+ invoker_name
+ )
+ },
+ Err(e) => {
+ if let Some(variant) = e.downcast_ref::<DieselError>() {
+ if *variant != NotFound {
+ error!("error encountered loading meme history: {}", e);
+ }
+ }
+
+ format!("{}. [{}{}] not found. invoked by {}.", i + 1, rand, ago, invoker_name)
+ },
+ };
- Ok(result)
+ Result::<_, CommandError>::Ok(result)
+ })
+ .try_collect::<Vec<String>>()
+ .await?;
+
+ let resp = resp.join("\n");
+
+ util::send(ctx, msg.channel_id, &resp, false).await.map_err(CommandError::from)
}
#[command]
@@ -328,12 +324,12 @@ pub async fn query(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
static ref AGE_REGEX: Regex = Regex::new(r"(?i)(?:age|order)=(.*)").unwrap();
}
- let guild =
- msg.channel_id.to_channel(&ctx).await?.guild().ok_or(anyhow!("couldn't find guild"))?;
+ let creator: Option<u64> = {
+ let guild =
+ msg.channel_id.to_channel(&ctx).await?.guild().ok_or(anyhow!("couldn't find guild"))?;
- let guild = guild.guild(&ctx).ok_or(anyhow!("couldn't find guild"))?;
+ let guild = guild.guild(&ctx).ok_or(anyhow!("couldn't find guild"))?;
- 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>()