diff --git a/src/config.rs b/src/config.rs index d1dcff5bac1bc530ed3f9c404601733ba8160e73..452553cb208a676073def9585ab2b9407410ec90 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,8 @@ -use std::collections::HashMap; use serde::Deserialize; +use serde_yaml; use serenity::model::id; +use std::collections::HashMap; use std::fs; -use serde_yaml; lazy_static! { static ref CONFIG_FILE: String = fs::read_to_string("config.yml").unwrap(); diff --git a/src/main.rs b/src/main.rs index 235e7dd6e4fd95b9f04f202e7a042fdce3d61057..071124931ba2692e7cdeef3cf3e7c785858b7dfa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -163,11 +163,8 @@ impl EventHandler for Handler { { return; } - match message_type { - MessageType::Motion => { - voting::reaction_remove(ctx, removed_reaction); - } - _ => {} + if message_type == MessageType::Motion { + voting::reaction_remove(ctx, removed_reaction); } } Err(why) => error!("Failed to get react message {:?}", why), @@ -245,7 +242,7 @@ fn get_message_type(message: &Message) -> MessageType { { return MessageType::RoleReactMessage; } - if message.embeds.len() <= 0 { + if message.embeds.is_empty() { // Get first word of message return match message.content.splitn(2, ' ').next().unwrap() { "Role" => MessageType::Role, @@ -256,9 +253,9 @@ fn get_message_type(message: &Message) -> MessageType { 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 { + match first_word_of_title { "Motion" => MessageType::Motion, "Poll" => MessageType::Poll, _ => MessageType::Misc, - }; + } } diff --git a/src/reaction_roles.rs b/src/reaction_roles.rs index 3517c84ff892a26921ca7cfb17b172b8f1408d2e..8add1b40a79672c867c3bf704fb3b54e0a796948 100644 --- a/src/reaction_roles.rs +++ b/src/reaction_roles.rs @@ -14,13 +14,12 @@ pub fn add_role_by_reaction(ctx: Context, msg: Message, added_reaction: Reaction .find(|rrm| rrm.message == msg.id) .and_then(|reaction_mapping| { let react_as_string = get_string_from_react(added_reaction.emoji); - return reaction_mapping.mapping.get(&react_as_string); + reaction_mapping.mapping.get(&react_as_string) }) .and_then(|role_id| { - return ctx - .http + ctx.http .add_member_role(CONFIG.server_id, *msg.author.id.as_u64(), *role_id.as_u64()) - .ok(); + .ok() }); } @@ -31,13 +30,12 @@ pub fn remove_role_by_reaction(ctx: Context, msg: Message, removed_reaction: Rea .find(|rrm| rrm.message == msg.id) .and_then(|reaction_mapping| { let react_as_string = get_string_from_react(removed_reaction.emoji); - return reaction_mapping.mapping.get(&react_as_string); + reaction_mapping.mapping.get(&react_as_string) }) .and_then(|role_id| { - return ctx - .http + ctx.http .remove_member_role(CONFIG.server_id, *msg.author.id.as_u64(), *role_id.as_u64()) - .ok(); + .ok() }); } @@ -85,7 +83,7 @@ fn get_all_role_reaction_message( )> { let guild = ctx.http.get_guild(CONFIG.server_id).unwrap(); let channels = ctx.http.get_channels(*guild.id.as_u64()).unwrap(); - return channels + channels .iter() .flat_map(|channel| { let ctxx = ctx.clone(); @@ -98,5 +96,5 @@ fn get_all_role_reaction_message( .map(|m| (m, &rrm.mapping)) }) }) - .collect(); + .collect() } diff --git a/src/token_management.rs b/src/token_management.rs index 5ea82a01026585a24712d56e1247db89b93da81b..84c357d51e36399e85cdd4f33550e7ae2ff3857f 100644 --- a/src/token_management.rs +++ b/src/token_management.rs @@ -14,7 +14,7 @@ fn text_encrypt(plaintext: &str) -> String { let iv: &[u8; 16] = &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; let encrypted_vec = encrypt(*CIPHER, &*KEY, Some(iv), plaintext.as_bytes()).expect("encryption failed"); - return base64::encode(encrypted_vec.as_slice()); + base64::encode(encrypted_vec.as_slice()) } fn text_decrypt(ciphertext: &str) -> Option<String> { let iv: &[u8; 16] = &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; @@ -31,10 +31,10 @@ fn text_decrypt(ciphertext: &str) -> Option<String> { } else { warn!("Unable to decode base64 text"); } - return None; + None } -pub fn generate_token<'a>(discord_user: &User, username: &str) -> String { +pub fn generate_token(discord_user: &User, username: &str) -> String { // if username doesn't exist : throw error let timestamp = Utc::now().to_rfc3339(); let payload = format!( @@ -44,7 +44,7 @@ pub fn generate_token<'a>(discord_user: &User, username: &str) -> String { username ); info!("Token generated for {}: {}", discord_user.name, &payload); - text_encrypt(&payload).to_string() + text_encrypt(&payload) } #[derive(Debug)] @@ -86,8 +86,8 @@ pub fn parse_token(discord_user: &User, encrypted_token: &str) -> Result<String, "... verification successful (token {} seconds old)", time_delta_seconds ); - return Ok(token_username.to_owned()); + Ok(token_username.to_owned()) } else { - return Err(TokenError::TokenInvalid); + Err(TokenError::TokenInvalid) } } diff --git a/src/user_management.rs b/src/user_management.rs index 22da8e17c8702319b5428787775c54305291ebca..6b208d7ba6d1061f10cc449fee11fe1fb3a15130 100644 --- a/src/user_management.rs +++ b/src/user_management.rs @@ -51,7 +51,7 @@ impl Commands { ); } pub fn register(ctx: Context, msg: Message, account_name: &str) { - if account_name.len() <= 0 { + if account_name.is_empty() { e!( "Error sending message: {:?}", msg.channel_id diff --git a/src/util.rs b/src/util.rs index b0f2158867614a218322de8d7c136799a58fadae..b168e8ac559409787aebf668a56bb199c61b409a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,15 +2,22 @@ use serenity::model::{channel::ReactionType, guild::PartialGuild}; 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::Custom { + name: Some(name), .. + } => name, + ReactionType::Custom { id, name: None, .. } => id.to_string(), ReactionType::Unicode(name) => name, _ => format!("Unrecognised reaction type: {:?}", react), } } pub fn get_react_from_string(string: String, guild: PartialGuild) -> ReactionType { - guild.emojis.values().find(|e| e.name == string).map_or_else( - || ReactionType::from(string), // unicode emoji - |custom_emoji| ReactionType::from(custom_emoji.id)) + guild + .emojis + .values() + .find(|e| e.name == string) + .map_or_else( + || ReactionType::from(string), // unicode emoji + |custom_emoji| ReactionType::from(custom_emoji.id), + ) } diff --git a/src/voting.rs b/src/voting.rs index d9dd4f6bfa219b876abb6d490ece615786adb26b..90b7c6e888b5b13d7021f2072ca77c6e060e0d92 100644 --- a/src/voting.rs +++ b/src/voting.rs @@ -22,7 +22,7 @@ pub struct Commands; impl Commands { pub fn move_something(ctx: Context, msg: Message, content: &str) { let motion = content; - if motion.len() > 0 { + if !motion.is_empty() { create_motion(&ctx, &msg, motion); return; } @@ -43,7 +43,7 @@ impl Commands { } pub fn poll(ctx: Context, msg: Message, content: &str) { let topic = content; - if topic.len() > 0 { + if !topic.is_empty() { create_poll(&ctx, &msg, topic); return; } @@ -83,7 +83,7 @@ fn create_motion(ctx: &Context, msg: &Message, topic: &str) { if let Err(why) = msg.delete(ctx) { error!("Error deleting motion prompt: {:?}", why); } - match msg.channel_id.send_message(&ctx.http, |m| { + let result = msg.channel_id.send_message(&ctx.http, |m| { m.embed(|embed| { embed.author(|a| { a.name(&msg.author.name); @@ -114,11 +114,9 @@ fn create_motion(ctx: &Context, msg: &Message, topic: &str) { CONFIG.disapprove_react.to_string(), ]); m - }) { - Err(why) => { - error!("Error creating motion: {:?}", why); - } - Ok(_) => {} + }); + if let Err(why) = result { + error!("Error creating motion: {:?}", why); } } @@ -199,10 +197,10 @@ fn get_cached_motion(ctx: &Context, msg: &Message) -> MotionInfo { }; cached_motions.insert(msg.id, this_motion); } - return (*cached_motions.get(&msg.id).unwrap()).clone(); + (*cached_motions.get(&msg.id).unwrap()).clone() } -fn set_cached_motion(id: &serenity::model::id::MessageId, motion_info: MotionInfo) { - if let Some(motion) = MOTIONS_CACHE.lock().unwrap().get_mut(id) { +fn set_cached_motion(id: serenity::model::id::MessageId, motion_info: MotionInfo) { + if let Some(motion) = MOTIONS_CACHE.lock().unwrap().get_mut(&id) { *motion = motion_info; return; } @@ -302,8 +300,7 @@ fn update_motion( let last_status_full = old_embed .fields .iter() - .filter(|f| f.name == "Status") - .next() + .find(|f| f.name == "Status") .expect("No previous status") .clone() .value; @@ -349,11 +346,17 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { match user.has_role(&ctx, CONFIG.server_id, CONFIG.vote_role) { Ok(true) => { // 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 != &&react_as_string) + for react in [ + CONFIG.for_vote.to_string(), + CONFIG.against_vote.to_string(), + CONFIG.abstain_vote.to_string(), + ] + .iter() + .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() { if a_user.id.0 == user.id.0 { if let Err(why) = add_reaction.delete(&ctx) { @@ -364,8 +367,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { } } // remove 'illegal' reacts - if !CONFIG.allowed_reacts().contains(&react_as_string) - { + if !CONFIG.allowed_reacts().contains(&react_as_string) { if let Err(why) = add_reaction.delete(&ctx) { error!("Error deleting react: {:?}", why); }; @@ -373,19 +375,19 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { } // update motion let mut motion_info = get_cached_motion(&ctx, &message); - if let Some(vote) = motion_info - .votes - .get_mut(&react_as_string) - { + if let Some(vote) = motion_info.votes.get_mut(&react_as_string) { vote.retain(|u| u.id != user.id); vote.push(user.clone()); } - set_cached_motion(&message.id, motion_info); + set_cached_motion(message.id, motion_info); update_motion(&ctx, &mut message, &user, "add", add_reaction); } Ok(false) => { - if ![CONFIG.approve_react.to_string(), CONFIG.disapprove_react.to_string()] - .contains(&react_as_string) + if ![ + CONFIG.approve_react.to_string(), + CONFIG.disapprove_react.to_string(), + ] + .contains(&react_as_string) { if let Err(why) = add_reaction.delete(&ctx) { error!("Error deleting react: {:?}", why); @@ -416,7 +418,7 @@ pub fn reaction_remove(ctx: Context, removed_reaction: channel::Reaction) { { vote.retain(|u| u.id != user.id); } - set_cached_motion(&message.id, motion_info); + set_cached_motion(message.id, motion_info); update_motion(&ctx, &mut message, &user, "remove", removed_reaction); } }