user_management.rs 3.77 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 10 11 12 13

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

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");
26
    if let Err(why) = CONFIG.welcome_channel.say(&ctx, message.build()) {
tec's avatar
tec committed
27
        error!("Error sending message: {:?}", why);
tec's avatar
tec committed
28 29 30 31
    }

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

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

pub struct Commands;
impl Commands {
    pub fn join(ctx: Context, msg: Message, _content: &str) {
        e!(
            "Unable to get user: {:?}",
47
            serenity::model::id::GuildId(CONFIG.server_id)
tec's avatar
tec committed
48 49 50 51 52 53
                .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;
54
        if 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>")
            );
60
            return;
tec's avatar
tec committed
61
        }
62 63
        e!(
            "Unable to get member: {:?}",
64
            serenity::model::id::GuildId(CONFIG.server_id)
65 66 67 68
                .member(ctx.http.clone(), msg.author.id)
                .map(|mut member| {
                    e!(
                        "Unable to remove role: {:?}",
69
                        member.remove_role(&ctx.http, CONFIG.unregistered_member_role)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                    );
                    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
                            })
                            .map(|()| {
                                e!(
                                    "Unable to add role: {:?}",
93
                                    member.add_role(&ctx.http, CONFIG.registered_member_role)
94 95 96 97 98 99
                                );
                            })
                    );
                })
        );
        e!("Error deleting register message: {:?}", msg.delete(ctx));
tec's avatar
tec committed
100 101
    }
}