aboutsummaryrefslogtreecommitdiff
path: root/src/modules/commands/admins/ignore.rs
blob: 39f64c13d62efdd2b8d95976830385eb2dbd4664 (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
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 IgnoreAdd;
impl Command for IgnoreAdd {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            desc: Some("Want me to ignore a channel? Don't worry, I ain't seen a thing.".to_string()),
            usage: Some("<channel_resolvable>".to_string()),
            example: Some("#general".to_string()),
            min_args: Some(1),
            max_args: Some(1),
            required_permissions: Permissions::MANAGE_GUILD,
            ..default
        };
        Arc::new(options)
    }

    fn execute(&self, _: &mut Context, message: &Message, args: Args) -> Result<(), CommandError> {
        if let Some(guild_id) = message.guild_id {
            let mut guild_data = db.get_guild(guild_id.0 as i64)?;
            if let Some((channel_id, channel)) = parse_channel(args.full().to_string(), guild_id) {
                if !guild_data.ignored_channels.contains(&(channel_id.0 as i64)) {
                    guild_data.ignored_channels.push(channel_id.0 as i64);
                    db.update_guild(guild_id.0 as i64, guild_data)?;
                    message.channel_id.say(format!(
                        "I will now ignore messages in {}",
                        channel.name
                    ))?;
                } else {
                    message.channel_id.say("That channel is already being ignored.")?;
                }
            } else {
                message.channel_id.say("I couldn't find that channel.")?;
            }
        } else {
            failed!(GUILDID_FAIL);
        }
        Ok(())
    }
}

pub struct IgnoreRemove;
impl Command for IgnoreRemove {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            desc: Some("Finally, I can see.".to_string()),
            aliases: vec!["rm", "delete", "del"].iter().map(|e| e.to_string()).collect(),
            usage: Some("<channel_resolvable>".to_string()),
            example: Some("#general".to_string()),
            min_args: Some(1),
            max_args: Some(1),
            required_permissions: Permissions::MANAGE_GUILD,
            ..default
        };
        Arc::new(options)
    }

    fn execute(&self, _: &mut Context, message: &Message, args: Args) -> Result<(), CommandError> {
        if let Some(guild_id) = message.guild_id {
            let mut guild_data = db.get_guild(guild_id.0 as i64)?;
            if let Some((channel_id, channel)) = parse_channel(args.full().to_string(), guild_id) {
                if guild_data.ignored_channels.contains(&(channel_id.0 as i64)) {
                    guild_data.ignored_channels.retain(|e| *e != channel_id.0 as i64);
                    db.update_guild(guild_id.0 as i64, guild_data)?;
                    message.channel_id.say(format!(
                        "I will no longer ignore messages in {}",
                        channel.name
                    ))?;
                } else {
                    message.channel_id.say("That channel isn't being ignored.")?;
                }
            } else {
                message.channel_id.say("I couldn't find that channel.")?;
            }
        } else {
            failed!(GUILDID_FAIL);
        }
        Ok(())
    }
}

pub struct IgnoreList;
impl Command for IgnoreList {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            desc: Some("You want ME to tell YOU want channels I'm ignoring?.".to_string()),
            aliases: vec!["ls"].iter().map(|e| e.to_string()).collect(),
            required_permissions: Permissions::MANAGE_GUILD,
            max_args: Some(0),
            ..default
        };
        Arc::new(options)
    }

    fn execute(&self, _: &mut Context, message: &Message, _: Args) -> Result<(), CommandError> {
        if let Some(guild_id) = message.guild_id {
            let guild_data = db.get_guild(guild_id.0 as i64)?;
            if !guild_data.ignored_channels.is_empty() {
                let channel_out = guild_data.ignored_channels.clone()
                    .iter()
                    .map(|c| format!("<#{}>", c))
                    .collect::<Vec<String>>()
                    .join("\n");
                message.channel_id.send_message(|m| m
                    .embed(|e| e
                        .title("Ignored Channels")
                        .description(channel_out)
                        .colour(*colours::MAIN)
                ))?;
            } else {
                message.channel_id.say("I'm not ignoring any channels.")?;
            }
        } else {
            failed!(GUILDID_FAIL);
        }
        Ok(())
    }
}

pub struct IgnoreLevel;
impl Command for IgnoreLevel {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            desc: Some("Change which ranks can bypass ignored channels, epic.\n\nValues:\n`4` = Bot Owner, `3` = Guild Owner, `2` = Guild Admin, `1` = Guild Mod, `0` = Everyone.".to_string()),
            usage: Some("<0..4>".to_string()),
            example: Some("2".to_string()),
            min_args: Some(1),
            max_args: Some(1),
            required_permissions: Permissions::MANAGE_GUILD,
            ..default
        };
        Arc::new(options)
    }

    fn execute(&self, _: &mut Context, message: &Message, mut args: Args) -> Result<(), CommandError> {
        if let Some(guild_id) = message.guild_id {
            let mut guild_data = db.get_guild(guild_id.0 as i64)?;
            match args.single::<i16>() {
                Ok(level) => {
                    guild_data.ignore_level = level;
                    db.update_guild(guild_id.0 as i64, guild_data)?;
                    message.channel_id.say(format!("Successfully set ignore level to {}", level))?;
                },
                Err(_) => {
                    message.channel_id.say("Please enter an integer between 0 and 4.")?;
                },
            }
        } else {
            failed!(GUILDID_FAIL);
        }
        Ok(())
    }
}