Error handling 2: electric boogaloo

Rewrite init and host commands
Minor cleanup
This commit is contained in:
Agatha 2020-03-29 22:15:00 +03:00
parent 036034fbb5
commit aaf5ed8887
1 changed files with 78 additions and 71 deletions

View File

@ -1,6 +1,5 @@
#![allow(clippy::unreadable_literal)] #![allow(clippy::unreadable_literal)]
use owoify::OwOifiable;
use rand::Rng; use rand::Rng;
use serenity::{ use serenity::{
client::Client, client::Client,
@ -83,25 +82,18 @@ fn owner_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions)
#[check] #[check]
#[name = "Server"] #[name = "Server"]
fn server_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult { fn server_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult {
(msg.guild_id == Some(serenity::model::id::GuildId(255386835964919810))).into() (msg.guild_id == Some(serenity::model::id::GuildId(687011389294116875))).into()
} }
#[command] #[command]
fn init(ctx: &mut Context, message: &Message) -> CommandResult { fn init(ctx: &mut Context, message: &Message) -> CommandResult {
let num = rand::thread_rng().gen_range(0, 2); let responses = [
match num { "Discordinator9000 is gonna hug nya'll!",
0 => { "Nyaa~!",
let _ = message "Hewwo uwu",
.channel_id ];
.say(&ctx.http, "The Discordinator9000 is gonna hug ny'all!"); let num = rand::thread_rng().gen_range(0, responses.len());
} let _ = message.channel_id.say(&ctx.http, responses[num]);
1 => {
let _ = message.channel_id.say(&ctx.http, "Nyaa~!");
}
_ => {
let _ = message.channel_id.say(&ctx.http, "Oopsie woopsie! UwU");
}
}
Ok(()) Ok(())
} }
@ -119,8 +111,8 @@ fn halt(ctx: &mut Context) -> CommandResult {
ctx.set_presence(None, OnlineStatus::Offline); ctx.set_presence(None, OnlineStatus::Offline);
use std::{thread, time}; use std::{thread, time};
let one_s = time::Duration::new(2, 0); // Sleep for 1s
thread::sleep(one_s); thread::sleep(time::Duration::new(1, 0));
process::exit(0); process::exit(0);
} }
@ -147,9 +139,14 @@ fn host(ctx: &mut Context, message: &Message) -> CommandResult {
let _ = message.channel_id.say( let _ = message.channel_id.say(
&ctx.http, &ctx.http,
format!( format!(
"Debug\nOS: {:?}\nHost: {:?}", "Debug\nOS: {os}; {release}\nHost: {host}\nCPU: {cpu}MHz",
sys_info::os_type().unwrap(), os = sys_info::os_type().unwrap(),
sys_info::hostname().unwrap() host = sys_info::hostname().unwrap(),
release = sys_info::linux_os_release()
.unwrap()
.pretty_name
.unwrap_or_else(|| "Unknown".to_string()),
cpu = sys_info::cpu_speed().unwrap()
), ),
); );
@ -175,18 +172,24 @@ fn embed(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
url: Option<String>, url: Option<String>,
} }
let input_embed: EmbedProperties = let input_embed: EmbedProperties = match toml::from_str(args.rest().trim()) {
toml::from_str(args.rest().trim()).unwrap_or(EmbedProperties { Ok(v) => v,
author: None, Err(e) => {
colour: 000000.to_string(), eprintln!("Deserialization error: {:?}", e);
description: None,
fields: None, EmbedProperties {
footer: None, author: None,
image: None, colour: 000000.to_string(),
timestamp: None, description: None,
title: None, fields: None,
url: None, footer: None,
}); image: None,
timestamp: None,
title: None,
url: None,
}
}
};
let _ = message.channel_id.send_message(&ctx.http, |m| { let _ = message.channel_id.send_message(&ctx.http, |m| {
m.embed(|e| { m.embed(|e| {
@ -291,8 +294,8 @@ fn ship(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
.map(|x| x.to_owned()) .map(|x| x.to_owned())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
let shipname: Result<String, ()> = match names.len() { let shipname: Result<String, String> = match names.len() {
0 => Err(()), 0 => Err("Invalid input!".to_string()),
1 => Ok(names[0].clone()), 1 => Ok(names[0].clone()),
_ => { _ => {
let mut first_halves = String::new(); let mut first_halves = String::new();
@ -306,34 +309,39 @@ fn ship(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
} }
}; };
let _ = message.channel_id.send_message(&ctx.http, |m| { if let Err(e) = shipname {
m.embed(|e| { let _ = message.channel_id.say(&ctx.http, e);
e.title(format!("Original names: {}", args.rest().trim())) } else {
.description(format!( let _ = message.channel_id.send_message(&ctx.http, |m| {
"Ship name:\n**{}**\nCompatibility: **{}%**\n{}", m.embed(|e| {
shipname.unwrap_or_else(|_| "Invalid input!".to_string()), e.title(format!("Original names: {}", args.rest().trim()))
compat, .description(format!(
compbar "Ship name:\n**{}**\nCompatibility: **{}%**\n{}",
)) shipname.unwrap(),
.color(0xffd1dc) compat,
}) compbar
}); ))
.color(0xffd1dc)
})
});
}
Ok(()) Ok(())
} }
#[command] #[command]
fn headpat(ctx: &mut Context, message: &Message, args: Args) -> CommandResult { fn headpat(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
message if let Err(e) = message.channel_id.send_message(&ctx.http, |m| {
.channel_id m.embed(|e| {
.send_message(&ctx.http, |m| { e.title(format!("Sending headpats to **{}**...", args.rest().trim()))
m.embed(|e| { .image(
e.title(format!("Sending headpats to **{}**...", args.rest().trim())) "https://i.pinimg.com/originals/83/1a/90/831a903eab6d827dcfd298b9e3196e30.jpg",
.image("https://i.pinimg.com/originals/83/1a/90/831a903eab6d827dcfd298b9e3196e30.jpg") )
.description("[Source](https://www.pinterest.com/pin/377809856242075277/)") .description("[Source](https://www.pinterest.com/pin/377809856242075277/)")
})
}) })
.expect("Failed to send message!"); }) {
let _ = message.channel_id.say(&ctx.http, format!("{:?}", e));
};
Ok(()) Ok(())
} }
@ -356,22 +364,19 @@ fn uwu(ctx: &mut Context, message: &Message) -> CommandResult {
#[command] #[command]
fn gayculator(ctx: &mut Context, message: &Message, mut args: Args) -> CommandResult { fn gayculator(ctx: &mut Context, message: &Message, mut args: Args) -> CommandResult {
//the amiter check let number_32: i32 = args.single::<i32>().unwrap_or(1);
if message.author.id != 191948420141809665 { let result = if number_32 % 2 == 0 {
let number_32: i32 = args.single::<i32>().unwrap_or(1); "much straight"
let result = if number_32 % 2 == 0 { } else {
"much straight" "large gay"
} else { };
"large gay" let _ = message.channel_id.send_message(&ctx.http, |m| {
}; m.embed(|e| {
let _ = message.channel_id.send_message(&ctx.http, |m| { e.title("Gayness level:")
m.embed(|e| { .description(result)
e.title("Gayness level:") .color(0xffd1dc)
.description(result) })
.color(0xffd1dc) });
})
});
}
Ok(()) Ok(())
} }
@ -528,6 +533,8 @@ fn what(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
#[command] #[command]
fn owo(ctx: &mut Context, message: &Message, args: Args) -> CommandResult { fn owo(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
use owoify::OwOifiable;
let input: String = args.rest().trim().to_string(); let input: String = args.rest().trim().to_string();
let _ = message.channel_id.say(&ctx.http, input.owoify()); let _ = message.channel_id.say(&ctx.http, input.owoify());