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 c1d3598c40
commit 1b3f7dfea4
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,8 +172,12 @@ 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,
Err(e) => {
eprintln!("Deserialization error: {:?}", e);
EmbedProperties {
author: None, author: None,
colour: 000000.to_string(), colour: 000000.to_string(),
description: None, description: None,
@ -186,7 +187,9 @@ fn embed(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
timestamp: None, timestamp: None,
title: None, title: None,
url: 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 {
} }
}; };
if let Err(e) = shipname {
let _ = message.channel_id.say(&ctx.http, e);
} else {
let _ = message.channel_id.send_message(&ctx.http, |m| { let _ = message.channel_id.send_message(&ctx.http, |m| {
m.embed(|e| { m.embed(|e| {
e.title(format!("Original names: {}", args.rest().trim())) e.title(format!("Original names: {}", args.rest().trim()))
.description(format!( .description(format!(
"Ship name:\n**{}**\nCompatibility: **{}%**\n{}", "Ship name:\n**{}**\nCompatibility: **{}%**\n{}",
shipname.unwrap_or_else(|_| "Invalid input!".to_string()), shipname.unwrap(),
compat, compat,
compbar compbar
)) ))
.color(0xffd1dc) .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
.send_message(&ctx.http, |m| {
m.embed(|e| { m.embed(|e| {
e.title(format!("Sending headpats to **{}**...", args.rest().trim())) e.title(format!("Sending headpats to **{}**...", args.rest().trim()))
.image("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,8 +364,6 @@ 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
if message.author.id != 191948420141809665 {
let number_32: i32 = args.single::<i32>().unwrap_or(1); let number_32: i32 = args.single::<i32>().unwrap_or(1);
let result = if number_32 % 2 == 0 { let result = if number_32 % 2 == 0 {
"much straight" "much straight"
@ -371,7 +377,6 @@ fn gayculator(ctx: &mut Context, message: &Message, mut args: Args) -> CommandRe
.color(0xffd1dc) .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());