user_management.rs 4.79 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
                .member(ctx.http.clone(), msg.author.id)
                .map(|member| new_member(&ctx, member))
        );
    }
tec's avatar
tec committed
53
54
    pub fn register(ctx: Context, msg: Message, account_name: &str) {
        if account_name.len() <= 0 {
tec's avatar
tec committed
55
56
57
58
59
            e!(
                "Error sending message: {:?}",
                msg.channel_id
                    .say(&ctx.http, "Usage: !register <ucc username>")
            );
tec's avatar
tec committed
60
            return;
tec's avatar
tec committed
61
        }
tec's avatar
tec committed
62
63
64
65
        e!(
            "Error sending message: {:?}",
            // TODO convert to email
            msg.channel_id
tec's avatar
tec committed
66
               .say(&ctx.http, format!("Hey {} here's that token you ordered: {}\nIf this wasn't you just ignore this.", account_name, generate_token(&msg.author, account_name)))
tec's avatar
tec committed
67
        );
tec's avatar
tec committed
68
69
        e!("Error deleting register message: {:?}", msg.delete(ctx));
    }
tec's avatar
tec committed
70
71
    pub fn verify(ctx: Context, msg: Message, token: &str) {
        match parse_token(&msg.author, token) {
tec's avatar
tec committed
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
            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
119
120
    }
}