diff options
Diffstat (limited to 'src/commands')
| -rw-r--r-- | src/commands/meme.rs | 118 |
1 files changed, 57 insertions, 61 deletions
diff --git a/src/commands/meme.rs b/src/commands/meme.rs index bb7315c..43362e1 100644 --- a/src/commands/meme.rs +++ b/src/commands/meme.rs @@ -30,89 +30,85 @@ command!(meme(ctx, msg, args) { let ch = msg.channel_id; if args.len() == 0 { - let conn = connection()?; + rand_meme(ctx, msg)?; + return Ok(()); + } +}); - let should_audio = ctx.currently_playing() && ctx.users_listening()?; - let dist: WeightedChoice<'static, MemeType> = if should_audio { - WeightedChoice::new(unsafe { &mut MEME_WEIGHTS }) - } else { - WeightedChoice::new(unsafe { &mut MEME_WEIGHTS[..2] }) - }; +fn rand_meme(ctx: &Context, message: &Message) -> Result<()> { + let conn = connection()?; - match dist.sample(&mut thread_rng()) { - MemeType::Text => { - let mut text_meme = rand_text(&conn)?; + let should_audio = ctx.currently_playing() && ctx.users_listening()?; + let dist: WeightedChoice<'static, MemeType> = if should_audio { + WeightedChoice::new(unsafe { &mut MEME_WEIGHTS }) + } else { + WeightedChoice::new(unsafe { &mut MEME_WEIGHTS[..2] }) + }; - let mut ctr = 0; - while !should_audio && text_meme.audio_id.is_some() { - text_meme = rand_text(&conn)?; + match dist.sample(&mut thread_rng()) { + MemeType::Text => { + let mut text_meme = rand_text(&conn)?; - ctr += 1; - if ctr > 10 { - warn!("looped 10 times trying to find a non-audio text meme"); - return Ok(()); - } - } + let mut ctr = 0; + while !should_audio && text_meme.audio_id.is_some() { + text_meme = rand_text(&conn)?; - send_text(ctx, &text_meme, &conn, msg)?; - }, - MemeType::Image => { - let image_meme = rand_image(&conn)?; - let image = image_meme.associated_data(&conn)?; - - send_image(&image_meme, &image, ch)?; - }, - MemeType::Audio => { - let audio = rand_audio(&conn)?.associated_data(&conn)?; - send_audio(ctx, msg, &audio)?; + ctr += 1; + if ctr > 10 { + warn!("looped 10 times trying to find a non-audio text meme"); + return Ok(()); + } } - } + + send_meme(ctx, &text_meme, &conn, message)?; + }, + MemeType::Image => send_meme(ctx, &rand_image(&conn)?, &conn, message)?, + MemeType::Audio => send_meme(ctx, &rand_audio(&conn)?, &conn, message)?, } -}); -fn send_text(ctx: &Context, t: &TextMeme, conn: &PgConnection, msg: &Message) -> Result<()> { - let (image, audio) = t.associated_data(conn)?; + Ok(()) +} + + +fn send_meme(ctx: &Context, t: &Meme, conn: &PgConnection, msg: &Message) -> Result<()> { + let image = t.image(conn); + let audio = t.audio(conn); let dist = WeightedChoice::new(unsafe { &mut TTS_WEIGHTS }); - let create_msg = |m: CreateMessage| m - .tts(dist.sample(&mut thread_rng())) - .content(&t.content); + let create_msg = |m: CreateMessage| { + let ret = m + .tts(dist.sample(&mut thread_rng())); + + match t.content { + Some(ref text) => ret.content(text), + None => ret + } + }; match image { - Some(image) => msg.channel_id.send_files(vec!(AttachmentType::Bytes((&image.data, &t.title))), create_msg)?, + Some(image) => msg.channel_id.send_files(vec!(AttachmentType::Bytes((&image?.data, &t.title))), create_msg)?, None => msg.channel_id.send_message(create_msg)?, }; + // note: slight edge-case race condition here: there could have been something queued since we + // checked whether anything was playing. not a significant negative impact and unlikely, so i'm + // not worrying about it if let Some(audio) = audio { - send_audio(ctx, msg, &audio)?; - } - - Ok(()) -} + let audio = audio?; + let queue_lock = ctx.data.lock().get::<PlayQueue>().cloned().unwrap(); + let mut play_queue = queue_lock.write().unwrap(); -fn send_image(image_meme: &ImageMeme, image: &Image, ch: ChannelId) -> Result<()> { - ch.send_files(vec!(AttachmentType::Bytes((&image.data, &image_meme.title))), |m| m.content(""))?; - Ok(()) -} - -// note: slight edge-case race condition here: there could have been something queued since we -// checked whether anything was playing. not a significant negative impact and unlikely, so i'm -// not worrying about it -fn send_audio(ctx: &Context, msg: &Message, audio: &Audio) -> Result<()> { - let queue_lock = ctx.data.lock().get::<PlayQueue>().cloned().unwrap(); - let mut play_queue = queue_lock.write().unwrap(); - - play_queue.queue.push_front(PlayArgs{ - initiator: msg.author.name.clone(), - data: ::either::Right(audio.data.clone()), - sender_channel: msg.channel_id, - }); + play_queue.queue.push_front(PlayArgs{ + initiator: msg.author.name.clone(), + data: ::either::Right(audio.data.clone()), + sender_channel: msg.channel_id, + }); + } Ok(()) } - pub fn db_fallback(ctx: &mut Context, msg: &Message, s: &str) -> Result<()> { |
