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