Properly log all failed commands

This commit is contained in:
August 2024-09-30 15:12:29 -04:00
parent ebf1f08cb2
commit 30232c529e
Signed by: shibedrill
GPG Key ID: 5FE0CB25945EFAA2
6 changed files with 32 additions and 13 deletions

2
Cargo.lock generated
View File

@ -1522,7 +1522,7 @@ dependencies = [
[[package]]
name = "shibe-bot"
version = "0.2.3"
version = "0.2.4"
dependencies = [
"build-time",
"dotenv",

View File

@ -1,6 +1,6 @@
[package]
name = "shibe-bot"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,4 +1,4 @@
# Shibe Bot 0.2.3
# Shibe Bot 0.2.4
[![Rust](https://github.com/shibedrill/shibe-bot/actions/workflows/rust.yml/badge.svg)](https://github.com/shibedrill/shibe-bot/actions/workflows/rust.yml)
[![GitHub License](https://img.shields.io/github/license/shibedrill/shibe-bot)](LICENSE.txt)
## About Shibe Bot

View File

@ -101,7 +101,10 @@ pub async fn eightball(ctx: Context<'_>) -> Result<(), Error> {
let mut rng = rand::thread_rng();
responses
.choose(&mut rng)
.expect("`responses` array is empty")
.ok_or("Response array is empty".to_string())
.inspect_err(|e| {
error!("Executing command `eightball` failed: {}", e)
})?
};
ctx.say(format!("Magic 8-ball says: '{}'", *response))
.await?;
@ -141,7 +144,10 @@ pub async fn deer(ctx: Context<'_>) -> Result<(), Error> {
hot.data
.children
.choose(&mut rng)
.ok_or("Unable to get any hot posts.")?
.ok_or("Unable to get any hot posts")
.inspect_err(|e| {
error!("Executing command `deer` failed: {}", e)
})?
};
ctx.say(format!("https://reddit.com{}", &chosen_post.data.permalink))
.await?;

View File

@ -59,9 +59,10 @@ async fn event_handler(
data: &Data,
) -> Result<(), Error> {
if let FullEvent::ChannelDelete {
channel,
messages: _,
} = event {
channel,
messages: _,
} = event
{
info!("Handling event type: ChannelDelete({})", channel.id);
data.config_manager
.lock()
@ -150,7 +151,10 @@ async fn main() {
let mut client = serenity::ClientBuilder::new(token, intents)
.framework(framework)
.await
.expect("Unable to build client");
.unwrap_or_else(|e| {
error!("Building client failed: {}", e);
std::process::exit(-1);
});
info!("Built client successfully");
// List the owner
@ -160,14 +164,23 @@ async fn main() {
.http
.get_current_application_info()
.await
.expect("Could not get current application info")
.unwrap_or_else(|e| {
error!("Getting application info failed: {}", e);
std::process::exit(-1);
})
.owner
.expect("Could not get owner info")
.unwrap_or_else(|| {
error!("Getting owner info failed: `.owner` is `None`");
std::process::exit(-1);
})
.name
);
// Finally start everything. Nothing after this should be reachable normally.
info!("Starting client");
client.start().await.expect("Could not start client");
client.start().await.unwrap_or_else(|e| {
error!("Starting client failed: {}", e);
std::process::exit(-1);
});
info!("All tasks finished, shutting down");
}