diff options
| author | seth <[email protected]> | 2023-11-30 22:18:51 -0500 |
|---|---|---|
| committer | seth <[email protected]> | 2023-12-01 07:12:49 -0500 |
| commit | 76c0f94e6d7aa108424b34826eb7d8514b026287 (patch) | |
| tree | 7315bd6dfe52c158041bed64ba39781718a69335 /src/commands | |
| parent | db52e639b85d79bed870020aec7a045851ca5ee3 (diff) | |
feat: use eyre, better logging, & refactor
small commits be damned
Diffstat (limited to 'src/commands')
| -rw-r--r-- | src/commands/ask.rs | 23 | ||||
| -rw-r--r-- | src/commands/bing.rs | 6 | ||||
| -rw-r--r-- | src/commands/convert.rs | 29 | ||||
| -rw-r--r-- | src/commands/copypasta.rs | 10 | ||||
| -rw-r--r-- | src/commands/mod.rs | 8 | ||||
| -rw-r--r-- | src/commands/random.rs | 42 | ||||
| -rw-r--r-- | src/commands/teawiespam.rs | 8 | ||||
| -rw-r--r-- | src/commands/version.rs | 6 |
8 files changed, 59 insertions, 73 deletions
diff --git a/src/commands/ask.rs b/src/commands/ask.rs index 7cc82a1..3589484 100644 --- a/src/commands/ask.rs +++ b/src/commands/ask.rs @@ -1,6 +1,6 @@ -use crate::consts; -use crate::utils; -use crate::{Context, Error}; +use crate::{consts, utils, Context}; + +use color_eyre::eyre::{Context as _, Result}; /// ask teawie a question! #[poise::command(prefix_command, slash_command)] @@ -9,15 +9,10 @@ pub async fn ask( #[description = "the question you want to ask teawie"] #[rename = "question"] _question: String, -) -> Result<(), Error> { - match utils::random_choice(consts::RESPONSES) { - Ok(resp) => { - ctx.say(resp).await?; - Ok(()) - } - Err(why) => { - ctx.say("idk").await?; - Err(why) - } - } +) -> Result<()> { + let resp = utils::random_choice(consts::RESPONSES) + .wrap_err("couldn't choose from random responses!")?; + + ctx.say(resp).await?; + Ok(()) } diff --git a/src/commands/bing.rs b/src/commands/bing.rs index ed91bb3..b80ebca 100644 --- a/src/commands/bing.rs +++ b/src/commands/bing.rs @@ -1,8 +1,10 @@ -use crate::{Context, Error}; +use crate::Context; + +use color_eyre::eyre::Result; /// make sure the wie is alive #[poise::command(prefix_command)] -pub async fn bing(ctx: Context<'_>) -> Result<(), Error> { +pub async fn bing(ctx: Context<'_>) -> Result<()> { ctx.say("bong!").await?; Ok(()) } diff --git a/src/commands/convert.rs b/src/commands/convert.rs index 1f39ae4..cbbf8dc 100644 --- a/src/commands/convert.rs +++ b/src/commands/convert.rs @@ -1,11 +1,13 @@ -use crate::{Context, Error}; +use crate::Context; + use bottomify::bottom; +use color_eyre::eyre::Result; #[poise::command( slash_command, subcommands("to_fahrenheit", "to_celsius", "to_bottom", "from_bottom") )] -pub async fn convert(_ctx: Context<'_>) -> Result<(), Error> { +pub async fn convert(_ctx: Context<'_>) -> Result<()> { Ok(()) } @@ -14,7 +16,7 @@ pub async fn convert(_ctx: Context<'_>) -> Result<(), Error> { pub async fn to_celsius( ctx: Context<'_>, #[description = "what teawie will convert"] degrees_fahrenheit: f32, -) -> Result<(), Error> { +) -> Result<()> { let temp = (degrees_fahrenheit - 32.0) * (5.0 / 9.0); ctx.say(temp.to_string()).await?; Ok(()) @@ -25,7 +27,7 @@ pub async fn to_celsius( pub async fn to_fahrenheit( ctx: Context<'_>, #[description = "what teawie will convert"] degrees_celsius: f32, -) -> Result<(), Error> { +) -> Result<()> { let temp = (degrees_celsius * (9.0 / 5.0)) + 32.0; ctx.say(temp.to_string()).await?; Ok(()) @@ -36,7 +38,7 @@ pub async fn to_fahrenheit( pub async fn to_bottom( ctx: Context<'_>, #[description = "what teawie will translate into bottom"] message: String, -) -> Result<(), Error> { +) -> Result<()> { let encoded = bottom::encode_string(&message); ctx.say(encoded).await?; Ok(()) @@ -47,17 +49,8 @@ pub async fn to_bottom( pub async fn from_bottom( ctx: Context<'_>, #[description = "what teawie will translate from bottom"] message: String, -) -> Result<(), Error> { - let d = bottom::decode_string(&message); - match d { - Ok(decoded) => { - ctx.say(decoded).await?; - Ok(()) - } - Err(why) => { - ctx.say("couldn't decode that for you, i'm sowwy!! :((".to_string()) - .await?; - Err(Box::new(why)) - } - } +) -> Result<()> { + let decoded = bottom::decode_string(&message)?; + ctx.say(decoded).await?; + Ok(()) } diff --git a/src/commands/copypasta.rs b/src/commands/copypasta.rs index 14a6673..313cd13 100644 --- a/src/commands/copypasta.rs +++ b/src/commands/copypasta.rs @@ -1,7 +1,8 @@ -use crate::{utils, Context, Error}; +use crate::{utils, Context}; use std::collections::HashMap; +use color_eyre::eyre::{eyre, Result}; use include_dir::{include_dir, Dir}; use log::*; @@ -58,8 +59,11 @@ fn get_copypasta(name: Copypastas) -> String { pub async fn copypasta( ctx: Context<'_>, #[description = "the copypasta you want to send"] copypasta: Copypastas, -) -> Result<(), Error> { - let gid = ctx.guild_id().unwrap_or_default(); +) -> Result<()> { + let gid = ctx + .guild_id() + .ok_or_else(|| eyre!("couldnt get guild from message!"))?; + if !utils::is_guild_allowed(gid) { info!("not running copypasta command in {gid}"); return Ok(()); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index b6130ab..5edf0b7 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -6,10 +6,12 @@ pub mod random; pub mod teawiespam; pub mod version; -use crate::{Data, Error}; +use crate::Data; + +use color_eyre::eyre::Report; use poise::Command; -pub fn to_global_commands() -> Vec<Command<Data, Error>> { +pub fn to_global_commands() -> Vec<Command<Data, Report>> { vec![ ask::ask(), bing::bing(), @@ -21,6 +23,6 @@ pub fn to_global_commands() -> Vec<Command<Data, Error>> { ] } -pub fn to_guild_commands() -> Vec<Command<Data, Error>> { +pub fn to_guild_commands() -> Vec<Command<Data, Report>> { vec![copypasta::copypasta(), teawiespam::teawiespam()] } diff --git a/src/commands/random.rs b/src/commands/random.rs index bc34928..9595d09 100644 --- a/src/commands/random.rs +++ b/src/commands/random.rs @@ -1,44 +1,30 @@ -use crate::{api, consts, utils, Context, Error}; +use crate::{api, consts, utils, Context}; + +use color_eyre::eyre::Result; #[poise::command(slash_command, subcommands("lore", "teawie", "shiggy"))] -pub async fn random(_ctx: Context<'_>) -> Result<(), Error> { +pub async fn random(_ctx: Context<'_>) -> Result<()> { Ok(()) } /// get a random piece of teawie lore! #[poise::command(prefix_command, slash_command)] -pub async fn lore(ctx: Context<'_>) -> Result<(), Error> { - match utils::random_choice(consts::LORE) { - Ok(resp) => { - ctx.say(resp).await?; - Ok(()) - } - Err(why) => { - ctx.say("i can't think of any right now :(").await?; - Err(why) - } - } +pub async fn lore(ctx: Context<'_>) -> Result<()> { + let resp = utils::random_choice(consts::LORE)?; + ctx.say(resp).await?; + Ok(()) } /// get a random teawie #[poise::command(prefix_command, slash_command)] -pub async fn teawie(ctx: Context<'_>) -> Result<(), Error> { - if let Ok(url) = api::guzzle::get_random_teawie().await { - utils::send_url_as_embed(ctx, url).await - } else { - ctx.say("i'm too lazy to send a selfie right now :(") - .await?; - Ok(()) - } +pub async fn teawie(ctx: Context<'_>) -> Result<()> { + let url = api::guzzle::get_random_teawie().await?; + utils::send_url_as_embed(ctx, url).await } /// get a random shiggy #[poise::command(prefix_command, slash_command)] -pub async fn shiggy(ctx: Context<'_>) -> Result<(), Error> { - if let Ok(url) = api::shiggy::get_random_shiggy().await { - utils::send_url_as_embed(ctx, url).await - } else { - ctx.say("i couldn't get a shiggy right now :(").await?; - Ok(()) - } +pub async fn shiggy(ctx: Context<'_>) -> Result<()> { + let url = api::shiggy::get_random_shiggy().await?; + utils::send_url_as_embed(ctx, url).await } diff --git a/src/commands/teawiespam.rs b/src/commands/teawiespam.rs index 4964e90..da01af9 100644 --- a/src/commands/teawiespam.rs +++ b/src/commands/teawiespam.rs @@ -1,13 +1,15 @@ use crate::utils; -use crate::{Context, Error}; +use crate::Context; + +use color_eyre::eyre::Result; use log::*; /// teawie will spam you. #[poise::command(slash_command, prefix_command)] -pub async fn teawiespam(ctx: Context<'_>) -> Result<(), Error> { +pub async fn teawiespam(ctx: Context<'_>) -> Result<()> { let gid = ctx.guild_id().unwrap_or_default(); if !utils::is_guild_allowed(gid) { - info!("not running copypasta command in {gid}"); + info!("not running teawiespam command in {gid}"); return Ok(()); } diff --git a/src/commands/version.rs b/src/commands/version.rs index e87ab4d..c5e97f9 100644 --- a/src/commands/version.rs +++ b/src/commands/version.rs @@ -1,9 +1,11 @@ use crate::colors::Colors; -use crate::{Context, Error}; +use crate::Context; + +use color_eyre::eyre::Result; /// get version info #[poise::command(slash_command)] -pub async fn version(ctx: Context<'_>) -> Result<(), Error> { +pub async fn version(ctx: Context<'_>) -> Result<()> { let sha = option_env!("GIT_SHA").unwrap_or("main"); let revision_url = format!( |
