user_management.rs 4.23 KB
Newer Older
Ash's avatar
Ash committed
1
use rand::seq::SliceRandom;
tec's avatar
tec committed
2
3
4
5
6
7
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
    };
}

Ash's avatar
Ash committed
43
44
45
46
47
48
49
50
51
pub const RANDOM_NICKNAMES: &[&str] = &[
    "The Big Cheese",
    "The One and Only",
    "The Exalted One",
    "not to be trusted",
    "The Scoundrel",
    "A big fish in a small pond",
];

tec's avatar
tec committed
52
53
pub struct Commands;
impl Commands {
tec's avatar
tec committed
54
    pub fn register(ctx: Context, msg: Message, account_name: &str) {
Timothy du Heaume's avatar
Timothy du Heaume committed
55
        if account_name.is_empty() {
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
        e!(
            "Error sending message: {:?}",
            // TODO convert to email
            msg.channel_id
tec's avatar
tec committed
67
               .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
68
        );
tec's avatar
tec committed
69
70
        e!("Error deleting register message: {:?}", msg.delete(ctx));
    }
tec's avatar
tec committed
71
72
    pub fn verify(ctx: Context, msg: Message, token: &str) {
        match parse_token(&msg.author, token) {
tec's avatar
tec committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
            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| {
                                    m.nickname(format!(
Ash's avatar
Ash committed
87
                                        "{}, {:?}",
tec's avatar
tec committed
88
                                        name,
Ash's avatar
Ash committed
89
                                        RANDOM_NICKNAMES.choose(&mut rand::thread_rng())
tec's avatar
tec committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
                                    ));
                                    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
112
113
    }
}