diff options
| author | Nathan Perry <avaglir@gmail.com> | 2019-03-30 05:23:54 -0400 |
|---|---|---|
| committer | Nathan Perry <avaglir@gmail.com> | 2019-03-30 05:23:54 -0400 |
| commit | e7e2daa560af199bd005a8526ebcf7ff4441e3ea (patch) | |
| tree | b54be3956efa77ed729e0940e82b258798062a58 /src/commands | |
| parent | a6f5334f984531cf2304b6333b138944517d3703 (diff) | |
add rarememe command
Diffstat (limited to 'src/commands')
| -rw-r--r-- | src/commands/meme/invoke.rs | 42 | ||||
| -rw-r--r-- | src/commands/meme/mod.rs | 32 | ||||
| -rw-r--r-- | src/commands/mod.rs | 6 |
3 files changed, 43 insertions, 37 deletions
diff --git a/src/commands/meme/invoke.rs b/src/commands/meme/invoke.rs index 8deecb5..0f992c2 100644 --- a/src/commands/meme/invoke.rs +++ b/src/commands/meme/invoke.rs @@ -1,4 +1,7 @@ -use diesel::result::Error as DieselError; +use diesel::{ + NotFound, + result::Error as DieselError, +}; use failure::Error; use serenity::{ framework::standard::Args, @@ -13,12 +16,10 @@ use crate::{ send, }, db::{ + self, connection, find_meme, InvocationRecord, - rand_audio_meme as db_rand_audio_meme, - rand_meme as db_rand_meme, - rand_silent_meme as db_rand_silent_meme, }, Result, }; @@ -79,9 +80,9 @@ fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> let should_audio = ctx.users_listening()?; let mem = match audio_playback { - AudioPlayback::Required => db_rand_audio_meme(&conn), - AudioPlayback::Optional => db_rand_meme(&conn, should_audio), - AudioPlayback::Prohibited => db_rand_silent_meme(&conn), + AudioPlayback::Required => db::rand_audio_meme(&conn), + AudioPlayback::Optional => db::rand_meme(&conn, should_audio), + AudioPlayback::Prohibited => db::rand_silent_meme(&conn), }; match mem { @@ -103,3 +104,30 @@ fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> }, } } + +pub fn rare_meme(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { + let should_audio = ctx.users_listening()?; + + let conn = connection()?; + let meme = db::rare_meme(&conn, should_audio); + + match meme { + Ok(meme) => { + InvocationRecord::create(&conn, msg.author.id.0, msg.id.0, meme.id, true)?; + send_meme(ctx, &meme, &conn, msg) + }, + Err(e) => { + match e.downcast_ref::<DieselError>() { + Some(NotFound) => { + info!("rare meme not found"); + return send(msg.channel_id, "i don't know any :(", msg.tts) + }, + _ => {}, + } + + send(msg.channel_id, "THE MEME MARKET IS IN FREEFALL", msg.tts)?; + + Err(e) + }, + } +} diff --git a/src/commands/meme/mod.rs b/src/commands/meme/mod.rs index d29a025..ce1cb48 100644 --- a/src/commands/meme/mod.rs +++ b/src/commands/meme/mod.rs @@ -1,46 +1,18 @@ -use std::{ - io::Read, - process::{ - Command, - Stdio, - }, -}; - -use diesel::{ - NotFound, - PgConnection, - result::Error as DieselError, -}; -use failure::Error; +use diesel::PgConnection; use rand::{Rng, thread_rng}; use serenity::{ builder::CreateMessage, - framework::standard::Args, http::AttachmentType, model::channel::Message, prelude::*, }; -use url::Url; use crate::{ audio::{ - CtxExt, - parse_times, PlayArgs, PlayQueue, - ytdl_url, - }, - commands::send, - db::{ - Audio, - connection, - delete_meme, - find_meme, - Image, - InvocationRecord, - Meme, - NewMeme, }, + db::Meme, Result, }; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6596a32..74e6e3f 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -128,6 +128,12 @@ fn register_db(f: StandardFramework) -> StandardFramework { .desc("history of recent messages") .cmd(history) ) + .command("rarememe", |c| c + .known_as("rare_meme") + .guild_only(true) + .desc("deliver an underutilized meme") + .cmd(rare_meme) + ) } #[cfg(not(feature = "diesel"))] |
