Commit 74f31847 authored by Timothy du Heaume's avatar Timothy du Heaume

let users add themselves to roles by reacting to a post

parent 7c6aceb8
use std::collections::HashMap;
use serde::Deserialize; use serde::Deserialize;
use serenity::model::id; use serenity::model::id;
use std::fs; use std::fs;
...@@ -27,6 +28,7 @@ pub struct UccbotConfig { ...@@ -27,6 +28,7 @@ pub struct UccbotConfig {
pub approve_react: String, pub approve_react: String,
pub disapprove_react: String, pub disapprove_react: String,
pub unsure_react: String, pub unsure_react: String,
pub react_role_messages: Vec<ReactionMapping>,
} }
impl UccbotConfig { impl UccbotConfig {
...@@ -41,3 +43,9 @@ impl UccbotConfig { ...@@ -41,3 +43,9 @@ impl UccbotConfig {
] ]
} }
} }
#[derive(Debug, Deserialize, Clone)]
pub struct ReactionMapping {
pub message: serenity::model::id::MessageId,
pub mapping: HashMap<String, id::RoleId>,
}
...@@ -18,3 +18,10 @@ abstain_vote: "🙊" ...@@ -18,3 +18,10 @@ abstain_vote: "🙊"
approve_react: "⬆" approve_react: "⬆"
disapprove_react: "⬇" disapprove_react: "⬇"
unsure_react: "❔" unsure_react: "❔"
react_role_messages:
- message: 673400351277187072
mapping:
🐊: 609708723006472198 # Autonomous Alligators
🐃: 609708839243087892 # Bipedal Bison
🦆: 609708889763479562 # Omnipresent Ostriches
...@@ -18,9 +18,11 @@ mod config; ...@@ -18,9 +18,11 @@ mod config;
mod user_management; mod user_management;
mod voting; mod voting;
mod util; mod util;
mod reaction_roles;
use config::CONFIG; use config::CONFIG;
use util::get_string_from_react; use util::get_string_from_react;
use reaction_roles::{add_role_by_reaction, remove_role_by_reaction};
macro_rules! e { macro_rules! e {
($error: literal, $x:expr) => { ($error: literal, $x:expr) => {
...@@ -102,10 +104,15 @@ impl EventHandler for Handler { ...@@ -102,10 +104,15 @@ impl EventHandler for Handler {
fn reaction_add(&self, ctx: Context, add_reaction: channel::Reaction) { fn reaction_add(&self, ctx: Context, add_reaction: channel::Reaction) {
match add_reaction.message(&ctx.http) { match add_reaction.message(&ctx.http) {
Ok(message) => { Ok(message) => {
let message_type = get_message_type(&message);
if message_type == MessageType::RoleReactMessage {
add_role_by_reaction(ctx, message, add_reaction);
return;
}
if message.author.id.0 != CONFIG.bot_id || add_reaction.user_id == CONFIG.bot_id { if message.author.id.0 != CONFIG.bot_id || add_reaction.user_id == CONFIG.bot_id {
return; return;
} }
match get_message_type(&message) { match message_type {
MessageType::Motion => { MessageType::Motion => {
voting::reaction_add(ctx, add_reaction); voting::reaction_add(ctx, add_reaction);
} }
...@@ -146,11 +153,16 @@ impl EventHandler for Handler { ...@@ -146,11 +153,16 @@ impl EventHandler for Handler {
fn reaction_remove(&self, ctx: Context, removed_reaction: channel::Reaction) { fn reaction_remove(&self, ctx: Context, removed_reaction: channel::Reaction) {
match removed_reaction.message(&ctx.http) { match removed_reaction.message(&ctx.http) {
Ok(message) => { Ok(message) => {
let message_type = get_message_type(&message);
if message_type == MessageType::RoleReactMessage {
remove_role_by_reaction(ctx, message, removed_reaction);
return;
}
if message.author.id.0 != CONFIG.bot_id || removed_reaction.user_id == CONFIG.bot_id if message.author.id.0 != CONFIG.bot_id || removed_reaction.user_id == CONFIG.bot_id
{ {
return; return;
} }
match get_message_type(&message) { match message_type {
MessageType::Motion => { MessageType::Motion => {
voting::reaction_remove(ctx, removed_reaction); voting::reaction_remove(ctx, removed_reaction);
} }
...@@ -213,12 +225,16 @@ fn main() { ...@@ -213,12 +225,16 @@ fn main() {
enum MessageType { enum MessageType {
Motion, Motion,
Role, Role,
RoleReactMessage,
LogReact, LogReact,
Poll, Poll,
Misc Misc
} }
fn get_message_type(message: &Message) -> MessageType { fn get_message_type(message: &Message) -> MessageType {
if CONFIG.react_role_messages.iter().any(|rrm| rrm.message == message.id) {
return MessageType::RoleReactMessage;
}
if message.embeds.len() <= 0 { if message.embeds.len() <= 0 {
// Get first word of message // Get first word of message
return match message.content.splitn(2, ' ').next().unwrap() { return match message.content.splitn(2, ' ').next().unwrap() {
......
use serenity::{
model::{channel::Message, channel::Reaction},
client::Context
};
use crate::util::get_string_from_react;
use crate::config::CONFIG;
#[derive(Debug, Clone)] pub fn add_role_by_reaction(ctx: Context, msg: Message, added_reaction: Reaction) {
struct ReactionMapping { CONFIG.react_role_messages.iter().find(|rrm| rrm.message == msg.id).and_then(|reaction_mapping| {
mapping: HashMap<serenity::model::id::EmojiId, serenity::model::id::RoleId>, let react_as_string = get_string_from_react(added_reaction.emoji);
return reaction_mapping.mapping.get(&react_as_string);
}).and_then(|role_id|{
return ctx.http.add_member_role(CONFIG.server_id, *msg.author.id.as_u64(), *role_id.as_u64()).ok();
});
} }
lazy_static! { pub fn remove_role_by_reaction(ctx: Context, msg: Message, removed_reaction: Reaction) {
static ref REACTIONS_CACHE: Mutex<HashMap<serenity::model::id::MessageId, ReactionMapping>> = CONFIG.react_role_messages.iter().find(|rrm| rrm.message == msg.id).and_then(|reaction_mapping| {
Mutex::new(HashMap::new()); let react_as_string = get_string_from_react(removed_reaction.emoji);
return reaction_mapping.mapping.get(&react_as_string);
}).and_then(|role_id|{
return ctx.http.remove_member_role(CONFIG.server_id, *msg.author.id.as_u64(), *role_id.as_u64()).ok();
});
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment