From 99e43404e38b77c03c0a3356453b0f7b40acb145 Mon Sep 17 00:00:00 2001 From: toast Date: Sat, 18 Jan 2025 20:59:14 +1100 Subject: [PATCH] Update template with changes --- .dockerignore | 3 +- .gitignore | 4 ++ events/src/events/ready.rs | 6 +-- jobs/src/tasks.rs | 4 +- library/src/config.rs | 2 +- library/src/lib.rs | 2 +- library/src/utils.rs | 6 +-- src/errors.rs | 74 +++++++++++++++++++++++++++++++++++++ src/main.rs | 76 +++----------------------------------- src/shutdown.rs | 4 +- 10 files changed, 96 insertions(+), 85 deletions(-) create mode 100644 src/errors.rs diff --git a/.dockerignore b/.dockerignore index a79056e..75bd1da 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,6 @@ .vscode -target +target/debug +target/release .gitignore docker-compose.yml Dockerfile diff --git a/.gitignore b/.gitignore index eb5a316..1e032da 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ target +.env* + +# Local version of GHA +act diff --git a/events/src/events/ready.rs b/events/src/events/ready.rs index f074341..64ee0a9 100755 --- a/events/src/events/ready.rs +++ b/events/src/events/ready.rs @@ -12,14 +12,14 @@ use { Ready }, rustbot_lib::{ + RustbotFwCtx, + RustbotResult, config::BINARY_PROPERTIES, utils::{ BOT_VERSION, GIT_COMMIT_BRANCH, GIT_COMMIT_HASH - }, - RustbotFwCtx, - RustbotResult + } }, std::sync::atomic::{ AtomicBool, diff --git a/jobs/src/tasks.rs b/jobs/src/tasks.rs index b858f54..4ae992e 100755 --- a/jobs/src/tasks.rs +++ b/jobs/src/tasks.rs @@ -8,8 +8,8 @@ use { tokio::{ task, time::{ - interval, - Duration + Duration, + interval } } }; diff --git a/library/src/config.rs b/library/src/config.rs index ae0d168..00c3b01 100755 --- a/library/src/config.rs +++ b/library/src/config.rs @@ -20,7 +20,7 @@ impl ConfigMeta { embed_color: 0xF1D63C, rustbot_logs: 1311282815601741844, developers: vec![ - 190407856527376384, // toast.ts + 190407856527376384, // nwero.sama ] } } diff --git a/library/src/lib.rs b/library/src/lib.rs index 46526d5..4e55371 100755 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -3,7 +3,7 @@ mod data; pub use data::RustbotData; pub mod utils; -type RustbotError = Box; +pub type RustbotError = Box; pub type RustbotContext<'a> = poise::Context<'a, RustbotData, RustbotError>; pub type RustbotFwCtx<'a> = poise::FrameworkContext<'a, RustbotData, RustbotError>; pub type RustbotResult = Result; diff --git a/library/src/utils.rs b/library/src/utils.rs index 5de5e6f..7dc8c8e 100755 --- a/library/src/utils.rs +++ b/library/src/utils.rs @@ -33,11 +33,7 @@ pub fn mention_dev(ctx: super::RustbotContext<'_>) -> Option { } } - if mentions.is_empty() { - None - } else { - Some(mentions.join(", ")) - } + if mentions.is_empty() { None } else { Some(mentions.join(", ")) } } pub fn get_guild_name(ctx: super::RustbotContext<'_>) -> String { diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..2fb9946 --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,74 @@ +use { + poise::FrameworkError, + rustbot_lib::{ + RustbotData, + RustbotError, + utils::mention_dev + } +}; + +pub async fn fw_errors(error: FrameworkError<'_, RustbotData, RustbotError>) { + match error { + poise::FrameworkError::Command { error, ctx, .. } => { + println!("PoiseCommandError({}): {error}", ctx.command().qualified_name); + ctx + .reply(format!( + "Encountered an error during command execution, ask {} to check console for more details!", + mention_dev(ctx).unwrap_or_default() + )) + .await + .expect("Error sending message"); + }, + poise::FrameworkError::EventHandler { error, event, .. } => println!("PoiseEventHandlerError({}): {error}", event.snake_case_name()), + poise::FrameworkError::NotAnOwner { ctx, .. } => { + println!( + "PoiseNotAnOwner: {} tried to execute a developer-level command ({})", + ctx.author().name, + ctx.command().qualified_name + ); + ctx + .reply("Whoa, you discovered a hidden command! Too bad, I can't allow you to execute it as you're not my creator.") + .await + .expect("Error sending message"); + }, + poise::FrameworkError::UnknownInteraction { interaction, .. } => println!( + "PoiseUnknownInteractionError: {} tried to execute an unknown interaction ({})", + interaction.user.name, interaction.data.name + ), + poise::FrameworkError::UnknownCommand { msg, .. } => println!( + "PoiseUnknownCommandError: {} tried to execute an unknown command ({})", + msg.author.name, msg.content + ), + poise::FrameworkError::ArgumentParse { ctx, error, .. } => { + println!("PoiseArgumentParseError: {error}"); + ctx + .reply(format!("Error parsing argument(s): {error}")) + .await + .expect("Error sending message"); + }, + poise::FrameworkError::CommandPanic { ctx, payload, .. } => { + if let Some(payload) = payload.clone() { + println!("PoiseCommandPanic: {payload}"); + ctx + .reply(format!( + "The command panicked, please tell my developer about this!\n**Error:**```\n{payload}\n```" + )) + .await + .expect("Error sending message"); + } else { + println!("PoiseCommandPanic: No payload provided"); + ctx + .reply( + [ + "Well, this is concerning... Hopefully you notified my developer about this!", + "The command panicked, but didn't leave any trace behind... Suspicious!" + ] + .join("\n") + ) + .await + .expect("Error sending message"); + } + }, + other => println!("PoiseOtherError: {other}") + } +} diff --git a/src/main.rs b/src/main.rs index ade446f..ac40f6a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,21 @@ +mod errors; mod shutdown; // https://cdn.toast-server.net/RustFSHiearchy.png // Using the new filesystem hierarchy use { poise::serenity_prelude::{ - builder::CreateAllowedMentions, ActivityData, ClientBuilder, - GatewayIntents + GatewayIntents, + builder::CreateAllowedMentions }, rustbot_cmds::collect, rustbot_events::events::processor, rustbot_lib::{ + RustbotData, config::BINARY_PROPERTIES, - utils::{ - get_guild_name, - mention_dev - }, - RustbotData + utils::get_guild_name }, rustbot_tokens::discord_token, std::{ @@ -65,69 +63,7 @@ async fn main() { execute_self_messages: false, ..Default::default() }, - on_error: |error| { - Box::pin(async move { - match error { - poise::FrameworkError::Command { error, ctx, .. } => { - println!("PoiseCommandError({}): {}", ctx.command().qualified_name, error); - ctx - .reply(format!( - "Encountered an error during command execution, ask {} to check console for more details!", - mention_dev(ctx).unwrap_or_default() - )) - .await - .expect("Error sending message"); - }, - poise::FrameworkError::EventHandler { error, event, .. } => println!("PoiseEventHandlerError({}): {}", event.snake_case_name(), error), - poise::FrameworkError::NotAnOwner { ctx, .. } => { - println!( - "PoiseNotAnOwner: {} tried to execute a developer-level command ({})", - ctx.author().name, - ctx.command().qualified_name - ); - ctx - .reply("Whoa, you discovered a hidden command! Too bad, I can't allow you to execute it as you're not my creator.") - .await - .expect("Error sending message"); - }, - poise::FrameworkError::UnknownInteraction { interaction, .. } => println!( - "PoiseUnknownInteractionError: {} tried to execute an unknown interaction ({})", - interaction.user.name, interaction.data.name - ), - poise::FrameworkError::UnknownCommand { msg, .. } => println!( - "PoiseUnknownCommandError: {} tried to execute an unknown command ({})", - msg.author.name, msg.content - ), - poise::FrameworkError::ArgumentParse { ctx, error, .. } => { - println!("PoiseArgumentParseError: {}", error); - ctx - .reply(format!("Error parsing argument(s): {error}")) - .await - .expect("Error sending message"); - }, - poise::FrameworkError::CommandPanic { ctx, payload, .. } => { - if let Some(payload) = payload.clone() { - println!("PoiseCommandPanic: {payload}"); - ctx - .reply(format!( - "The command panicked, please tell my developer about this!\n**Error:**```\n{payload}\n```" - )) - .await - .expect("Error sending message"); - } else { - println!("PoiseCommandPanic: No payload provided"); - let uh_oh = [ - "Well, this is concerning... Hopefully you notified my developer about this!", - "The command panicked, but didn't leave any trace behind... Suspicious!" - ] - .join("\n"); - ctx.reply(uh_oh).await.expect("Error sending message"); - } - }, - other => println!("PoiseOtherError: {other}") - } - }) - }, + on_error: |error| Box::pin(async move { errors::fw_errors(error).await }), allowed_mentions: Some(CreateAllowedMentions::default().empty_users()), initialize_owners: true, skip_checks_for_owners: true, diff --git a/src/shutdown.rs b/src/shutdown.rs index d0e7dce..af9d95f 100644 --- a/src/shutdown.rs +++ b/src/shutdown.rs @@ -1,8 +1,8 @@ use tokio::{ select, signal::unix::{ - signal, - SignalKind + SignalKind, + signal } };