diff --git a/src/main.rs b/src/main.rs index de524f812bb3e15db5fc556cfbeeac688a11cfd5..ee6321aa391a70ca4322df56960103349968611e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,8 +17,10 @@ use serenity::{ mod config; mod user_management; mod voting; +mod util; use config::CONFIG; +use util::get_string_from_react; macro_rules! e { ($error: literal, $x:expr) => { @@ -109,6 +111,7 @@ impl EventHandler for Handler { } "logreact" => { let react_user = add_reaction.user(&ctx).unwrap(); + let react_as_string = get_string_from_react(add_reaction.emoji.clone()); if Utc::now().timestamp() - message.timestamp.timestamp() > 300 { warn!( "The logreact message {} just tried to use is too old", @@ -119,7 +122,7 @@ impl EventHandler for Handler { info!( "The react {} just added is {:?}", react_user.name, - add_reaction.emoji.as_data() + react_as_string ); let mut msg = MessageBuilder::new(); msg.push_italic(react_user.name); @@ -127,7 +130,7 @@ impl EventHandler for Handler { " wanted to know that {} is represented by ", add_reaction.emoji, )); - msg.push_mono(add_reaction.emoji.as_data()); + msg.push_mono(react_as_string); e!( "Error sending message: {:?}", message.channel_id.say(&ctx.http, msg.build()) diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000000000000000000000000000000000000..92a58cba6f9aa45f00bacff78791f8f52631f5d5 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,10 @@ +use serenity::model::channel::ReactionType; + +pub fn get_string_from_react(react: ReactionType) -> String { + match react { + ReactionType::Custom {animated: _, id: _, name: Some(name)} => name, + ReactionType::Custom {animated: _, id, name: None} => id.to_string(), + ReactionType::Unicode(name) => name, + _ => format!("Unrecognised reaction type: {:?}", react), + } +} diff --git a/src/voting.rs b/src/voting.rs index 9e4f4d5b60c9e6a15aef8a330e2a57e69e21a771..d9dd4f6bfa219b876abb6d490ece615786adb26b 100644 --- a/src/voting.rs +++ b/src/voting.rs @@ -7,6 +7,7 @@ use std::collections::HashMap; use std::sync::Mutex; use crate::config::CONFIG; +use crate::util::get_string_from_react; macro_rules! e { ($error: literal, $x:expr) => { @@ -254,7 +255,7 @@ fn update_motion( " {:10} {:6} {} on {}", user.name, change, - reaction.emoji.as_data().as_str(), + get_string_from_react(reaction.emoji), topic ); @@ -341,6 +342,7 @@ fn update_motion( } pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { + let react_as_string = get_string_from_react(add_reaction.emoji.clone()); match add_reaction.message(&ctx.http) { Ok(mut message) => { if let Ok(user) = add_reaction.user(&ctx) { @@ -349,7 +351,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { // remove vote if already voted for react in [CONFIG.for_vote.to_string(), CONFIG.against_vote.to_string(), CONFIG.abstain_vote.to_string()] .iter() - .filter(|r| r != &&add_reaction.emoji.as_data().as_str()) + .filter(|r| r != &&react_as_string) { for a_user in message.reaction_users(&ctx, react.as_str(), None, None).unwrap() { @@ -362,7 +364,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { } } // remove 'illegal' reacts - if !CONFIG.allowed_reacts().contains(&add_reaction.emoji.as_data()) + if !CONFIG.allowed_reacts().contains(&react_as_string) { if let Err(why) = add_reaction.delete(&ctx) { error!("Error deleting react: {:?}", why); @@ -373,7 +375,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { 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()) + .get_mut(&react_as_string) { vote.retain(|u| u.id != user.id); vote.push(user.clone()); @@ -383,7 +385,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { } Ok(false) => { if ![CONFIG.approve_react.to_string(), CONFIG.disapprove_react.to_string()] - .contains(&add_reaction.emoji.as_data()) + .contains(&react_as_string) { if let Err(why) = add_reaction.delete(&ctx) { error!("Error deleting react: {:?}", why); @@ -410,7 +412,7 @@ pub fn reaction_remove(ctx: Context, removed_reaction: channel::Reaction) { 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()) + .get_mut(&get_string_from_react(removed_reaction.emoji.clone())) { vote.retain(|u| u.id != user.id); }