From 9d80f19d71f4c24833217e0c33f76f0cd3b2febe Mon Sep 17 00:00:00 2001
From: tec <tec@ucc.gu.uwa.edu.au>
Date: Sun, 18 Aug 2019 22:12:39 +0800
Subject: [PATCH] Add identification of message type

---
 src/main.rs | 43 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 08263f8..21cb9a9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -85,11 +85,35 @@ impl EventHandler for Handler {
     }
 
     fn reaction_add(&self, ctx: Context, add_reaction: channel::Reaction) {
-        voting::reaction_add(ctx, add_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);
+                        }
+                        _ => {}
+                    }
+                }
+            }
+            Err(why) => error!("Failed to get react message {:?}", why),
+        }
     }
 
     fn reaction_remove(&self, ctx: Context, removed_reaction: channel::Reaction) {
-        voting::reaction_remove(ctx, removed_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);
+                        }
+                        _ => {}
+                    }
+                }
+            }
+            Err(why) => error!("Failed to get react message {:?}", why),
+        }
     }
 
     fn guild_member_addition(
@@ -138,3 +162,18 @@ fn main() {
         error!("Client error: {:?}", why);
     }
 }
+
+fn message_type(message: &Message) -> &'static str {
+    if message.embeds.len() > 0 {
+        let title: String = message.embeds[0].title.clone().unwrap();
+        let words_of_title: Vec<_> = title.splitn(2, ' ').collect();
+        let first_word_of_title = words_of_title[0];
+        return match first_word_of_title {
+            "Motion" => "motion",
+            "Poll" => "poll",
+            _ => "misc",
+        };
+    } else {
+        return "misc";
+    }
+}
-- 
GitLab