aboutsummaryrefslogtreecommitdiff
path: root/src/commands/meme/mod.rs
blob: d816a937fbf320ad3ef2b9a7834a7e63700ab85d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use diesel::PgConnection;
use log::debug;
use rand::{Rng, thread_rng};
use serenity::{
    framework::standard::macros::group,
    http::AttachmentType,
    model::channel::Message,
    prelude::*,
};

use crate::{
    audio::{
        PlayArgs,
        PlayQueue,
    },
    db::Meme,
    Result,
};

pub use self::{
    create::*,
    delete::*,
    history::*,
    invoke::*,
};

mod history;
mod create;
mod invoke;
mod delete;

group!({
    name: "memes",
    options: {
        only_in: "guild",
    },
    commands: [
        meme,
        omen,
        audio_meme,
        silent_Meme,
        addmeme,
        addaudiomeme,
        delmeme,
        wat,
        stats,
        history,
        rare_meme,
        memers,
        query,
    ],
});

fn send_meme(ctx: &Context, t: &Meme, conn: &mut PgConnection, msg: &Message) -> Result<()> {
    let should_tts = t.content.as_ref().map(|t| t.len() > 0).unwrap_or(false) &&
        thread_rng().gen::<u32>() % 25 == 0;

    debug!("sending meme (tts: {}): {:?}", should_tts, t);

    let image = t.image(conn);
    let audio = t.audio(conn);

    match image {
        Some(image) => {
            let image = image?;
            msg.channel_id.send_files(ctx, vec!(AttachmentType::Bytes((&image.data, &image.filename))), |m| {
                let ret = m.tts(should_tts);

                match t.content {
                    Some(ref text) if text.len() > 0 => ret.content(text),
                    _ => ret,
                }
            })?;
        },

        None => match t.content {
            Some(_) => { msg.channel_id.send_message(ctx, |m| {
                let ret = m.tts(should_tts);

                match t.content {
                    Some(ref text) if text.len() > 0 => ret.content(text),
                    _ => ret,
                }
            })?; },
            None => {},

        },
    };

    // 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 {
        let audio = audio?;

        {
            let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap();
            let mut play_queue = queue_lock.write().unwrap();

            play_queue.meme_queue.push_back(PlayArgs{
                initiator: msg.author.name.clone(),
                data: ::either::Right(audio.data.clone()),
                sender_channel: msg.channel_id,
                start: None,
                end: None,
            });
        }

        msg.react(ctx, "📣")?;
    }

    Ok(())
}