aboutsummaryrefslogtreecommitdiff
path: root/src/commands/meme/delete.rs
blob: 6af1b6b696e1b27d4c6ba36b611aee403b51c578 (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
use diesel::{
    result::Error as DieselError,
    NotFound,
};
use log::info;
use serenity::{
    all::ReactionType,
    framework::standard::{
        macros::command,
        Args,
        CommandResult,
    },
    model::channel::Message,
    prelude::*,
};

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

#[command]
#[aliases("delmem")]
pub async fn delmeme(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
    let title = args.single_quoted::<String>()?;

    let mut conn = connection().await?;

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

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