aboutsummaryrefslogtreecommitdiff
path: root/src/commands/meme/invoke.rs
blob: f057db5703c71dc59c0cfb9b3b36a9c4e69ab307 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use diesel::{
    NotFound,
    result::Error as DieselError,
};
use grate::tracing;

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

/// Post a meme.
#[poise::command(prefix_command, guild_only, category = "memes", aliases("mem"))]
pub async fn meme(ctx: PoiseContext<'_>, title: RestVec) -> anyhow::Result<()> {
    let title = title.into_inner().join(" ");

    _meme(ctx, title.trim(), AudioPlayback::Optional).await
}

/// Post a random omen.
#[poise::command(prefix_command, guild_only, category = "memes", discard_spare_arguments)]
pub async fn omen(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
    _meme(ctx, "", AudioPlayback::Optional).await
}

/// Post a random omen without audio.
#[poise::command(prefix_command, guild_only, category = "memes", discard_spare_arguments)]
pub async fn silentomen(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
    _meme(ctx, "", AudioPlayback::Prohibited).await
}

/// Post a random omen with audio.
#[poise::command(prefix_command, guild_only, category = "memes", discard_spare_arguments)]
pub async fn audioomen(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
    _meme(ctx, "", AudioPlayback::Required).await
}

/// Post a random meme with audio.
#[poise::command(prefix_command, guild_only, category = "memes", aliases("audiomeme", "audiomem"))]
pub async fn audio_meme(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
    _meme(ctx, "", AudioPlayback::Required).await
}

/// Post a random meme without audio.
#[poise::command(
    prefix_command,
    guild_only,
    category = "memes",
    aliases("silentmeme", "silentmem")
)]
pub async fn silent_meme(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
    _meme(ctx, "", AudioPlayback::Prohibited).await
}

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

pub(crate) async fn _meme(
    ctx: PoiseContext<'_>,
    args: impl AsRef<str>,
    audio_playback: AudioPlayback,
) -> anyhow::Result<()> {
    let args = args.as_ref().trim();

    if args.is_empty() || audio_playback != AudioPlayback::Optional {
        return rand_meme(ctx, audio_playback).await;
    }

    let guild_id = util::guild_id(ctx)?;

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

            x
        },
        Err(e) => {
            return if let Some(NotFound) = e.downcast_ref::<DieselError>() {
                tracing::info!("requested meme not found in database");

                util::reply(ctx, "c'mon baby, guesstimate").await?;
                Ok(())
            } else {
                util::reply(ctx, "what in ryan's name").await?;
                Err(e)
            };
        },
    };

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

async fn rand_meme(ctx: PoiseContext<'_>, audio_playback: AudioPlayback) -> anyhow::Result<()> {
    let should_audio = util::users_listening(ctx).await?;
    let guild_id = util::guild_id(ctx)?;

    let mut conn = connection().await?;

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

    match mem {
        Ok(Some(mem)) => {
            InvocationRecord::create(
                &mut conn,
                ctx.author().id.get(),
                util::guild_id(ctx)?.get(),
                ctx.id(),
                mem.id,
                true,
            )
            .await?;
            send_meme(ctx, &mem, &mut conn).await?;
            Ok(())
        },
        Ok(None) => {
            tracing::info!("random meme not found");
            util::reply(ctx, "i don't know any :(").await?;
            Ok(())
        },
        Err(e) => {
            if let Some(NotFound) = e.downcast_ref::<DieselError>() {
                tracing::info!("random meme not found");

                util::reply(ctx, "i don't know any :(").await?;
                return Ok(());
            }

            util::reply(ctx, "HELP").await?;
            Err(e)
        },
    }
}

/// Post a rare meme.
#[poise::command(prefix_command, guild_only, category = "memes", aliases("raremem", "rarememe"))]
pub async fn rare_meme(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
    let guild = util::guild_id(ctx)?;
    let should_audio = util::users_listening(ctx).await?;

    let mut conn = connection().await?;
    let meme = db::rare_meme(&mut conn, should_audio, guild.get()).await;

    match meme {
        Ok(Some(meme)) => {
            InvocationRecord::create(
                &mut conn,
                ctx.author().id.get(),
                util::guild_id(ctx)?.get(),
                ctx.id(),
                meme.id,
                true,
            )
            .await?;
            send_meme(ctx, &meme, &mut conn).await
        },

        Ok(None) => {
            tracing::info!("rare meme not found");
            util::reply(ctx, "i don't know any :(").await?;

            Ok(())
        },

        Err(e) => {
            if let Some(NotFound) = e.downcast_ref::<DieselError>() {
                tracing::info!("rare meme not found");
                util::reply(ctx, "i don't know any :(").await?;

                return Ok(());
            }

            util::reply(ctx, "THE MEME MARKET IS IN FREEFALL").await?;

            Err(e)
        },
    }
}