Clean up match statements in main.rs

This commit is contained in:
charlotte ✨ 2021-07-08 00:14:40 +01:00
parent 7c43b9e1cd
commit e5bc576c83
1 changed files with 51 additions and 66 deletions

View File

@ -14,7 +14,8 @@ use serenity::{
}, },
framework::standard::{ framework::standard::{
macros::{check, command, group, hook}, macros::{check, command, group, hook},
Args, CommandOptions, CommandResult, DispatchError, Reason, StandardFramework, Args, CommandError, CommandOptions, CommandResult, DispatchError, Reason,
StandardFramework,
}, },
model::{ model::{
channel::{Message, ReactionType}, 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 // I have no idea if this works but its 5æm and I need to sleep help
let data = ctx.data.read().await; let data = ctx.data.read().await;
let shard_manager = match data.get::<ShardManagerContainer>() { let shard_manager = data
Some(v) => v, .get::<ShardManagerContainer>()
None => return Err("There was a problem getting the shard manager!".into()), .ok_or_else(|| CommandError::from("There was a problem getting the shard manager!"))?;
};
let manager = shard_manager.lock().await; let manager = shard_manager.lock().await;
let runners = manager.runners.lock().await; let runners = manager.runners.lock().await;
let runner = match runners.get(&ShardId(ctx.shard_id)) { let runner = runners
Some(v) => v, .get(&ShardId(ctx.shard_id))
None => return Err("No shard found!".into()), .ok_or_else(|| CommandError::from("No shard found!"))?;
};
let ping = match runner.latency { let ping = runner
Some(v) => v.as_millis(), .latency
None => return Err("Could not get latency!".into()), .ok_or_else(|| CommandError::from("Could not get latency!"))?
}; .as_millis();
let _ = message let _ = message
.channel_id .channel_id
@ -344,32 +343,29 @@ async fn bottom_rng(ctx: &Context, message: &Message, mut args: Args) -> Command
// get N last messages, otherwise 10 // get N last messages, otherwise 10
let num = args.single::<u64>().unwrap_or(10); let num = args.single::<u64>().unwrap_or(10);
let messages = message let mut messages = message
.channel_id .channel_id
.messages(&ctx.http, |get| get.before(message.id).limit(num)) .messages(&ctx.http, |get| get.before(message.id).limit(num))
.await; .await
if let Err(e) = messages { .map_err(|e| CommandError::from(format!("Error: {}", e)))?;
return Err(format!("Error: {}", e).into());
} else { // remove all messages by other users
let mut messages = messages?; messages.retain(|v| v.author != message.mentions[0]);
// remove all messages by other users let mut input = String::new();
messages.retain(|v| v.author != message.mentions[0]); for msg in messages {
let mut input = String::new(); input += &format!("{} ", msg.content);
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;
} }
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(()) Ok(())
} }
@ -389,7 +385,7 @@ async fn headpat(ctx: &Context, message: &Message, args: Args) -> CommandResult
_ => message.mentions[0].name.as_str(), _ => message.mentions[0].name.as_str(),
}; };
if let Err(e) = message message
.channel_id .channel_id
.send_message(&ctx.http, |m| { .send_message(&ctx.http, |m| {
m.embed(|e| { 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/)") .description("[Source](https://www.pinterest.com/pin/377809856242075277/)")
}) })
}) })
.await .await?;
{
let _ = message.channel_id.say(&ctx.http, format!("{:?}", e)).await;
};
Ok(()) Ok(())
} }
@ -530,20 +523,18 @@ async fn pfp(ctx: &Context, message: &Message) -> CommandResult {
async fn owo(ctx: &Context, message: &Message, args: Args) -> CommandResult { async fn owo(ctx: &Context, message: &Message, args: Args) -> CommandResult {
use owoify::OwOifiable; use owoify::OwOifiable;
let lastmsg = match message let messages = message
.channel_id .channel_id
.messages(&ctx.http, |get| get.before(message.id).limit(1)) .messages(&ctx.http, |get| get.before(message.id).limit(1))
.await .await
{ .map_err(|_| CommandError::from("Could not get last message!"))?;
Ok(v) => v,
Err(_) => return Err("Could not get last message!".into()),
};
let lastmsg = &lastmsg[0].content; let lastmsg = &messages[0].content;
let input: String = match args.is_empty() { let input: String = if args.is_empty() {
true => s!(lastmsg), s!(lastmsg)
false => args.rest().trim().to_string(), } else {
args.rest().trim().to_string()
}; };
let _ = message.channel_id.say(&ctx.http, input.owoify()).await; 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)] #[only_in(guilds)]
#[aliases("description", "topic")] #[aliases("description", "topic")]
async fn desc(ctx: &Context, message: &Message) -> CommandResult { async fn desc(ctx: &Context, message: &Message) -> CommandResult {
let channel = match message.channel(&ctx).await { let channel = message
Some(ch) => ch, .channel(&ctx)
None => { .await
return Err("Could not get channel!".into()); .ok_or_else(|| CommandError::from("Could not get channel!"))?;
} let channel = channel
}; .guild()
let channel = match channel.guild() { .ok_or_else(|| CommandError::from("Could not get guild channel!"))?;
Some(g) => g,
None => {
return Err("Could not get guild channel!".into());
}
};
let topic = if channel.topic.clone().unwrap() != "" { let topic = match channel.topic.as_deref() {
channel.topic.clone().unwrap() Some("") | None => String::from("No channel topic found"),
} else { Some(topic) => topic.to_string(),
String::from("No channel topic found")
}; };
let _ = message let _ = message