diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands/meme.rs | 196 |
1 files changed, 134 insertions, 62 deletions
diff --git a/src/commands/meme.rs b/src/commands/meme.rs index 26f6ee3..fcff7f5 100644 --- a/src/commands/meme.rs +++ b/src/commands/meme.rs @@ -35,12 +35,8 @@ lazy_static! { ).expect("unable to compile command regex"); static ref QUOTES_REGEX: Regex = Regex::new( - r##"^\s*(?:"([^"]*)"|([^"\s]*))\s*$"## + r##"\s*(?:"([^"]*)"|([^"\s]*))\s*"## ).expect("unable to compile quotes regex"); - - static ref HELP_REGEX: Regex = Regex::new( - r##"(?:^|\s)(?:--help|-h)(?:\s|$)"## - ).expect("unable to compile help regex"); } pub fn meme(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { @@ -51,8 +47,10 @@ pub fn meme(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { .ok_or::<Error>(::failure::err_msg("first capture group not found"))? .as_str(); + let normalized = format!("meme {}", arg_str); + let args = QUOTES_REGEX - .captures_iter(arg_str) + .captures_iter(&normalized) .map(|capture| { capture.iter() .skip(1) @@ -64,33 +62,37 @@ pub fn meme(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { let matches = match app().get_matches_from_safe_borrow(args.iter()) { Ok(x) => x, - Err(_) => { + Err(e) => { + warn!("syntax error: {:?}", e); + return send(msg.channel_id, "hwaet the fuck fix your syntax", msg.tts); } }; + trace!("{:?}", matches); + lazy_static! { - static ref HELP: String = { + static ref GEN_HELP: String = { let mut str = Vec::new(); app().write_long_help(&mut str).expect("unable to write out help"); String::from_utf8(str).expect("unable to read long help as utf8") }; } - if HELP_REGEX.is_match(arg_str) { // because clap is stupid - return send(msg.channel_id, &format!("```{}```", &*HELP), msg.tts); + if matches.is_present("help") { // because clap is stupid + return send(msg.channel_id, &format!("```{}```", &*GEN_HELP), msg.tts); } if let Some(add_matches) = matches.subcommand_matches("add") { lazy_static! { static ref ADD_HELP: String = { let mut str = Vec::new(); - app().write_long_help(&mut str).expect("unable to write out help"); + app_add().write_long_help(&mut str).expect("unable to write out help"); String::from_utf8(str).expect("unable to read long help as utf8") }; } - if HELP_REGEX.is_match(arg_str) { // because clap is stupid + if add_matches.is_present("help") { return send(msg.channel_id, &format!("```{}```", &*ADD_HELP), msg.tts); } @@ -98,8 +100,13 @@ pub fn meme(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { let audio = add_matches.value_of("audio"); let text = add_matches.value_of("text"); - let title = add_matches.value_of("title") - .ok_or::<Error>(::failure::err_msg("somehow found no title for new meme"))?; + let title = match add_matches.value_of("TITLE") { + Some(title) => title, + None => { + send(msg.channel_id, "bottom text", msg.tts)?; + return Err(::failure::err_msg("title missing")); + } + }; if image.is_none() && audio.is_none() && text.is_none() { return send(msg.channel_id, "hahAA it's empty xdddd", msg.tts); @@ -140,21 +147,47 @@ pub fn meme(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { }.save(&conn, msg.author.id.0).map(|_| {}); } - if let Some(_) = matches.subcommand_matches("delete") { + if let Some(matches) = matches.subcommand_matches("delete") { + lazy_static! { + static ref DELETE_HELP: String = { + let mut str = Vec::new(); + app_delete().write_long_help(&mut str).expect("unable to write out help"); + String::from_utf8(str).expect("unable to read long help as utf8") + }; + } + + if matches.is_present("help") { + return send(msg.channel_id, &format!("```{}```", &*DELETE_HELP), msg.tts); + } + return send(msg.channel_id, "hwaet", msg.tts); } - if let Some(search) = matches.value_of("SEARCH") { - let conn = connection()?; - let mem = match find_meme(&conn, search) { - Ok(x) => x, - Err(e) => { - send(msg.channel_id, "what in ryan's name", msg.tts)?; - return Err(e) - }, - }; + if let Some(matches) = matches.subcommand_matches("post") { + lazy_static! { + static ref POST_HELP: String = { + let mut str = Vec::new(); + app_post().write_long_help(&mut str).expect("unable to write out help"); + String::from_utf8(str).expect("unable to read long help as utf8") + }; + } - return send_meme(ctx, &mem, &conn, msg); + if matches.is_present("help") { + return send(msg.channel_id, &format!("```{}```", &*POST_HELP), msg.tts); + } + + if let Some(search) = matches.value_of("SEARCH") { + let conn = connection()?; + let mem = match find_meme(&conn, search) { + Ok(x) => x, + Err(e) => { + send(msg.channel_id, "what in ryan's name", msg.tts)?; + return Err(e) + }, + }; + + return send_meme(ctx, &mem, &conn, msg); + } } rand_meme(ctx, msg) @@ -307,53 +340,92 @@ fn load_image(client: &Client, conn: &PgConnection, url: &str, title: &str, msg: let ext = resp.headers().get::<ContentType>() .and_then(|typ| ::mime_guess::get_extensions(typ.type_().as_str(), typ.subtype().as_str())) .and_then(|x| x.first()) + .map(|x| match *x { + "jpe" => "jpg", + x => x, + }) .unwrap_or(&"bin"); - let filename = format!("{}.{}", title, *ext); + let filename = format!("{}.{}", title, ext); Image::create(conn, &filename, data, msg.author.id.0) } fn app<'a, 'b>() -> App<'a, 'b> { - App::new("!thulani meme") + App::new("meme") .about("manipulate memes. pass no arguments to produce a randomly-selected meme.") - .settings(&vec![AppSettings::DisableHelpSubcommand, AppSettings::DisableVersion]) + .global_settings(&vec![AppSettings::DisableHelpSubcommand, AppSettings::DisableVersion]) + .arg(Arg::with_name("help") + .short("h") + .long("help") + .help("show this help message") + ) + .subcommand(app_add()) + .subcommand(app_delete()) + .subcommand(app_post()) +} + +fn app_post<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("post") + .about("post a specific meme (partial, exact, case-insensitive matches only)") + .global_settings(&vec![AppSettings::DisableHelpSubcommand, AppSettings::DisableVersion]) .arg(Arg::with_name("SEARCH") + .takes_value(true) .index(1) - .help("search for a meme by name or content (exact, case-insensitive matches only)") ) - .subcommand(SubCommand::with_name("add") - .about("add a meme to the database") - .arg(Arg::with_name("TITLE") - .help("title for new meme") - .required(true) - .index(1) - ) - .arg(Arg::with_name("image") - .short("i") - .long("image") - .multiple(false) - .help("url of image to attach (use 'attached' to use image attached to message)") - .takes_value(true) - ) - .arg(Arg::with_name("audio") - .short("a") - .long("audio") - .multiple(false) - .help("address of a video downloadable with youtube-dl. timestamps not yet supported.") - .takes_value(true) - ) - .arg(Arg::with_name("text") - .short("t") - .long("text") - .multiple(false) - .help("text to play back") - .takes_value(true) - ) + .arg(Arg::with_name("help") + .short("h") + .long("help") + .help("show this help message") ) - .subcommand(SubCommand::with_name("delete") - .about("delete a meme from the database") - .arg(Arg::with_name("title") - .index(1) - ) +} + +fn app_add<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("add") + .about("add a meme to the database") + .global_settings(&vec![AppSettings::DisableHelpSubcommand, AppSettings::DisableVersion]) + .arg(Arg::with_name("help") + .short("h") + .long("help") + .help("show this help message") + ) + .arg(Arg::with_name("TITLE") + .index(1) + .help("title for new meme") + .takes_value(true) + ) + .arg(Arg::with_name("image") + .short("i") + .long("image") + .multiple(false) + .help("url of image to attach (use 'attached' to use image attached to message)") + .takes_value(true) + ) + .arg(Arg::with_name("audio") + .short("a") + .long("audio") + .multiple(false) + .help("address of a video downloadable with youtube-dl. timestamps not yet supported.") + .takes_value(true) + ) + .arg(Arg::with_name("text") + .short("t") + .long("text") + .multiple(false) + .help("text to play back") + .takes_value(true) + ) +} + +fn app_delete<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("delete") + .about("delete a meme from the database") + .global_settings(&vec![AppSettings::DisableHelpSubcommand, AppSettings::DisableVersion]) + .arg(Arg::with_name("help") + .short("h") + .long("help") + .help("show this help message") + ) + .arg(Arg::with_name("title") + .index(1) ) }
\ No newline at end of file |
