diff options
Diffstat (limited to 'src/commands/meme/invoke.rs')
| -rw-r--r-- | src/commands/meme/invoke.rs | 81 |
1 files changed, 53 insertions, 28 deletions
diff --git a/src/commands/meme/invoke.rs b/src/commands/meme/invoke.rs index de0272f..03c6251 100644 --- a/src/commands/meme/invoke.rs +++ b/src/commands/meme/invoke.rs @@ -1,63 +1,65 @@ use diesel::{ - NotFound, result::Error as DieselError, + NotFound, }; use itertools::Itertools; use log::info; use serenity::{ framework::standard::{ - Args, macros::command, + Args, + CommandError, + CommandResult, }, + futures::TryFutureExt, model::channel::Message, prelude::*, }; use crate::{ commands::meme::send_meme, - Result, db::{ self, connection, find_meme, InvocationRecord, }, - util::CtxExt, + util, }; #[command] #[aliases("mem")] -pub fn meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { +pub async fn meme(ctx: &Context, msg: &Message, args: Args) -> CommandResult { _meme(ctx, msg, args, AudioPlayback::Optional) } #[command] -pub fn omen(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { +pub async fn omen(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let args = Args::new("", &[]); _meme(ctx, msg, args, AudioPlayback::Optional) } #[command] -pub fn silentomen(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { +pub async fn silentomen(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let args = Args::new("", &[]); _meme(ctx, msg, args, AudioPlayback::Prohibited) } #[command] -pub fn audioomen(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { +pub async fn audioomen(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let args = Args::new("", &[]); _meme(ctx, msg, args, AudioPlayback::Required) } #[command] #[aliases("audiomeme", "audiomem")] -pub fn audio_meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { +pub async fn audio_meme(ctx: &Context, msg: &Message, args: Args) -> CommandResult { _meme(ctx, msg, args, AudioPlayback::Required) } #[command] #[aliases("silentmeme", "silentmem")] -pub fn silent_meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { +pub async fn silent_meme(ctx: &Context, msg: &Message, args: Args) -> CommandResult { _meme(ctx, msg, args, AudioPlayback::Prohibited) } @@ -68,9 +70,14 @@ enum AudioPlayback { Prohibited, } -fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_playback: AudioPlayback) -> Result<()> { +async fn _meme( + ctx: &Context, + msg: &Message, + args: Args, + audio_playback: AudioPlayback, +) -> CommandResult { if args.len() == 0 || audio_playback != AudioPlayback::Optional { - return rand_meme(ctx, msg, audio_playback); + return rand_meme(ctx, msg, audio_playback).await; } let search = args.raw().join(" "); @@ -78,28 +85,34 @@ fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_playback: AudioPlay let mut conn = connection()?; let mem = match find_meme(&mut conn, search) { Ok(x) => { - InvocationRecord::create(&mut conn, msg.author.id.0, msg.id.0, x.id, false)?; + InvocationRecord::create(&mut conn, msg.author.id.get(), msg.id.get(), x.id, false)?; x }, Err(e) => { return if let Some(NotFound) = e.downcast_ref::<DieselError>() { info!("requested meme not found in database"); - ctx.send(msg.channel_id, "c'mon baby, guesstimate", msg.tts) + util::send(ctx, msg.channel_id, "c'mon baby, guesstimate", msg.tts) + .await + .map_err(CommandError::from) } else { - ctx.send(msg.channel_id, "what in ryan's name", msg.tts)?; - Err(e) + util::send(ctx, msg.channel_id, "what in ryan's name", msg.tts).await?; + Err(e.into()) }; }, }; - send_meme(ctx, &mem, &mut conn, msg) + send_meme(ctx, &mem, &mut conn, msg).await } -fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> Result<()> { +async fn rand_meme( + ctx: &Context, + message: &Message, + audio_playback: AudioPlayback, +) -> CommandResult { let mut conn = connection()?; - let should_audio = ctx.users_listening()?; + let should_audio = util::users_listening(ctx).await?; let mem = match audio_playback { AudioPlayback::Required => db::rand_audio_meme(&mut conn), @@ -109,28 +122,36 @@ fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> match mem { Ok(mem) => { - InvocationRecord::create(&mut conn, message.author.id.0, message.id.0, mem.id, true)?; - send_meme(ctx, &mem, &mut conn, message)?; + InvocationRecord::create( + &mut conn, + message.author.id.get(), + message.id.get(), + mem.id, + true, + )?; + send_meme(ctx, &mem, &mut conn, message).await?; Ok(()) }, Err(e) => { match e.downcast_ref::<DieselError>() { Some(NotFound) => { info!("random meme not found"); - return ctx.send(message.channel_id, "i don't know any :(", message.tts) + return util::send(ctx, message.channel_id, "i don't know any :(", message.tts) + .map_err(CommandError::from) + .await; }, _ => {}, } - ctx.send(message.channel_id, "HELP", message.tts)?; - return Err(e); + util::send(ctx, message.channel_id, "HELP", message.tts).await?; + return Err(e.into()); }, } } #[command] #[aliases("rarememe", "raremem")] -pub fn rare_meme(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { +pub async fn rare_meme(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let should_audio = ctx.users_listening()?; let mut conn = connection()?; @@ -138,19 +159,23 @@ pub fn rare_meme(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { let meme = db::rare_meme(&mut conn, should_audio); match meme { Ok(meme) => { - InvocationRecord::create(&mut conn, msg.author.id.0, msg.id.0, meme.id, true)?; + InvocationRecord::create(&mut conn, msg.author.id.get(), msg.id.get(), meme.id, true)?; send_meme(ctx, &meme, &mut conn, msg) }, Err(e) => { match e.downcast_ref::<DieselError>() { Some(NotFound) => { info!("rare meme not found"); - return ctx.send(msg.channel_id, "i don't know any :(", msg.tts) + return util::send(ctx, msg.channel_id, "i don't know any :(", msg.tts) + .map_err(CommandError::from) + .await; }, _ => {}, } - ctx.send(msg.channel_id, "THE MEME MARKET IS IN FREEFALL", msg.tts)?; + util::send(ctx, msg.channel_id, "THE MEME MARKET IS IN FREEFALL", msg.tts) + .map_err(CommandError::from) + .await?; Err(e) }, |
