main.rs 12 KB
Newer Older
tec's avatar
tec committed
1
2
3
#[macro_use]
extern crate lazy_static;

tec's avatar
tec committed
4
5
#[macro_use]
extern crate log;
6
extern crate indexmap;
tec's avatar
tec committed
7
8
9
extern crate simplelog;
#[macro_use]
extern crate guard;
tec's avatar
tec committed
10
11
12
13
14

#[macro_use]
extern crate diesel;
extern crate ldap3;

tec's avatar
tec committed
15
use simplelog::*;
16
use std::fs::File;
tec's avatar
tec committed
17

tec's avatar
tec committed
18
use chrono::prelude::Utc;
tec's avatar
tec committed
19
20
21
22
23
24
use serenity::{
    model::{channel, channel::Message, gateway::Ready, guild::Member},
    prelude::*,
    utils::MessageBuilder,
};

25
26
#[macro_use]
mod util;
tec's avatar
tec committed
27
mod config;
tec's avatar
tec committed
28
29
mod database;
mod ldap;
tec's avatar
tec committed
30
31
mod reaction_roles;
mod token_management;
tec's avatar
tec committed
32
mod user_management;
tec's avatar
tec committed
33
mod voting;
34
use rand::seq::SliceRandom;
tec's avatar
tec committed
35

36
use config::{CONFIG, SECRETS};
37
use reaction_roles::{add_role_by_reaction, remove_role_by_reaction};
tec's avatar
tec committed
38
use util::get_string_from_react;
39

Tom Almeida's avatar
Tom Almeida committed
40
41
struct Handler;

42
43
44
45
46
47
48
49
50
51
52
53
54
pub const MENTION_RESPONSES: &[&str] = &[
    "Oh hello there",
    "Stop bothering me. I'm busy.",
    "You know, I'm trying to keep track of this place. I don't need any more distractions.",
    "Don't you have better things to do?",
    "(sigh) what now?",
    "Yes, yes, I know I'm brilliant",
    "What do I need to do to catch a break around here? Eh.",
    "Mmmmhmmm. I'm still around, don't mind me.",
    "You know, some people would consider this rude. Luckily I'm not one of those people. In fact, I'm not even a person.",
    "Perhaps try bothering someone else for a change."
];

tec's avatar
tec committed
55
56
57
58
59
60
61
impl EventHandler for Handler {
    // Set a handler for the `message` event - so that whenever a new message
    // is received - the closure (or function) passed will be called.
    //
    // Event handlers are dispatched through a threadpool, and so multiple
    // events can be dispatched simultaneously.
    fn message(&self, ctx: Context, msg: Message) {
62
        if !(msg.content.starts_with(&CONFIG.command_prefix)) {
tec's avatar
tec committed
63
            if msg.content.contains(&format!("<@!{}>", CONFIG.bot_id)) // desktop mentions
64
65
                || msg.content.contains(&format!("<@{}>", CONFIG.bot_id))
            // mobile mentions
tec's avatar
tec committed
66
            {
67
68
69
70
71
72
73
74
                send_message!(
                    msg.channel_id,
                    &ctx.http,
                    MENTION_RESPONSES
                        .choose(&mut rand::thread_rng())
                        .expect("We couldn't get any sass")
                );
            }
tec's avatar
tec committed
75
76
77
            return;
        }
        let message_content: Vec<_> = msg.content[1..].splitn(2, ' ').collect();
tec's avatar
tec committed
78
79
80
81
82
        let content = if message_content.len() > 1 {
            message_content[1]
        } else {
            ""
        };
tec's avatar
tec committed
83
        match message_content[0] {
84
            "say" => println!("{:#?}", msg.content),
tec's avatar
tec committed
85
86
87
88
            "register" => user_management::Commands::register(ctx, msg.clone(), content),
            "verify" => user_management::Commands::verify(ctx, msg.clone(), content),
            "profile" => user_management::Commands::profile(ctx, msg.clone(), content),
            "set" => user_management::Commands::set_info(ctx, msg.clone(), content),
tec's avatar
tec committed
89
            "clear" => user_management::Commands::clear_info(ctx, msg.clone(), content),
tec's avatar
tec committed
90
91
92
93
            "move" => voting::Commands::move_something(ctx, msg.clone(), content),
            "motion" => voting::Commands::motion(ctx, msg.clone(), content),
            "poll" => voting::Commands::poll(ctx, msg.clone(), content),
            "cowsay" => voting::Commands::cowsay(ctx, msg.clone(), content),
94
            "source" => {
95
96
97
                let mut mesg = MessageBuilder::new();
                mesg.push(
                    "You want to look at my insides!? Eurgh.\nJust kidding, you can go over ",
tec's avatar
tec committed
98
                );
tec's avatar
tec committed
99
100
                mesg.push_italic("every inch");
                mesg.push(" of me here: https://gitlab.ucc.asn.au/UCC/discord-bot 😉");
101
                send_message!(msg.channel_id, &ctx.http, mesg.build());
tec's avatar
tec committed
102
            }
tec's avatar
tec committed
103
            "help" => {
tec's avatar
tec committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
                // Plaintext version, keep in case IRC users kick up a fuss
                // let mut message = MessageBuilder::new();
                // message.push_line(format!(
                //     "Use {}move <action> to make a circular motion",
                //     &CONFIG.command_prefix
                // ));
                // message.push_line(format!(
                //     "Use {}poll <proposal> to see what people think about something",
                //     &CONFIG.command_prefix
                // ));
                // send_message!(msg.channel_id, &ctx.http, message.build());

                let result = msg.channel_id.send_message(&ctx.http, |m| {
                    m.embed(|embed| {
                        embed.colour(serenity::utils::Colour::DARK_GREY);
                        embed.title("Commands for the UCC Bot");
                        embed.field("About", "This is UCC's own little in-house bot, please treat it nicely :)", false);
                        embed.field("Commitee", "`!move <text>` to make a circular motion\n\
                                                 `!poll <text>` to get people's opinions on something", false);
                        embed.field("Account", "`!register <ucc username>` to link your Discord and UCC account\n\
                                                `!profile <user>` to get the profile of a user\n\
Ash's avatar
Ash committed
125
126
                                                `!set <bio|git|web|photo>` to set that property of _your_ profile\n\
                                                `!updateroles` to update your registered roles", false);
tec's avatar
tec committed
127
128
129
130
131
132
133
134
135
136
137
                        embed.field("Fun", "`!cowsay <text>` to have a cow say your words\n\
                                            with no `<text>` it'll give you a fortune 😉", false);
                        embed
                    });
                    m
                });
                if let Err(why) = result {
                    error!("Error sending help embed: {:?}", why);
                }
            }
            // undocumented (in !help) functins
138
139
140
141
142
143
144
145
            "logreact" => {
                e!("Error deleting logreact prompt: {:?}", msg.delete(&ctx));
                send_message!(
                    msg.channel_id,
                    &ctx.http,
                    "React to this to log the ID (for the next 5min)"
                );
            }
tec's avatar
tec committed
146
147
148
149
150
151
152
153
154
155
            "ldap" => send_message!(
                msg.channel_id,
                &ctx.http,
                format!("{:?}", ldap::ldap_search(message_content[1]))
            ),
            "tla" => send_message!(
                msg.channel_id,
                &ctx.http,
                format!("{:?}", ldap::tla_search(message_content[1]))
            ),
Ash's avatar
Ash committed
156
            "updateroles" => user_management::Commands::update_registered_role(ctx, msg),
tec's avatar
tec committed
157
158
159
160
161
            _ => send_message!(
                msg.channel_id,
                &ctx.http,
                format!("Unrecognised command. Try {}help", &CONFIG.command_prefix)
            ),
tec's avatar
tec committed
162
163
164
        }
    }

tec's avatar
tec committed
165
    fn reaction_add(&self, ctx: Context, add_reaction: channel::Reaction) {
tec's avatar
tec committed
166
        match add_reaction.message(&ctx.http) {
tec's avatar
tec committed
167
168
169
            Ok(message) => match get_message_type(&message) {
                MessageType::RoleReactMessage if add_reaction.user_id.0 != CONFIG.bot_id => {
                    add_role_by_reaction(&ctx, message, add_reaction);
170
                    return;
tec's avatar
tec committed
171
172
173
174
                }
                _ if message.author.id.0 != CONFIG.bot_id
                    || add_reaction.user_id == CONFIG.bot_id =>
                {
175
                    return
tec's avatar
tec committed
176
177
178
179
180
181
182
183
184
                }
                MessageType::Motion => voting::reaction_add(ctx, add_reaction),
                MessageType::LogReact => {
                    let react_user = add_reaction.user(&ctx).unwrap();
                    let react_as_string = get_string_from_react(&add_reaction.emoji);
                    if Utc::now().timestamp() - message.timestamp.timestamp() > 300 {
                        warn!(
                            "The logreact message {} just tried to use is too old",
                            react_user.name
tec's avatar
tec committed
185
                        );
186
                        return;
tec's avatar
tec committed
187
188
189
190
191
192
193
194
195
196
197
198
199
                    }
                    info!(
                        "The react {} just added is {:?}. In full: {:?}",
                        react_user.name, react_as_string, add_reaction.emoji
                    );
                    let mut msg = MessageBuilder::new();
                    msg.push_italic(react_user.name);
                    msg.push(format!(
                        " wanted to know that {} is represented by ",
                        add_reaction.emoji,
                    ));
                    msg.push_mono(react_as_string);
                    send_message!(message.channel_id, &ctx.http, msg.build());
tec's avatar
tec committed
200
                }
tec's avatar
tec committed
201
                _ => {}
202
            },
tec's avatar
tec committed
203
204
            Err(why) => error!("Failed to get react message {:?}", why),
        }
tec's avatar
tec committed
205
206
    }

tec's avatar
tec committed
207
    fn reaction_remove(&self, ctx: Context, removed_reaction: channel::Reaction) {
tec's avatar
tec committed
208
        match removed_reaction.message(&ctx.http) {
tec's avatar
tec committed
209
210
211
            Ok(message) => match get_message_type(&message) {
                MessageType::RoleReactMessage if removed_reaction.user_id != CONFIG.bot_id => {
                    remove_role_by_reaction(&ctx, message, removed_reaction);
212
                    return;
tec's avatar
tec committed
213
214
215
216
                }
                _ if message.author.id.0 != CONFIG.bot_id
                    || removed_reaction.user_id == CONFIG.bot_id =>
                {
217
                    return
tec's avatar
tec committed
218
                }
tec's avatar
tec committed
219
220
                MessageType::Motion => voting::reaction_remove(ctx, removed_reaction),
                _ => {}
221
            },
tec's avatar
tec committed
222
223
            Err(why) => error!("Failed to get react message {:?}", why),
        }
tec's avatar
tec committed
224
225
    }

tec's avatar
tec committed
226
227
228
229
    fn guild_member_addition(
        &self,
        ctx: Context,
        _guild_id: serenity::model::id::GuildId,
tec's avatar
tec committed
230
        the_new_member: Member,
tec's avatar
tec committed
231
    ) {
tec's avatar
tec committed
232
        user_management::new_member(&ctx, the_new_member);
tec's avatar
tec committed
233
234
235
236
237
238
239
240
    }

    // Set a handler to be called on the `ready` event. This is called when a
    // shard is booted, and a READY payload is sent by Discord. This payload
    // contains data like the current user's guild Ids, current user data,
    // private channels, and more.
    //
    // In this case, just print what the current user's username is.
241
    fn ready(&self, ctx: Context, ready: Ready) {
tec's avatar
tec committed
242
        info!("{} is connected!", ready.user.name);
tec's avatar
tec committed
243
        reaction_roles::sync_all_role_reactions(&ctx);
244
245
246
    }

    fn resume(&self, ctx: Context, _: serenity::model::event::ResumedEvent) {
tec's avatar
tec committed
247
        reaction_roles::sync_all_role_reactions(&ctx);
tec's avatar
tec committed
248
249
250
251
    }
}

fn main() {
tec's avatar
tec committed
252
253
254
255
256
257
258
259
260
    CombinedLogger::init(vec![
        TermLogger::new(LevelFilter::Info, Config::default(), TerminalMode::Mixed).unwrap(),
        WriteLogger::new(
            LevelFilter::Info,
            Config::default(),
            File::create("ucc-bot.log").unwrap(),
        ),
    ])
    .unwrap();
261

tec's avatar
tec committed
262
263
264
    // Create a new instance of the Client, logging in as a bot. This will
    // automatically prepend your bot token with "Bot ", which is a requirement
    // by Discord for bot users.
265
    let mut client = Client::new(&SECRETS.discord_token, Handler).expect("Err creating client");
tec's avatar
tec committed
266
267
268
269
270
271

    // Finally, start a single shard, and start listening to events.
    //
    // Shards will automatically attempt to reconnect, and will perform
    // exponential backoff until it reconnects.
    if let Err(why) = client.start() {
tec's avatar
tec committed
272
        error!("Client error: {:?}", why);
tec's avatar
tec committed
273
274
    }
}
tec's avatar
tec committed
275

276
277
278
279
#[derive(Debug, PartialEq)]
enum MessageType {
    Motion,
    Role,
280
    RoleReactMessage,
281
282
    LogReact,
    Poll,
tec's avatar
tec committed
283
    Misc,
284
285
286
}

fn get_message_type(message: &Message) -> MessageType {
tec's avatar
tec committed
287
288
289
290
291
    if CONFIG
        .react_role_messages
        .iter()
        .any(|rrm| rrm.message == message.id)
    {
292
293
        return MessageType::RoleReactMessage;
    }
Timothy du Heaume's avatar
Timothy du Heaume committed
294
    if message.embeds.is_empty() {
tec's avatar
tec committed
295
        // Get first word of message
tec's avatar
tec committed
296
        return match message.content.splitn(2, ' ').next().unwrap() {
297
298
299
            "Role" => MessageType::Role,
            "React" => MessageType::LogReact,
            _ => MessageType::Misc,
tec's avatar
tec committed
300
        };
tec's avatar
tec committed
301
    }
tec's avatar
tec committed
302
303
304
    let title: String = message.embeds[0].title.clone().unwrap();
    let words_of_title: Vec<_> = title.splitn(2, ' ').collect();
    let first_word_of_title = words_of_title[0];
Timothy du Heaume's avatar
Timothy du Heaume committed
305
    match first_word_of_title {
306
307
308
        "Motion" => MessageType::Motion,
        "Poll" => MessageType::Poll,
        _ => MessageType::Misc,
Timothy du Heaume's avatar
Timothy du Heaume committed
309
    }
tec's avatar
tec committed
310
}