user_management.rs 4.72 KB
Newer Older
tec's avatar
tec committed
1
2
3
4
5
6
7
use rand::Rng;
use serenity::{
    model::{channel::Message, guild::Member},
    prelude::*,
    utils::MessageBuilder,
};

8
use crate::config::CONFIG;
tec's avatar
tec committed
9
use crate::token_management::*;
tec's avatar
tec committed
10
11
12
13
14

macro_rules! e {
    ($error: literal, $x:expr) => {
        match $x {
            Ok(_) => (),
tec's avatar
tec committed
15
            Err(why) => error!($error, why),
tec's avatar
tec committed
16
17
18
19
20
21
22
23
24
25
26
        }
    };
}

pub fn new_member(ctx: &Context, mut new_member: Member) {
    let mut message = MessageBuilder::new();
    message.push("Nice to see you here ");
    message.mention(&new_member);
    message.push_line("! Would you care to introduce yourself?");
    message.push_line("If you're not sure where to start, perhaps you could tell us about your projects, your first computer…");
    message.push_line("You should also know that we follow the Freenode Channel Guidelines: https://freenode.net/changuide, and try to avoid defamatory content");
27
    if let Err(why) = CONFIG.welcome_channel.say(&ctx, message.build()) {
tec's avatar
tec committed
28
        error!("Error sending message: {:?}", why);
tec's avatar
tec committed
29
30
31
32
    }

    let mut message = MessageBuilder::new();
    message.push(format!("Say hi to {} in ", new_member.display_name()));
33
34
    message.mention(&CONFIG.welcome_channel);
    if let Err(why) = CONFIG.main_channel.say(&ctx, message.build()) {
tec's avatar
tec committed
35
        error!("Error sending message: {:?}", why);
tec's avatar
tec committed
36
37
    }

38
    if let Err(why) = new_member.add_role(&ctx.http, CONFIG.unregistered_member_role) {
tec's avatar
tec committed
39
        error!("Error adding user role: {:?}", why);
tec's avatar
tec committed
40
41
42
43
44
45
46
47
    };
}

pub struct Commands;
impl Commands {
    pub fn join(ctx: Context, msg: Message, _content: &str) {
        e!(
            "Unable to get user: {:?}",
48
            serenity::model::id::GuildId(CONFIG.server_id)
tec's avatar
tec committed
49
50
51
52
53
54
                .member(ctx.http.clone(), msg.author.id)
                .map(|member| new_member(&ctx, member))
        );
    }
    pub fn register(ctx: Context, msg: Message, content: &str) {
        let name = content;
tec's avatar
tec committed
55
        if name.len() <= 0 {
tec's avatar
tec committed
56
57
58
59
60
            e!(
                "Error sending message: {:?}",
                msg.channel_id
                    .say(&ctx.http, "Usage: !register <ucc username>")
            );
tec's avatar
tec committed
61
            return;
tec's avatar
tec committed
62
        }
tec's avatar
tec committed
63
64
65
66
67
68
        e!(
            "Error sending message: {:?}",
            // TODO convert to email
            msg.channel_id
                .say(&ctx.http, generate_token(&msg.author, name))
        );
tec's avatar
tec committed
69
70
71
72
        e!("Error deleting register message: {:?}", msg.delete(ctx));
    }
    pub fn verify(ctx: Context, msg: Message, content: &str) {
        let token = content;
tec's avatar
tec committed
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
        match parse_token(&msg.author, content) {
            Ok(name) => {
                e!(
                    "Unable to get member: {:?}",
                    serenity::model::id::GuildId(CONFIG.server_id)
                        .member(ctx.http.clone(), msg.author.id)
                        .map(|mut member| {
                            e!(
                                "Unable to remove role: {:?}",
                                member.remove_role(&ctx.http, CONFIG.unregistered_member_role)
                            );
                            e!(
                                "Unable to edit nickname: {:?}",
                                member.edit(&ctx.http, |m| {
                                    let mut rng = rand::thread_rng();
                                    m.nickname(format!(
                                        "{}, {}",
                                        name,
                                        [
                                            "The Big Cheese",
                                            "The One and Only",
                                            "The Exalted One",
                                            "not to be trusted",
                                            "The Scoundrel",
                                            "A big fish in a small pond",
                                        ][rng.gen_range(0, 5)]
                                    ));
                                    m
                                })
                            );
                            let new_msg = msg
                                .channel_id
                                .say(&ctx.http, "Verification succesful")
                                .expect("Error sending message");
                            e!(
                                "Error deleting register message: {:?}",
                                new_msg.delete(&ctx)
                            );
                        })
                );
            }
            Err(reason) => e!(
                "Error sending message: {:?}",
                msg.channel_id
                    .say(&ctx.http, format!("Verification error: {:?}", reason))
            ),
        }
        e!("Error deleting register message: {:?}", msg.delete(&ctx));
tec's avatar
tec committed
121
122
    }
}