aboutsummaryrefslogtreecommitdiff
path: root/src/commands/meme/delete.rs
blob: 96598d0c0268ca2c1358dd4ea6644befb36d885b (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
use diesel::{
    result::Error as DieselError,
    NotFound,
};
use grate::tracing;
use serenity::all::ReactionType;

use crate::{
    db::{
        connection,
        delete_meme,
    },
    util,
    PoiseContext,
};

/// Delete a meme by name.
#[poise::command(prefix_command, guild_only, category = "memes", aliases("delmem"))]
pub async fn delmeme(ctx: PoiseContext<'_>, title: String) -> anyhow::Result<()> {
    let mut conn = connection().await?;

    match delete_meme(&mut conn, &title, ctx.author().id.get(), util::guild_id(ctx)?.get()).await {
        Ok(_) => {
            util::react(ctx, ReactionType::Unicode("💀".to_owned())).await?;
            Ok(())
        },
        Err(e) => {
            if let Some(NotFound) = e.downcast_ref::<DieselError>() {
                tracing::info!(title, "attempted to delete nonexistent meme");

                util::react(ctx, ReactionType::Unicode("❓".to_owned())).await?;
                util::reply(ctx, "nice try").await?;
                return Ok(());
            }

            Err(e)?;
            Ok(())
        },
    }
}