diff --git a/src/main.rs b/src/main.rs index 16bd90914b7d455d01b0f16748821291703d2f41..2100ae2c63d8de576a74f36854a1bca1f319f8f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,51 +35,52 @@ impl EventHandler for Handler { // Event handlers are dispatched through a threadpool, and so multiple // events can be dispatched simultaneously. fn message(&self, ctx: Context, msg: Message) { - if msg.content.starts_with(config::COMMAND_PREFIX) { - let message_content: Vec<_> = msg.content[1..].splitn(2, ' ').collect(); - match message_content[0] { - "register" => { - user_management::Commands::register(ctx, msg.clone(), message_content[1]) - } - "join" => { - user_management::Commands::join(ctx, msg.clone(), message_content[1]); - } - "move" => { - voting::Commands::move_something(ctx, msg.clone(), message_content[1]); - } - "motion" => { - voting::Commands::motion(ctx, msg.clone(), message_content[1]); - } - "poll" => { - voting::Commands::poll(ctx, msg.clone(), message_content[1]); - } - "cowsay" => { - voting::Commands::cowsay(ctx, msg.clone(), message_content[1]); - } - "help" => { - let mut message = MessageBuilder::new(); - message.push_line(format!( - "Use {}move <action> to make a circular motion", - config::COMMAND_PREFIX - )); - message.push_line(format!( - "Use {}poll <proposal> to see what people think about something", - config::COMMAND_PREFIX - )); - e!( - "Error sending message: {:?}", - msg.channel_id.say(&ctx.http, message.build()) - ); - } - _ => { - e!( - "Error sending message: {:?}", - msg.channel_id.say( - &ctx.http, - format!("Unrecognised command. Try {}help", config::COMMAND_PREFIX) - ) - ); - } + if !(msg.content.starts_with(config::COMMAND_PREFIX)) { + return; + } + let message_content: Vec<_> = msg.content[1..].splitn(2, ' ').collect(); + match message_content[0] { + "register" => { + user_management::Commands::register(ctx, msg.clone(), message_content[1]) + } + "join" => { + user_management::Commands::join(ctx, msg.clone(), message_content[1]); + } + "move" => { + voting::Commands::move_something(ctx, msg.clone(), message_content[1]); + } + "motion" => { + voting::Commands::motion(ctx, msg.clone(), message_content[1]); + } + "poll" => { + voting::Commands::poll(ctx, msg.clone(), message_content[1]); + } + "cowsay" => { + voting::Commands::cowsay(ctx, msg.clone(), message_content[1]); + } + "help" => { + let mut message = MessageBuilder::new(); + message.push_line(format!( + "Use {}move <action> to make a circular motion", + config::COMMAND_PREFIX + )); + message.push_line(format!( + "Use {}poll <proposal> to see what people think about something", + config::COMMAND_PREFIX + )); + e!( + "Error sending message: {:?}", + msg.channel_id.say(&ctx.http, message.build()) + ); + } + _ => { + e!( + "Error sending message: {:?}", + msg.channel_id.say( + &ctx.http, + format!("Unrecognised command. Try {}help", config::COMMAND_PREFIX) + ) + ); } } } @@ -166,16 +167,15 @@ fn main() { } 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 { + if message.embeds.len() <= 0 { return "misc"; } + 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", + }; } diff --git a/src/user_management.rs b/src/user_management.rs index 286c244afa511a20e564f367b3465aa34e284d7e..a95d50029efe80add6f8b835afcf691d3628df1e 100644 --- a/src/user_management.rs +++ b/src/user_management.rs @@ -51,51 +51,51 @@ impl Commands { } pub fn register(ctx: Context, msg: Message, content: &str) { let name = content; - if name.len() > 0 { - e!( - "Unable to get member: {:?}", - serenity::model::id::GuildId(config::SERVER_ID) - .member(ctx.http.clone(), msg.author.id) - .map(|mut member| { - e!( - "Unable to remove role: {:?}", - member.remove_role(&ctx.http, config::UNREGISTERED_MEMBER_ROLE) - ); - e!( - "Unable to edit nickname: {:?}", - member - .edit(&ctx.http, |m| { - let mut rng = rand::thread_rng(); - m.nickname(format!( - "{}, {}", - name, - [ - "The Big Cheese", - "The One and Only", - "The Exalted One", - "not to be trusted", - "The Scoundrel", - "A big fish in a small pond", - ][rng.gen_range(0, 5)] - )); - m - }) - .map(|()| { - e!( - "Unable to add role: {:?}", - member.add_role(&ctx.http, config::REGISTERED_MEMBER_ROLE) - ); - }) - ); - }) - ); - e!("Error deleting register message: {:?}", msg.delete(ctx)); - } else { + if name.len() <= 0 { e!( "Error sending message: {:?}", msg.channel_id .say(&ctx.http, "Usage: !register <ucc username>") ); + return; } + e!( + "Unable to get member: {:?}", + serenity::model::id::GuildId(config::SERVER_ID) + .member(ctx.http.clone(), msg.author.id) + .map(|mut member| { + e!( + "Unable to remove role: {:?}", + member.remove_role(&ctx.http, config::UNREGISTERED_MEMBER_ROLE) + ); + e!( + "Unable to edit nickname: {:?}", + member + .edit(&ctx.http, |m| { + let mut rng = rand::thread_rng(); + m.nickname(format!( + "{}, {}", + name, + [ + "The Big Cheese", + "The One and Only", + "The Exalted One", + "not to be trusted", + "The Scoundrel", + "A big fish in a small pond", + ][rng.gen_range(0, 5)] + )); + m + }) + .map(|()| { + e!( + "Unable to add role: {:?}", + member.add_role(&ctx.http, config::REGISTERED_MEMBER_ROLE) + ); + }) + ); + }) + ); + e!("Error deleting register message: {:?}", msg.delete(ctx)); } } diff --git a/src/voting.rs b/src/voting.rs index 30cf974bda245d391cd58e27b509ea856be3854c..a43b663a392e6d2b2f6aea18ab94d123d3124e6d 100644 --- a/src/voting.rs +++ b/src/voting.rs @@ -23,15 +23,15 @@ impl Commands { let motion = content; if motion.len() > 0 { create_motion(&ctx, &msg, motion); - } else { - e!( - "Error sending message: {:?}", - msg.channel_id.say( - &ctx.http, - "If there's something you want to motion, put it after the !move keyword", - ) - ); + return; } + e!( + "Error sending message: {:?}", + msg.channel_id.say( + &ctx.http, + "If there's something you want to motion, put it after the !move keyword", + ) + ); } pub fn motion(ctx: Context, msg: Message, _content: &str) { e!("Error sending message: {:?}", @@ -44,15 +44,15 @@ impl Commands { let topic = content; if topic.len() > 0 { create_poll(&ctx, &msg, topic); - } else { - e!( - "Error sending message: {:?}", - msg.channel_id.say( - &ctx.http, - "If there's something you want to motion, put it after the !move keyword", - ) - ); + return; } + e!( + "Error sending message: {:?}", + msg.channel_id.say( + &ctx.http, + "If there's something you want to motion, put it after the !move keyword", + ) + ); } pub fn cowsay(ctx: Context, msg: Message, content: &str) { let mut text = content.to_owned(); @@ -203,9 +203,9 @@ fn get_cached_motion(ctx: &Context, msg: &Message) -> MotionInfo { 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; - } else { - warn!("{}", "Couldn't find motion in cache to set"); + return; } + warn!("{}", "Couldn't find motion in cache to set"); } fn update_motion( @@ -357,7 +357,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { if let Err(why) = add_reaction.delete(&ctx) { error!("Error deleting react: {:?}", why); }; - return; + return; } } }