aboutsummaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2018-06-01 05:42:40 -0400
committerNathan Perry <avaglir@gmail.com>2018-06-01 05:42:40 -0400
commit413384896f66aeb2737f5f60433c2467d867fb36 (patch)
tree9fc8482e9d61d8d1038d791f46ef055d7cd7c0f7 /src/commands
parenta52eae2964e8e86bf3e587f17d607025f07efdbb (diff)
various command improvements
- support last_meme (report details about last meme played back) - update filter priority for meme search (check title match first)
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/meme.rs68
-rw-r--r--src/commands/mod.rs10
2 files changed, 69 insertions, 9 deletions
diff --git a/src/commands/meme.rs b/src/commands/meme.rs
index d1c90d1..44e29f1 100644
--- a/src/commands/meme.rs
+++ b/src/commands/meme.rs
@@ -3,14 +3,26 @@ use serenity::http::AttachmentType;
use serenity::builder::CreateMessage;
use serenity::framework::standard::Args;
use diesel::PgConnection;
+use failure::Error;
+use std::sync::RwLock;
use super::*;
use super::playback::CtxExt;
use db::*;
-use failure::Error;
use Result;
+lazy_static! {
+ static ref LAST_MEME: RwLock<Option<i32>> = RwLock::new(None);
+}
+
+fn update_meme(meme: &Meme) -> Result<()> {
+ let mut opt = LAST_MEME.write().map_err(|_| ::failure::err_msg("unable to acquire lock"))?;
+ *opt = Some(meme.id);
+
+ Ok(())
+}
+
pub fn meme(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
if args.len() == 0 {
return rand_meme(ctx, msg);
@@ -20,9 +32,20 @@ pub fn meme(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
let conn = connection()?;
let mem = match find_meme(&conn, search) {
- Ok(x) => x,
+ Ok(x) => {
+ update_meme(&x)?;
+
+ x
+ },
Err(e) => {
- send(msg.channel_id, "what in ryan's name", msg.tts)?;
+ use diesel::{NotFound, self};
+
+ if let Some(NotFound) = e.downcast_ref::<diesel::result::Error>() {
+ send(msg.channel_id, "c'mon baby, guesstimate", msg.tts)?;
+ } else {
+ send(msg.channel_id, "what in ryan's name", msg.tts)?;
+ }
+
return Err(e)
},
};
@@ -30,6 +53,34 @@ pub fn meme(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
send_meme(ctx, &mem, &conn, msg)
}
+pub fn wat(_: &mut Context, msg: &Message, _: Args) -> Result<()> {
+ use failure::err_msg;
+
+ let conn = connection()?;
+ let meme = LAST_MEME.read()
+ .map_err(|_| err_msg("unable to acquire read lock"))
+ .and_then(|id| {
+ id.ok_or(err_msg("no previous meme"))
+ .and_then(|id| {
+ Meme::find(&conn, id)
+ })
+ });
+
+ match meme {
+ Ok(ref meme) => {
+ let metadata = Metadata::find(&conn, meme.metadata_id)?;
+ let author = ::TARGET_GUILD_ID.member(metadata.created_by as u64)?;
+
+ send(msg.channel_id,
+ &format!("that was \"{}\" by {} ({})",
+ meme.title, author.mention(), metadata.created.date()), msg.tts)?
+ },
+ Err(_) => send(msg.channel_id, "heuueueeeeh?", msg.tts)?,
+ };
+
+ meme.map(|_| {})
+}
+
pub fn addmeme(_: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
let title = args.single_quoted::<String>()?;
let text = match args.multiple_quoted::<String>() {
@@ -106,14 +157,15 @@ fn rand_meme(ctx: &Context, message: &Message) -> Result<()> {
});
match mem {
- Err(e) => {
+ Ok(mem) => {
+ update_meme(&mem)?;
+ send_meme(ctx, &mem, &conn, message).map_err(Error::from)
+ },
+ err @ Err(_) => {
send(message.channel_id, "i don't know any :(", message.tts)?;
- return Err(e);
+ err.map(|_| {})
},
- _ => {},
}
-
- send_meme(ctx, &mem?, &conn, message).map_err(Error::from)
}
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 056aec6..80581d5 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -87,7 +87,7 @@ fn register_db(f: StandardFramework) -> StandardFramework {
)
.command("delmeme", |c| c
.guild_only(true)
- .desc("not currently working")
+ .desc("delete a meme by name (exact match only)")
.cmd(delmeme)
)
.command("renamememe", |c| c
@@ -96,6 +96,14 @@ fn register_db(f: StandardFramework) -> StandardFramework {
.help_available(false)
.cmd(renamememe)
)
+ .command("wat", |c| c
+ .known_as("what")
+ .known_as("last")
+ .known_as("lastmeme")
+ .guild_only(true)
+ .desc("check info for last meme")
+ .cmd(wat)
+ )
}
#[cfg(not(feature = "diesel"))]