aboutsummaryrefslogtreecommitdiff
path: root/src/modules/commands/mods/notes.rs
blob: 9427732c5e9b9dc08fece89cfbddc60f7e2336d8 (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
use crate::core::colours;
use crate::core::consts::*;
use crate::core::consts::DB as db;
use crate::core::utils::*;
use serenity::framework::standard::{
    Args,
    Command,
    CommandError,
    CommandOptions
};
use serenity::model::channel::Message;
use serenity::model::Permissions;
use serenity::prelude::Context;
use std::sync::Arc;

pub struct NoteAdd;
impl Command for NoteAdd {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            desc: Some("Of the forgetful kind? Add a note to someone!".to_string()),
            usage: Some("<user_resolvable> <note>".to_string()),
            example: Some("@fun test note".to_string()),
            required_permissions: Permissions::MANAGE_MESSAGES,
            ..default
        };
        Arc::new(options)
    }

    fn execute(&self, _: &mut Context, message: &Message, mut args: Args) -> Result<(), CommandError> {
        if let Some(guild_id) = message.guild_id {
            match parse_user(args.single::<String>().unwrap_or(String::new()), guild_id) {
                Some((user,_)) => {
                    let note = args.rest().to_string();
                    let data = db.new_note(user.0 as i64, guild_id.0 as i64, note, message.author.id.0 as i64)?;
                    message.channel_id.say(format!("Added note `{}`.", data.note))?;
                },
                None => { message.channel_id.say("I couldn't find that user")?; }
            }
        } else { failed!(GUILDID_FAIL); }
        Ok(())
    }
}

pub struct NoteRemove;
impl Command for NoteRemove {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            desc: Some("Oh, you remembered...".to_string()),
            usage: Some("<user_resolvable> <index>".to_string()),
            example: Some("@fun 3".to_string()),
            aliases: vec!["del", "rm"].iter().map(|e| e.to_string()).collect(),
            required_permissions: Permissions::MANAGE_MESSAGES,
            ..default
        };
        Arc::new(options)
    }

    fn execute(&self, _: &mut Context, message: &Message, mut args: Args) -> Result<(), CommandError> {
        if let Some(guild_id) = message.guild_id {
            match parse_user(args.single::<String>().unwrap_or(String::new()), guild_id) {
                Some((user,_)) => {
                    let index = args.single::<i32>().unwrap_or(0);
                    let data = db.del_note(index, user.0 as i64, guild_id.0 as i64)?;
                    message.channel_id.say(format!("Deleted note `{}`.", data))?;
                },
                None => { message.channel_id.say("I couldn't find that user")?; }
            }
        } else { failed!(GUILDID_FAIL); }
        Ok(())
    }
}

pub struct NoteList;
impl Command for NoteList {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            desc: Some("Checking in? See all the notes on someone.".to_string()),
            usage: Some("<user_resolvable>".to_string()),
            example: Some("@fun".to_string()),
            aliases: vec!["ls"].iter().map(|e| e.to_string()).collect(),
            required_permissions: Permissions::MANAGE_MESSAGES,
            ..default
        };
        Arc::new(options)
    }

    fn execute(&self, _: &mut Context, message: &Message, mut args: Args) -> Result<(), CommandError> {
        if let Some(guild_id) = message.guild_id {
            match parse_user(args.single::<String>().unwrap_or(String::new()), guild_id) {
                Some((user_id, member)) => {
                    let notes = db.get_notes(user_id.0 as i64, guild_id.0 as i64)?;
                    let notes_fmt = notes.iter().map(|n| n.to_string()).collect::<Vec<String>>().join("\n\n");
                    message.channel_id.send_message(|m| m
                        .embed(|e| e
                            .colour(*colours::MAIN)
                            .title(format!("Notes for {}", member.display_name().into_owned()))
                            .description(notes_fmt)
                    ))?;
                },
                None => { message.channel_id.say("I couldn't find that user")?; }
            }
        } else { failed!(GUILDID_FAIL); }
        Ok(())
    }
}