Commit 0f952362 authored by Timothy du Heaume's avatar Timothy du Heaume

add a function to convert a reaction to a string

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.
parent a6bba31d
...@@ -17,8 +17,10 @@ use serenity::{ ...@@ -17,8 +17,10 @@ use serenity::{
mod config; mod config;
mod user_management; mod user_management;
mod voting; mod voting;
mod util;
use config::CONFIG; use config::CONFIG;
use util::get_string_from_react;
macro_rules! e { macro_rules! e {
($error: literal, $x:expr) => { ($error: literal, $x:expr) => {
...@@ -109,6 +111,7 @@ impl EventHandler for Handler { ...@@ -109,6 +111,7 @@ impl EventHandler for Handler {
} }
"logreact" => { "logreact" => {
let react_user = add_reaction.user(&ctx).unwrap(); 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 { if Utc::now().timestamp() - message.timestamp.timestamp() > 300 {
warn!( warn!(
"The logreact message {} just tried to use is too old", "The logreact message {} just tried to use is too old",
...@@ -119,7 +122,7 @@ impl EventHandler for Handler { ...@@ -119,7 +122,7 @@ impl EventHandler for Handler {
info!( info!(
"The react {} just added is {:?}", "The react {} just added is {:?}",
react_user.name, react_user.name,
add_reaction.emoji.as_data() react_as_string
); );
let mut msg = MessageBuilder::new(); let mut msg = MessageBuilder::new();
msg.push_italic(react_user.name); msg.push_italic(react_user.name);
...@@ -127,7 +130,7 @@ impl EventHandler for Handler { ...@@ -127,7 +130,7 @@ impl EventHandler for Handler {
" wanted to know that {} is represented by ", " wanted to know that {} is represented by ",
add_reaction.emoji, add_reaction.emoji,
)); ));
msg.push_mono(add_reaction.emoji.as_data()); msg.push_mono(react_as_string);
e!( e!(
"Error sending message: {:?}", "Error sending message: {:?}",
message.channel_id.say(&ctx.http, msg.build()) message.channel_id.say(&ctx.http, msg.build())
......
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),
}
}
...@@ -7,6 +7,7 @@ use std::collections::HashMap; ...@@ -7,6 +7,7 @@ use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::util::get_string_from_react;
macro_rules! e { macro_rules! e {
($error: literal, $x:expr) => { ($error: literal, $x:expr) => {
...@@ -254,7 +255,7 @@ fn update_motion( ...@@ -254,7 +255,7 @@ fn update_motion(
" {:10} {:6} {} on {}", " {:10} {:6} {} on {}",
user.name, user.name,
change, change,
reaction.emoji.as_data().as_str(), get_string_from_react(reaction.emoji),
topic topic
); );
...@@ -341,6 +342,7 @@ fn update_motion( ...@@ -341,6 +342,7 @@ fn update_motion(
} }
pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { 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) { match add_reaction.message(&ctx.http) {
Ok(mut message) => { Ok(mut message) => {
if let Ok(user) = add_reaction.user(&ctx) { if let Ok(user) = add_reaction.user(&ctx) {
...@@ -349,7 +351,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { ...@@ -349,7 +351,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
// remove vote if already voted // remove vote if already voted
for react in [CONFIG.for_vote.to_string(), CONFIG.against_vote.to_string(), CONFIG.abstain_vote.to_string()] for react in [CONFIG.for_vote.to_string(), CONFIG.against_vote.to_string(), CONFIG.abstain_vote.to_string()]
.iter() .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() 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) { ...@@ -362,7 +364,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
} }
} }
// remove 'illegal' reacts // 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) { if let Err(why) = add_reaction.delete(&ctx) {
error!("Error deleting react: {:?}", why); error!("Error deleting react: {:?}", why);
...@@ -373,7 +375,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { ...@@ -373,7 +375,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
let mut motion_info = get_cached_motion(&ctx, &message); let mut motion_info = get_cached_motion(&ctx, &message);
if let Some(vote) = motion_info if let Some(vote) = motion_info
.votes .votes
.get_mut(add_reaction.emoji.as_data().as_str()) .get_mut(&react_as_string)
{ {
vote.retain(|u| u.id != user.id); vote.retain(|u| u.id != user.id);
vote.push(user.clone()); vote.push(user.clone());
...@@ -383,7 +385,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { ...@@ -383,7 +385,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
} }
Ok(false) => { Ok(false) => {
if ![CONFIG.approve_react.to_string(), CONFIG.disapprove_react.to_string()] 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) { if let Err(why) = add_reaction.delete(&ctx) {
error!("Error deleting react: {:?}", why); error!("Error deleting react: {:?}", why);
...@@ -410,7 +412,7 @@ pub fn reaction_remove(ctx: Context, removed_reaction: channel::Reaction) { ...@@ -410,7 +412,7 @@ pub fn reaction_remove(ctx: Context, removed_reaction: channel::Reaction) {
let mut motion_info = get_cached_motion(&ctx, &message); let mut motion_info = get_cached_motion(&ctx, &message);
if let Some(vote) = motion_info if let Some(vote) = motion_info
.votes .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); vote.retain(|u| u.id != user.id);
} }
......
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