util.rs 1.18 KB
Newer Older
1
use serenity::model::{channel::ReactionType, guild::PartialGuild};
2

3
pub fn get_string_from_react(react: &ReactionType) -> String {
4
    match react {
Timothy du Heaume's avatar
Timothy du Heaume committed
5
6
        ReactionType::Custom {
            name: Some(name), ..
7
        } => name.to_string(),
Timothy du Heaume's avatar
Timothy du Heaume committed
8
        ReactionType::Custom { id, name: None, .. } => id.to_string(),
9
        ReactionType::Unicode(name) => name.to_string(),
10
11
12
        _ => format!("Unrecognised reaction type: {:?}", react),
    }
}
13
14

pub fn get_react_from_string(string: String, guild: PartialGuild) -> ReactionType {
Timothy du Heaume's avatar
Timothy du Heaume committed
15
16
17
18
19
20
    guild
        .emojis
        .values()
        .find(|e| e.name == string)
        .map_or_else(
            || ReactionType::from(string), // unicode emoji
21
            |custom_emoji| ReactionType::from(custom_emoji.clone()),
Timothy du Heaume's avatar
Timothy du Heaume committed
22
        )
23
}
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#[macro_use]
macro_rules! e {
    ($error: literal, $x:expr) => {
        match $x {
            Ok(_) => (),
            Err(why) => error!($error, why),
        }
    };
}

#[macro_use]
macro_rules! send_message {
    ($chan:expr, $context:expr, $message:expr) => {
        match $chan
            .say($context, $message) {
            Ok(_) => (),
            Err(why) => error!("Error sending message: {:?}", why),
        }
    };
}