aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2019-03-09 17:09:57 -0500
committerNathan Perry <avaglir@gmail.com>2019-03-09 17:09:57 -0500
commita541cc28c435f039b977087cf7d07648a160633b (patch)
tree57f4148dcd789e865dbf8d81daf07458c88fc07e
parenta0aa6c6310b046d27c71cef23dcf6e6811cc11de (diff)
add silentmeme command
-rw-r--r--src/commands/meme.rs33
-rw-r--r--src/commands/mod.rs5
-rw-r--r--src/db/mod.rs21
3 files changed, 49 insertions, 10 deletions
diff --git a/src/commands/meme.rs b/src/commands/meme.rs
index 784c07e..fa87393 100644
--- a/src/commands/meme.rs
+++ b/src/commands/meme.rs
@@ -39,6 +39,7 @@ use crate::{
*,
rand_audio_meme as db_rand_audio_meme,
rand_meme as db_rand_meme,
+ rand_silent_meme as db_rand_silent_meme,
},
Result,
};
@@ -55,17 +56,28 @@ lazy_static! {
#[inline]
pub fn meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
- _meme(ctx, msg, args, false)
+ _meme(ctx, msg, args, AudioPlayback::Optional)
}
#[inline]
pub fn audio_meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
- _meme(ctx, msg, args, true)
+ _meme(ctx, msg, args, AudioPlayback::Required)
}
-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);
+pub fn silent_meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
+ _meme(ctx, msg, args, AudioPlayback::Prohibited)
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
+enum AudioPlayback {
+ Required,
+ Optional,
+ Prohibited,
+}
+
+fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_playback: AudioPlayback) -> Result<()> {
+ if args.len() == 0 || audio_playback != AudioPlayback::Optional {
+ return rand_meme(ctx, msg, AudioPlayback::Required);
}
let search = args.full();
@@ -391,14 +403,15 @@ and {} were audio ({:0.1}%)"#,
send(msg.channel_id, s, msg.tts)
}
-fn rand_meme(ctx: &Context, message: &Message, audio_only: bool) -> Result<()> {
+fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> Result<()> {
let conn = connection()?;
let should_audio = ctx.users_listening()?;
- let mem = if audio_only {
- db_rand_audio_meme(&conn)
- } else {
- db_rand_meme(&conn, should_audio)
+
+ 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),
};
match mem {
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 4619a5a..1582977 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -94,6 +94,11 @@ fn register_db(f: StandardFramework) -> StandardFramework {
.help_available(false)
.cmd(audio_meme)
)
+ .command("silentmeme", |c| c
+ .guild_only(true)
+ .help_available(false)
+ .cmd(silent_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 b3861d2..191b9e9 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -158,6 +158,27 @@ pub fn rand_audio_meme(conn: &PgConnection) -> Result<Meme> {
.map_err(Error::from)
}
+pub fn rand_silent_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_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)
+}
+
#[derive(Debug, Copy, Clone)]
pub struct Stats {
pub memes_overall: usize,