-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathreaction_roles.rs
92 lines (82 loc) · 2.44 KB
/
reaction_roles.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::collections::HashMap;
use serenity::all::{Context as SerenityContext, MessageId, Reaction, ReactionType, RoleId};
use tracing::{debug, error};
use crate::{
ids::{
AI_ROLE_ID, ARCHIVE_ROLE_ID, DEVOPS_ROLE_ID, MOBILE_ROLE_ID, RESEARCH_ROLE_ID,
ROLES_MESSAGE_ID, SYSTEMS_ROLE_ID, WEB_ROLE_ID,
},
Data,
};
pub fn populate_data_with_reaction_roles(data: &mut Data) {
let roles = [
(
ReactionType::Unicode("📁".to_string()),
RoleId::new(ARCHIVE_ROLE_ID),
),
(
ReactionType::Unicode("📱".to_string()),
RoleId::new(MOBILE_ROLE_ID),
),
(
ReactionType::Unicode("⚙️".to_string()),
RoleId::new(SYSTEMS_ROLE_ID),
),
(
ReactionType::Unicode("🤖".to_string()),
RoleId::new(AI_ROLE_ID),
),
(
ReactionType::Unicode("📜".to_string()),
RoleId::new(RESEARCH_ROLE_ID),
),
(
ReactionType::Unicode("🚀".to_string()),
RoleId::new(DEVOPS_ROLE_ID),
),
(
ReactionType::Unicode("🌐".to_string()),
RoleId::new(WEB_ROLE_ID),
),
];
data.reaction_roles
.extend::<HashMap<ReactionType, RoleId>>(roles.into());
}
pub async fn handle_reaction(
ctx: &SerenityContext,
reaction: &Reaction,
data: &Data,
is_add: bool,
) {
if !is_relevant_reaction(reaction.message_id, &reaction.emoji, data) {
return;
}
debug!("Handling {:?} from {:?}.", reaction.emoji, reaction.user_id);
// TODO Log these errors
let Some(guild_id) = reaction.guild_id else {
return;
};
let Some(user_id) = reaction.user_id else {
return;
};
let Ok(member) = guild_id.member(ctx, user_id).await else {
return;
};
let Some(role_id) = data.reaction_roles.get(&reaction.emoji) else {
return;
};
let result = if is_add {
member.add_role(&ctx.http, *role_id).await
} else {
member.remove_role(&ctx.http, *role_id).await
};
if let Err(e) = result {
error!(
"Could not handle {:?} from {:?}. Error: {}",
reaction.emoji, reaction.user_id, e
);
}
}
fn is_relevant_reaction(message_id: MessageId, emoji: &ReactionType, data: &Data) -> bool {
message_id == MessageId::new(ROLES_MESSAGE_ID) && data.reaction_roles.contains_key(emoji)
}