diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands/meme.rs | 25 | ||||
| -rw-r--r-- | src/commands/mod.rs | 5 | ||||
| -rw-r--r-- | src/db/mod.rs | 21 |
3 files changed, 45 insertions, 6 deletions
diff --git a/src/commands/meme.rs b/src/commands/meme.rs index 0f9cda8..f235913 100644 --- a/src/commands/meme.rs +++ b/src/commands/meme.rs @@ -32,6 +32,7 @@ use crate::{ commands::send, db::{ *, + rand_audio_meme as db_rand_audio_meme, rand_meme as db_rand_meme, }, Result, @@ -48,9 +49,19 @@ fn update_meme(meme: &Meme) -> Result<()> { Ok(()) } +#[inline] pub fn meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { - if args.len() == 0 { - return rand_meme(ctx, msg); + _meme(ctx, msg, args, true) +} + +#[inline] +pub fn audio_meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { + _meme(ctx, msg, args, true) +} + +fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_only: bool) -> Result<()> { + if args.len() == 0 || audio_only { + return rand_meme(ctx, msg, audio_only); } let search = args.full(); @@ -237,12 +248,15 @@ pub fn renamememe(_: &mut Context, msg: &Message, _: Args) -> Result<()> { send(msg.channel_id, "hwaet", msg.tts) } -fn rand_meme(ctx: &Context, message: &Message) -> Result<()> { +fn rand_meme(ctx: &Context, message: &Message, audio_only: bool) -> Result<()> { let conn = connection()?; let should_audio = ctx.currently_playing() && ctx.users_listening()?; - - let mem = db_rand_meme(&conn, should_audio); + let mem = if audio_only { + db_rand_audio_meme(&conn) + } else { + db_rand_meme(&conn, should_audio) + }; match mem { Ok(mem) => { @@ -256,7 +270,6 @@ fn rand_meme(ctx: &Context, message: &Message) -> Result<()> { } } - fn send_meme(ctx: &Context, t: &Meme, conn: &PgConnection, msg: &Message) -> Result<()> { debug!("sending meme: {:?}", t); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d5d91e2..067d9be 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -83,6 +83,11 @@ fn register_db(f: StandardFramework) -> StandardFramework { .guild_only(true) .help_available(false) .cmd(meme)) + .command("audiomeme", |c| c + .guild_only(true) + .help_available(false) + .cmd(audio_meme) + ) .command("addmeme", |c| c .guild_only(true) .desc("first argument is title, everything after is text. one attached image is included if present.") diff --git a/src/db/mod.rs b/src/db/mod.rs index 1c2a032..00b2138 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -134,4 +134,25 @@ pub fn rand_meme(conn: &PgConnection, audio: bool) -> Result<Meme> { .map_err(Error::from) } +pub fn rand_audio_meme(conn: &PgConnection) -> Result<Meme> { + use rand::{thread_rng, seq::SliceRandom}; + use failure::err_msg; + use std::ops::Try; + + let ids: Vec<i32> = memes::table + .select(memes::id) + .filter(memes::audio_id.is_not_null()) + .load(conn) + .map_err(Error::from)?; + + let id = ids.choose(&mut thread_rng()) + .into_result() + .map_err(|_| err_msg("couldn't load audio meme"))?; + + memes::table + .find(id) + .first::<Meme>(conn) + .map_err(Error::from) +} + no_arg_sql_function!(random, sql_types::Double, "SQL random() function"); |
