From 0f952362b434917d10f7ef6a0e47bc3140e05cb0 Mon Sep 17 00:00:00 2001
From: Timothy du Heaume <timothy.duheaume@gmail.com>
Date: Sat, 1 Feb 2020 23:01:51 +0900
Subject: [PATCH] add a function to convert a reaction to a string
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reactions can be either a unicode character (e.g. 👾) or a discord custom emoji. This function converts both types to a string so they can be dealt with interchangeably.
---
 src/main.rs   |  7 +++++--
 src/util.rs   | 10 ++++++++++
 src/voting.rs | 14 ++++++++------
 3 files changed, 23 insertions(+), 8 deletions(-)
 create mode 100644 src/util.rs

diff --git a/src/main.rs b/src/main.rs
index de524f8..ee6321a 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 0000000..92a58cb
--- /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 9e4f4d5..d9dd4f6 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);
                 }
-- 
GitLab