From e5bc576c8350dd6b5038814bb7766dedec6026da Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Thu, 8 Jul 2021 00:14:40 +0100 Subject: [PATCH] Clean up match statements in main.rs --- src/main.rs | 117 +++++++++++++++++++++++----------------------------- 1 file changed, 51 insertions(+), 66 deletions(-) diff --git a/src/main.rs b/src/main.rs index 17a060d..e138bc2 100755 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,8 @@ use serenity::{ }, framework::standard::{ macros::{check, command, group, hook}, - Args, CommandOptions, CommandResult, DispatchError, Reason, StandardFramework, + Args, CommandError, CommandOptions, CommandResult, DispatchError, Reason, + StandardFramework, }, model::{ channel::{Message, ReactionType}, @@ -206,23 +207,21 @@ async fn ping(ctx: &Context, message: &Message) -> CommandResult { // I have no idea if this works but its 5æm and I need to sleep help let data = ctx.data.read().await; - let shard_manager = match data.get::() { - Some(v) => v, - None => return Err("There was a problem getting the shard manager!".into()), - }; + let shard_manager = data + .get::() + .ok_or_else(|| CommandError::from("There was a problem getting the shard manager!"))?; let manager = shard_manager.lock().await; let runners = manager.runners.lock().await; - let runner = match runners.get(&ShardId(ctx.shard_id)) { - Some(v) => v, - None => return Err("No shard found!".into()), - }; + let runner = runners + .get(&ShardId(ctx.shard_id)) + .ok_or_else(|| CommandError::from("No shard found!"))?; - let ping = match runner.latency { - Some(v) => v.as_millis(), - None => return Err("Could not get latency!".into()), - }; + let ping = runner + .latency + .ok_or_else(|| CommandError::from("Could not get latency!"))? + .as_millis(); let _ = message .channel_id @@ -344,32 +343,29 @@ async fn bottom_rng(ctx: &Context, message: &Message, mut args: Args) -> Command // get N last messages, otherwise 10 let num = args.single::().unwrap_or(10); - let messages = message + let mut messages = message .channel_id .messages(&ctx.http, |get| get.before(message.id).limit(num)) - .await; - if let Err(e) = messages { - return Err(format!("Error: {}", e).into()); - } else { - let mut messages = messages?; - // remove all messages by other users - messages.retain(|v| v.author != message.mentions[0]); - let mut input = String::new(); - for msg in messages { - input += &format!("{} ", msg.content); - } - let result: u64 = StdRng::seed_from_u64(calculate_hash(&input)).gen_range(0..100); - let _ = message - .channel_id - .send_message(&ctx.http, |m| { - m.embed(|e| { - e.title("Bottom RNG") - .description(format!("Result: {}", result)) - .color(0x800869) - }) - }) - .await; + .await + .map_err(|e| CommandError::from(format!("Error: {}", e)))?; + + // remove all messages by other users + messages.retain(|v| v.author != message.mentions[0]); + let mut input = String::new(); + for msg in messages { + input += &format!("{} ", msg.content); } + let result: u64 = StdRng::seed_from_u64(calculate_hash(&input)).gen_range(0..100); + let _ = message + .channel_id + .send_message(&ctx.http, |m| { + m.embed(|e| { + e.title("Bottom RNG") + .description(format!("Result: {}", result)) + .color(0x800869) + }) + }) + .await; Ok(()) } @@ -389,7 +385,7 @@ async fn headpat(ctx: &Context, message: &Message, args: Args) -> CommandResult _ => message.mentions[0].name.as_str(), }; - if let Err(e) = message + message .channel_id .send_message(&ctx.http, |m| { m.embed(|e| { @@ -400,10 +396,7 @@ async fn headpat(ctx: &Context, message: &Message, args: Args) -> CommandResult .description("[Source](https://www.pinterest.com/pin/377809856242075277/)") }) }) - .await - { - let _ = message.channel_id.say(&ctx.http, format!("{:?}", e)).await; - }; + .await?; Ok(()) } @@ -530,20 +523,18 @@ async fn pfp(ctx: &Context, message: &Message) -> CommandResult { async fn owo(ctx: &Context, message: &Message, args: Args) -> CommandResult { use owoify::OwOifiable; - let lastmsg = match message + let messages = message .channel_id .messages(&ctx.http, |get| get.before(message.id).limit(1)) .await - { - Ok(v) => v, - Err(_) => return Err("Could not get last message!".into()), - }; + .map_err(|_| CommandError::from("Could not get last message!"))?; - let lastmsg = &lastmsg[0].content; + let lastmsg = &messages[0].content; - let input: String = match args.is_empty() { - true => s!(lastmsg), - false => args.rest().trim().to_string(), + let input: String = if args.is_empty() { + s!(lastmsg) + } else { + args.rest().trim().to_string() }; let _ = message.channel_id.say(&ctx.http, input.owoify()).await; @@ -555,23 +546,17 @@ async fn owo(ctx: &Context, message: &Message, args: Args) -> CommandResult { #[only_in(guilds)] #[aliases("description", "topic")] async fn desc(ctx: &Context, message: &Message) -> CommandResult { - let channel = match message.channel(&ctx).await { - Some(ch) => ch, - None => { - return Err("Could not get channel!".into()); - } - }; - let channel = match channel.guild() { - Some(g) => g, - None => { - return Err("Could not get guild channel!".into()); - } - }; + let channel = message + .channel(&ctx) + .await + .ok_or_else(|| CommandError::from("Could not get channel!"))?; + let channel = channel + .guild() + .ok_or_else(|| CommandError::from("Could not get guild channel!"))?; - let topic = if channel.topic.clone().unwrap() != "" { - channel.topic.clone().unwrap() - } else { - String::from("No channel topic found") + let topic = match channel.topic.as_deref() { + Some("") | None => String::from("No channel topic found"), + Some(topic) => topic.to_string(), }; let _ = message