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.rs151
1 files changed, 68 insertions, 83 deletions
diff --git a/src/commands/meme/history.rs b/src/commands/meme/history.rs
index edc75cd..cfd78df 100644
--- a/src/commands/meme/history.rs
+++ b/src/commands/meme/history.rs
@@ -1,4 +1,3 @@
-use anyhow::anyhow;
use diesel::{
result::Error as DieselError,
NotFound,
@@ -11,18 +10,10 @@ use log::{
info,
};
use serenity::{
- framework::standard::{
- macros::command,
- Args,
- CommandError,
- CommandResult,
- },
futures::{
StreamExt,
- TryFutureExt,
TryStreamExt,
},
- model::channel::Message,
prelude::*,
};
use tap::Pipe;
@@ -32,6 +23,7 @@ use timeago::{
};
use crate::{
+ commands::game::get_user_id,
db::{
self,
connection,
@@ -40,6 +32,7 @@ use crate::{
Metadata,
},
util,
+ PoiseContext,
CONFIG,
};
@@ -55,9 +48,14 @@ lazy_static! {
static CLEAN_DATE_FORMAT: &str = "%b %-e %Y";
-#[command]
-#[aliases("what", "hwaet", "hwæt")]
-pub async fn wat(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
+#[poise::command(
+ slash_command,
+ prefix_command,
+ guild_only,
+ category = "memes",
+ aliases("what", "hwaet", "hwæt")
+)]
+pub async fn wat(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
let mut conn = connection().await?;
let record = match InvocationRecord::last(&mut conn).await {
@@ -65,12 +63,12 @@ pub async fn wat(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
Err(e) => {
if let Some(NotFound) = e.downcast_ref::<DieselError>() {
info!("found no memes in history");
- return util::send(ctx, msg.channel_id, "no one has ever memed before", msg.tts)
- .map_err(CommandError::from)
- .await;
+
+ util::reply(ctx, "no one has ever memed before").await?;
+ return Ok(());
}
- util::send(ctx, msg.channel_id, "BAD MEME BAD MEME", msg.tts).await?;
+ util::reply(ctx, "BAD MEME BAD MEME").await?;
return Err(e.into());
},
};
@@ -82,44 +80,41 @@ pub async fn wat(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
let metadata = Metadata::find(&mut conn, meme.metadata_id).await?;
let author = CONFIG.discord.guild().member(&ctx, metadata.created_by as u64).await?;
- util::send(
+ util::reply(
ctx,
- msg.channel_id,
&format!(
"that was \"{}\" by {} ({})",
meme.title,
author.mention(),
metadata.created.date().format(CLEAN_DATE_FORMAT)
),
- msg.tts,
)
- .await?
+ .await?;
},
Err(e) => {
if let Some(NotFound) = e.downcast_ref::<DieselError>() {
info!("last meme not found in database");
- return util::send(ctx, msg.channel_id, "heuueueeeeh?", msg.tts)
- .await
- .map_err(CommandError::from);
+
+ util::reply(ctx, "heuueueeeeh?").await?;
+ return Ok(());
}
- util::send(ctx, msg.channel_id, "do i look like i know what a jpeg is", msg.tts)
- .await?;
+ util::reply(ctx, "do i look like i know what a jpeg is").await?;
return Err(e.into());
},
};
- meme.map(|_| {}).map_err(CommandError::from)
+ let _meme = meme?;
+ Ok(())
}
-#[command]
-#[aliases("hist")]
-pub async fn history(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
- let n = args.single_quoted::<usize>().unwrap_or(CONFIG.default_hist);
-
+#[poise::command(slash_command, prefix_command, guild_only, category = "memes", aliases("hist"))]
+pub async fn history(ctx: PoiseContext<'_>, n: Option<usize>) -> anyhow::Result<()> {
+ let n = n.unwrap_or(CONFIG.default_hist);
+
if n > CONFIG.max_hist {
debug!("user requested more than MAX_HIST ({}) items from history", CONFIG.max_hist);
- util::send(ctx, msg.channel_id, "YER PUSHIN ME OVER THE FUCKIN LINE", true).await?;
+ util::reply(ctx, "YER PUSHIN ME OVER THE FUCKIN LINE").await?;
}
let n = n.min(CONFIG.max_hist);
@@ -131,9 +126,9 @@ pub async fn history(ctx: &Context, msg: &Message, mut args: Args) -> CommandRes
if records.is_empty() {
info!("no memes in history");
- return util::send(ctx, msg.channel_id, "i don't remember anything :(", msg.tts)
- .map_err(CommandError::from)
- .await;
+ util::reply(ctx, "i don't remember anything :(").await?;
+
+ return Ok(());
}
info!("reporting meme history (len {})", n);
@@ -198,19 +193,19 @@ pub async fn history(ctx: &Context, msg: &Message, mut args: Args) -> CommandRes
},
};
- Result::<_, CommandError>::Ok(result)
+ anyhow::Ok(result)
})
.try_collect::<Vec<String>>()
.await?;
let resp = resp.join("\n");
+ util::reply(ctx, resp).await?;
- util::send(ctx, msg.channel_id, &resp, false).await.map_err(CommandError::from)
+ Ok(())
}
-#[command]
-#[aliases("stat")]
-pub async fn stats(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
+#[poise::command(slash_command, prefix_command, guild_only, category = "memes", aliases("stat"))]
+pub async fn stats(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
use db;
use serenity::model::{
id::UserId,
@@ -277,11 +272,12 @@ and *{}* was the most-memed overall ({})"#,
stats.most_popular_meme_overall_count,
);
- util::send(ctx, msg.channel_id, s, msg.tts).map_err(CommandError::from).await
+ util::reply(ctx, s).await?;
+ Ok(())
}
-#[command]
-pub async fn memers(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
+#[poise::command(slash_command, prefix_command, guild_only, category = "memes")]
+pub async fn memers(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
use serenity::model::id::UserId;
let s = db::memers()
@@ -302,25 +298,25 @@ pub async fn memers(ctx: &Context, msg: &Message, _args: Args) -> CommandResult
info.most_used_meme_count,
);
- Result::<_, CommandError>::Ok(res)
+ anyhow::Ok(res)
})
.try_collect::<Vec<String>>()
.await?
.into_iter()
.join("\n");
- util::send(ctx, msg.channel_id, &s, msg.tts).map_err(CommandError::from).await
+ util::reply(ctx, s).await?;
+
+ Ok(())
}
-#[command]
-pub async fn query(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
+#[poise::command(prefix_command, guild_only, category = "memes")]
+pub async fn query(ctx: PoiseContext<'_>, rest: util::RestVec) -> anyhow::Result<()> {
use regex::Regex;
use serenity::model::id::UserId;
- use std::borrow::Borrow;
use crate::{
db,
- game::get_user_id,
CONFIG,
};
@@ -329,42 +325,31 @@ 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 creator: Option<u64> = {
- let guild =
- msg.channel_id.to_channel(&ctx).await?.guild().ok_or(anyhow!("couldn't find guild"))?;
+ let mut rest = rest.into_inner();
- let guild = guild.guild(&ctx).ok_or(anyhow!("couldn't find guild"))?;
+ let creator: Option<u64> = try {
+ let fst = rest.first()?;
+ let captures = CREATOR_REGEX.captures(fst)?;
+ let creator = captures.get(1)?.as_str().to_owned();
- 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(UserId::get))
- } else {
- None
- }
+ let guild = ctx.guild()?;
+ let user_id = get_user_id(&guild, creator).ok()?.get();
+ rest.pop();
+
+ user_id
};
- let order = {
- let order = args.quoted().current().map(|s| AGE_REGEX.is_match(s)).unwrap_or(false);
+ let order: Option<String> = try {
+ let fst = rest.first()?;
+ let captures = AGE_REGEX.captures(fst)?;
+ let order = captures.get(1)?.as_str().to_owned();
- 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())
- })
- .map(|s: String| s.contains("new"))
- .unwrap_or(true)
- } else {
- true
- }
+ order
};
- let iter = db::query_meme(args.rest(), creator, order).await?.into_iter();
+ let order = order.is_some_and(|o| o.contains("new"));
+
+ let iter = db::query_meme(rest.join(" "), creator, order).await?.into_iter();
let result = iter
.pipe(serenity::futures::stream::iter)
@@ -380,7 +365,7 @@ pub async fn query(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
meme.content.map_or(0, |s| s.len()),
meme.image_id.map_or("NO", |_s| "YES"),
meme.audio_id.map_or("NO", |_s| "YES"),
- )) as Result<String, CommandError>
+ )) as anyhow::Result<String>
})
.try_collect::<Vec<String>>()
.await;
@@ -401,10 +386,10 @@ pub async fn query(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
if result.is_empty() {
info!("no memes matched query");
- return util::send(ctx, msg.channel_id, "no match".to_owned(), msg.tts)
- .map_err(CommandError::from)
- .await;
+ util::reply(ctx, "no match").await?;
+ return Ok(());
}
- util::send(ctx, msg.channel_id, &result, msg.tts).map_err(CommandError::from).await
+ util::reply(ctx, result).await?;
+ Ok(())
}