aboutsummaryrefslogtreecommitdiff
path: root/src/modules/commands/mods/watchlist.rs
blob: 4f3cd7ba858cdd17333c98d1a21f4c6ae58fb06c (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
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::id::UserId;
use serenity::model::Permissions;
use serenity::prelude::Context;
use std::sync::Arc;

pub struct WatchlistAdd;
impl Command for WatchlistAdd {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            desc: Some("What have they done now... Add someone the the watchlist.".to_string()),
            usage: Some("<user_resolvable>".to_string()),
            example: Some("@fun".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_id, member)) => {
                    let mut user_data = db.get_user(user_id.0 as i64, guild_id.0 as i64)?;
                    user_data.watchlist = true;
                    db.update_user(user_id.0 as i64, guild_id.0 as i64, user_data)?;
                    message.channel_id.say(format!("Set {} to watchlist status.", member.display_name().into_owned()))?;
                },
                None => { message.channel_id.say("I couldn't find that user")?; }
            }
        } else { failed!(GUILDID_FAIL); }
        Ok(())
    }
}

pub struct WatchlistRemove;
impl Command for WatchlistRemove {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            desc: Some("Given up, have you?".to_string()),
            usage: Some("<user_resolvable>".to_string()),
            example: Some("@fun".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_id, member)) => {
                    let mut user_data = db.get_user(user_id.0 as i64, guild_id.0 as i64)?;
                    user_data.watchlist = false;
                    db.update_user(user_id.0 as i64, guild_id.0 as i64, user_data)?;
                    message.channel_id.say(format!("Unset {} from watchlist status.", member.display_name().into_owned()))?;
                },
                None => { message.channel_id.say("I couldn't find that user")?; }
            }
        } else { failed!(GUILDID_FAIL); }
        Ok(())
    }
}

pub struct WatchlistList;
impl Command for WatchlistList {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            desc: Some("Yeah, I'll tell you who's on the watchlist.".to_string()),
            required_permissions: Permissions::MANAGE_MESSAGES,
            aliases: vec!["ls"].iter().map(|e| e.to_string()).collect(),
            ..default
        };
        Arc::new(options)
    }

    fn execute(&self, _: &mut Context, message: &Message, _: Args) -> Result<(), CommandError> {
        if let Some(guild_id) = message.guild_id {
            let users = db.get_users(guild_id.0 as i64)?;
            let user_map = users.iter()
                .filter(|e| e.watchlist)
                .map(|u| {
                    match UserId(u.id as u64).to_user() {
                        Ok(user) => user.tag(),
                        Err(_) => format!("<#{}>", u.id),
                    }
                })
                .collect::<Vec<String>>()
                .join("\n");
            message.channel_id.send_message(|m| m
                .embed(|e| e
                    .title("Watchlist")
                    .description(user_map)
                    .colour(*colours::MAIN)
            ))?;
        } else { failed!(GUILDID_FAIL); }
        Ok(())
    }
}