diff options
| author | Nathan Perry <avaglir@gmail.com> | 2018-04-06 19:22:55 -0400 |
|---|---|---|
| committer | Nathan Perry <avaglir@gmail.com> | 2018-04-06 19:22:55 -0400 |
| commit | d6bea61fa917d257219a43386c88a4f04cf82408 (patch) | |
| tree | c008923ffd1a934a4d295dddb9f5557a0e96ab4f /src/commands | |
| parent | d209da7a02887f433bfdd44f9b225179bdbb7b75 (diff) | |
database alteration in-flight
Diffstat (limited to 'src/commands')
| -rw-r--r-- | src/commands/meme.rs | 121 |
1 files changed, 120 insertions, 1 deletions
diff --git a/src/commands/meme.rs b/src/commands/meme.rs index 89a979d..277b5f8 100644 --- a/src/commands/meme.rs +++ b/src/commands/meme.rs @@ -1,7 +1,20 @@ +use std::time::Duration; + use rand::{thread_rng, distributions::{Weighted, WeightedChoice, Distribution}}; use serenity::http::AttachmentType; use serenity::builder::CreateMessage; use diesel::PgConnection; +use reqwest::{ + Client, + header::{ + Headers, + ContentLength, + UserAgent, + AcceptEncoding, + Encoding, + qitem, + } +}; use super::*; use super::playback::CtxExt; @@ -28,10 +41,116 @@ static mut TTS_WEIGHTS: [Weighted<bool>; 2] = [ ]; command!(meme(ctx, msg, args) { - if args.len() == 0 { + if args.len_quoted() == 0 { rand_meme(ctx, msg)?; return Ok(()); } + + macro_rules! next { () => { args.single_quoted::<String>()?.to_lowercase() }; } + + match next!().as_ref() { + "add" => { // e.g.: !thulani meme add title [image IMAGE] [audio|sound AUDIO] [text TEXT...] + let mut new_meme = NewMeme { + title: next!(), + content: None, + image_id: None, + audio_id: None, + metadata_id: 0, + }; + + let mut headers = Headers::new(); + headers.set(AcceptEncoding(vec!(qitem(Encoding::Gzip)))); + headers.set(UserAgent::new("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0)")); + + let client = Client::builder() + .default_headers(headers) + .timeout(Duration::from_secs(5)) + .build()?; + + let conn = connection()?; + + while args.len() > 0 { + match next!().as_ref() { + "text" => new_meme.content = Some(args.full().to_owned()), + "image" => { + let url = args.single_quoted::<String>()?; + let resp = client.head(&url).send()?; + + if !resp.status().is_success() { + send(msg.channel_id, "pick a better url next time thanks", msg.tts)?; + return Ok(()); + } + + let len = resp.headers().get::<ContentLength>() + .map(|ct_len| **ct_len) + .unwrap_or(0); + + if len > 20_000_000 { + send(msg.channel_id, "are you trying to bankrupt my disk space", msg.tts)?; + return Ok(()); + } + + let mut resp = client.get(&url).send()?; + + if !resp.status().is_success() { + send(msg.channel_id, "pick a better url next time thanks", msg.tts)?; + return Ok(()); + } + + let len = resp.headers().get::<ContentLength>() + .map(|ct_len| **ct_len) + .unwrap_or(0); + + if len > 20_000_000 { + send(msg.channel_id, "are you fucking serious", msg.tts)?; + return Ok(()); + } + + if !resp.status().is_success() { + send(msg.channel_id, "bad link reeeeee", msg.tts)?; + return Ok(()); + } + + let mut data = Vec::with_capacity(len as usize); + ::std::io::copy(&mut resp, &mut data)?; + + let image_id = Image::create(&conn, data, msg.author.id.0)?; + new_meme.image_id = Some(image_id); + }, + "audio" | "sound" => { + let _url = args.single_quoted::<String>()?; + }, + _ => { + send(msg.channel_id, "hueh?", msg.tts)?; + return Ok(()); + } + } + } + + if new_meme.content.is_none() && new_meme.image_id.is_none() && new_meme.audio_id.is_none() { + send(msg.channel_id, "haha it's empty lol xdddd", msg.tts)?; + return Ok(()); + } + + new_meme.save(&conn, msg.author.id.0)?; + send(msg.channel_id, "i hate my job", msg.tts)?; + }, + "delete" | "remove" => { + send(msg.channel_id, "hwaet", msg.tts)?; + }, + search => { + let conn = connection()?; + let mem = match find_meme(&conn, search) { + Ok(x) => x, + Err(_) => { + send(msg.channel_id, "what in ryan's name", msg.tts)?; + return Ok(()); + }, + }; + + send_meme(ctx, &mem, &conn, msg)?; + } + } }); fn rand_meme(ctx: &Context, message: &Message) -> Result<()> { |
