diff --git a/src/main.rs b/src/main.rs index 21cb9a9bf232ae65cef490b17f693e457759c315..16bd90914b7d455d01b0f16748821291703d2f41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,13 +87,14 @@ impl EventHandler for Handler { fn reaction_add(&self, ctx: Context, add_reaction: channel::Reaction) { match add_reaction.message(&ctx.http) { Ok(message) => { - if message.author.id.0 == config::BOT_ID { - match message_type(&message) { - "motion" => { - voting::reaction_add(ctx, add_reaction); - } - _ => {} + if message.author.id.0 != config::BOT_ID { + return + } + match message_type(&message) { + "motion" => { + voting::reaction_add(ctx, add_reaction); } + _ => {} } } Err(why) => error!("Failed to get react message {:?}", why), @@ -103,13 +104,14 @@ impl EventHandler for Handler { fn reaction_remove(&self, ctx: Context, removed_reaction: channel::Reaction) { match removed_reaction.message(&ctx.http) { Ok(message) => { - if message.author.id.0 == config::BOT_ID { - match message_type(&message) { - "motion" => { - voting::reaction_remove(ctx, removed_reaction); - } - _ => {} + if message.author.id.0 != config::BOT_ID { + return + } + match message_type(&message) { + "motion" => { + voting::reaction_remove(ctx, removed_reaction); } + _ => {} } } Err(why) => error!("Failed to get react message {:?}", why), diff --git a/src/voting.rs b/src/voting.rs index f3b9b10b2d79b8d28579e016345bf832e8ff52d2..8f812464023d6dea9a66ba6cb72d5aba969e3114 100644 --- a/src/voting.rs +++ b/src/voting.rs @@ -343,27 +343,46 @@ fn update_motion( pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { match add_reaction.message(&ctx.http) { Ok(mut message) => { - if message.author.id.0 == config::BOT_ID { - if let Ok(user) = add_reaction.user(&ctx) { - match user.has_role(&ctx, config::SERVER_ID, config::VOTE_ROLE) { - Ok(true) => { - for react in - [config::FOR_VOTE, config::AGAINST_VOTE, config::ABSTAIN_VOTE] - .iter() - .filter(|r| r != &&add_reaction.emoji.as_data().as_str()) + if let Ok(user) = add_reaction.user(&ctx) { + match user.has_role(&ctx, config::SERVER_ID, config::VOTE_ROLE) { + Ok(true) => { + for react in [config::FOR_VOTE, config::AGAINST_VOTE, config::ABSTAIN_VOTE] + .iter() + .filter(|r| r != &&add_reaction.emoji.as_data().as_str()) + { + for a_user in message.reaction_users(&ctx, *react, None, None).unwrap() { - for a_user in - message.reaction_users(&ctx, *react, None, None).unwrap() - { - if a_user.id.0 == user.id.0 { - if let Err(why) = add_reaction.delete(&ctx) { - error!("Error deleting react: {:?}", why); - }; - return; - } + if a_user.id.0 == user.id.0 { + if let Err(why) = add_reaction.delete(&ctx) { + error!("Error deleting react: {:?}", why); + }; + return; } } - if !config::ALLOWED_REACTS + } + if !config::ALLOWED_REACTS.contains(&add_reaction.emoji.as_data().as_str()) + { + if let Err(why) = add_reaction.delete(&ctx) { + error!("Error deleting react: {:?}", why); + }; + return; + } + if user.id.0 != config::BOT_ID { + let mut motion_info = get_cached_motion(&ctx, &message); + if let Some(vote) = motion_info + .votes + .get_mut(add_reaction.emoji.as_data().as_str()) + { + vote.retain(|u| u.id != user.id); + vote.push(user.clone()); + } + set_cached_motion(&message.id, motion_info); + update_motion(&ctx, &mut message, &user, "add", add_reaction); + } + } + Ok(false) => { + if user.id.0 != config::BOT_ID { + if ![config::APPROVE_REACT, config::DISAPPROVE_REACT] .contains(&add_reaction.emoji.as_data().as_str()) { if let Err(why) = add_reaction.delete(&ctx) { @@ -371,35 +390,11 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { }; return; } - if user.id.0 != config::BOT_ID { - let mut motion_info = get_cached_motion(&ctx, &message); - if let Some(vote) = motion_info - .votes - .get_mut(add_reaction.emoji.as_data().as_str()) - { - vote.retain(|u| u.id != user.id); - vote.push(user.clone()); - } - set_cached_motion(&message.id, motion_info); - update_motion(&ctx, &mut message, &user, "add", add_reaction); - } - } - Ok(false) => { - if user.id.0 != config::BOT_ID { - if ![config::APPROVE_REACT, config::DISAPPROVE_REACT] - .contains(&add_reaction.emoji.as_data().as_str()) - { - if let Err(why) = add_reaction.delete(&ctx) { - error!("Error deleting react: {:?}", why); - }; - return; - } - } - } - Err(why) => { - error!("Error getting user role: {:?}", why); } } + Err(why) => { + error!("Error getting user role: {:?}", why); + } } } } @@ -412,18 +407,16 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { pub fn reaction_remove(ctx: Context, removed_reaction: channel::Reaction) { match removed_reaction.message(&ctx.http) { Ok(mut message) => { - if message.author.id.0 == config::BOT_ID { - if let Ok(user) = removed_reaction.user(&ctx) { - let mut motion_info = get_cached_motion(&ctx, &message); - if let Some(vote) = motion_info - .votes - .get_mut(removed_reaction.emoji.as_data().as_str()) - { - vote.retain(|u| u.id != user.id); - } - set_cached_motion(&message.id, motion_info); - update_motion(&ctx, &mut message, &user, "remove", removed_reaction); + if let Ok(user) = removed_reaction.user(&ctx) { + let mut motion_info = get_cached_motion(&ctx, &message); + if let Some(vote) = motion_info + .votes + .get_mut(removed_reaction.emoji.as_data().as_str()) + { + vote.retain(|u| u.id != user.id); } + set_cached_motion(&message.id, motion_info); + update_motion(&ctx, &mut message, &user, "remove", removed_reaction); } } Err(why) => {