aboutsummaryrefslogtreecommitdiff
path: root/src/commands/meme/invoke.rs
blob: 03c62512a94b52a7e126d2f9e5476e88214d0fd7 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use diesel::{
    result::Error as DieselError,
    NotFound,
};
use itertools::Itertools;
use log::info;
use serenity::{
    framework::standard::{
        macros::command,
        Args,
        CommandError,
        CommandResult,
    },
    futures::TryFutureExt,
    model::channel::Message,
    prelude::*,
};

use crate::{
    commands::meme::send_meme,
    db::{
        self,
        connection,
        find_meme,
        InvocationRecord,
    },
    util,
};

#[command]
#[aliases("mem")]
pub async fn meme(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
    _meme(ctx, msg, args, AudioPlayback::Optional)
}

#[command]
pub async fn omen(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
    let args = Args::new("", &[]);
    _meme(ctx, msg, args, AudioPlayback::Optional)
}

#[command]
pub async fn silentomen(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
    let args = Args::new("", &[]);
    _meme(ctx, msg, args, AudioPlayback::Prohibited)
}

#[command]
pub async fn audioomen(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
    let args = Args::new("", &[]);
    _meme(ctx, msg, args, AudioPlayback::Required)
}

#[command]
#[aliases("audiomeme", "audiomem")]
pub async fn audio_meme(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
    _meme(ctx, msg, args, AudioPlayback::Required)
}

#[command]
#[aliases("silentmeme", "silentmem")]
pub async fn silent_meme(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
    _meme(ctx, msg, args, AudioPlayback::Prohibited)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum AudioPlayback {
    Required,
    Optional,
    Prohibited,
}

async fn _meme(
    ctx: &Context,
    msg: &Message,
    args: Args,
    audio_playback: AudioPlayback,
) -> CommandResult {
    if args.len() == 0 || audio_playback != AudioPlayback::Optional {
        return rand_meme(ctx, msg, audio_playback).await;
    }

    let search = args.raw().join(" ");

    let mut conn = connection()?;
    let mem = match find_meme(&mut conn, search) {
        Ok(x) => {
            InvocationRecord::create(&mut conn, msg.author.id.get(), msg.id.get(), x.id, false)?;

            x
        },
        Err(e) => {
            return if let Some(NotFound) = e.downcast_ref::<DieselError>() {
                info!("requested meme not found in database");
                util::send(ctx, msg.channel_id, "c'mon baby, guesstimate", msg.tts)
                    .await
                    .map_err(CommandError::from)
            } else {
                util::send(ctx, msg.channel_id, "what in ryan's name", msg.tts).await?;
                Err(e.into())
            };
        },
    };

    send_meme(ctx, &mem, &mut conn, msg).await
}

async fn rand_meme(
    ctx: &Context,
    message: &Message,
    audio_playback: AudioPlayback,
) -> CommandResult {
    let mut conn = connection()?;

    let should_audio = util::users_listening(ctx).await?;

    let mem = match audio_playback {
        AudioPlayback::Required => db::rand_audio_meme(&mut conn),
        AudioPlayback::Optional => db::rand_meme(&mut conn, should_audio),
        AudioPlayback::Prohibited => db::rand_silent_meme(&mut conn),
    };

    match mem {
        Ok(mem) => {
            InvocationRecord::create(
                &mut conn,
                message.author.id.get(),
                message.id.get(),
                mem.id,
                true,
            )?;
            send_meme(ctx, &mem, &mut conn, message).await?;
            Ok(())
        },
        Err(e) => {
            match e.downcast_ref::<DieselError>() {
                Some(NotFound) => {
                    info!("random meme not found");
                    return util::send(ctx, message.channel_id, "i don't know any :(", message.tts)
                        .map_err(CommandError::from)
                        .await;
                },
                _ => {},
            }

            util::send(ctx, message.channel_id, "HELP", message.tts).await?;
            return Err(e.into());
        },
    }
}

#[command]
#[aliases("rarememe", "raremem")]
pub async fn rare_meme(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
    let should_audio = ctx.users_listening()?;

    let mut conn = connection()?;

    let meme = db::rare_meme(&mut conn, should_audio);
    match meme {
        Ok(meme) => {
            InvocationRecord::create(&mut conn, msg.author.id.get(), msg.id.get(), meme.id, true)?;
            send_meme(ctx, &meme, &mut conn, msg)
        },
        Err(e) => {
            match e.downcast_ref::<DieselError>() {
                Some(NotFound) => {
                    info!("rare meme not found");
                    return util::send(ctx, msg.channel_id, "i don't know any :(", msg.tts)
                        .map_err(CommandError::from)
                        .await;
                },
                _ => {},
            }

            util::send(ctx, msg.channel_id, "THE MEME MARKET IS IN FREEFALL", msg.tts)
                .map_err(CommandError::from)
                .await?;

            Err(e)
        },
    }
}