diff --git a/Cargo.toml b/Cargo.toml index 452aed04..a612dfa9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" twilight-gateway = "0.14.0" twilight-http = "0.14.0" twilight-model = "0.14.0" +twilight-validate = "0.14.0" twilight-util = { version = "0.14.0", features = ["builder", "snowflake"] } twilight-interactions = "0.14.0" twilight-mention = "0.14.0" diff --git a/src/cache/cache_struct.rs b/src/cache/cache_struct.rs index bce5482b..ae015df5 100644 --- a/src/cache/cache_struct.rs +++ b/src/cache/cache_struct.rs @@ -159,7 +159,7 @@ impl Cache { }); if must_fetch { - let channel = bot.http.channel(channel_id).await?.model().await.unwrap(); + let channel = bot.http.channel(channel_id).await?.model().await?; current_channel_id = channel.parent_id; } } @@ -180,7 +180,7 @@ impl Cache { if !self.users.contains_key(&user_id) { let user_get = bot.http.user(user_id).await; let user = match user_get { - Ok(user) => Some(Arc::new(user.model().await.unwrap().into())), + Ok(user) => Some(Arc::new(user.model().await?.into())), Err(why) => { if get_status(&why) == Some(404) { None @@ -220,7 +220,7 @@ impl Cache { } } Ok(wh) => { - let wh = Arc::new(wh.model().await.unwrap()); + let wh = Arc::new(wh.model().await?); self.webhooks.insert(webhook_id, wh.clone()); Some(wh) } @@ -248,7 +248,7 @@ impl Cache { return Err(why.into()); } } - Ok(msg) => Some(Arc::new(msg.model().await.unwrap().into())), + Ok(msg) => Some(Arc::new(msg.model().await?.into())), }; self.messages.insert(message_id, msg, 1).await; @@ -276,7 +276,7 @@ impl Cache { } } }; - Ok(Some(channel.model().await.unwrap())) + Ok(Some(channel.model().await?)) } let Some(channel) = get_channel(bot, channel_id).await? else { diff --git a/src/client/bot.rs b/src/client/bot.rs index ff9cd0fb..43283247 100644 --- a/src/client/bot.rs +++ b/src/client/bot.rs @@ -117,12 +117,14 @@ impl StarboardBot { eprintln!("{msg}"); if let Some(chid) = self.config.error_channel { - let _ = self - .http - .create_message(chid.into_id()) - .content(msg) - .unwrap() - .await; + let ret = self.http.create_message(chid.into_id()).content(msg); + let ret = match ret { + Ok(ret) => ret, + Err(why) => return eprintln!("{why}"), + }; + if let Err(why) = ret.await { + eprintln!("{why}"); + } } } } diff --git a/src/core/autostar.rs b/src/core/autostar.rs index 7e5cdfea..a8d29b37 100644 --- a/src/core/autostar.rs +++ b/src/core/autostar.rs @@ -59,7 +59,7 @@ pub async fn handle(bot: &StarboardBot, event: &MessageCreate) -> StarboardResul event.channel_id ) + &reasons.join("\n - ") }; - notify::notify(bot, event.author.id, &message).await; + notify::notify(bot, event.author.id, &message).await?; } continue; diff --git a/src/core/embedder/handle.rs b/src/core/embedder/handle.rs index b6aecb0d..f1dcdc2e 100644 --- a/src/core/embedder/handle.rs +++ b/src/core/embedder/handle.rs @@ -49,12 +49,9 @@ impl Embedder<'_, '_> { let ret = bot .http .execute_webhook(wh.id, wh.token.as_ref().unwrap()) - .content(&built.top_content) - .unwrap() - .embeds(&built.embeds) - .unwrap() - .attachments(&attachments) - .unwrap() + .content(&built.top_content)? + .embeds(&built.embeds)? + .attachments(&attachments)? .wait() .await; @@ -76,12 +73,9 @@ impl Embedder<'_, '_> { bot.http .create_message(self.config.starboard.channel_id.into_id()) - .content(&built.top_content) - .unwrap() - .embeds(&built.embeds) - .unwrap() - .attachments(&attachments) - .unwrap() + .content(&built.top_content)? + .embeds(&built.embeds)? + .attachments(&attachments)? .await .map_err(|e| e.into()) } @@ -113,18 +107,14 @@ impl Embedder<'_, '_> { if let Some(wh) = wh { bot.http .update_webhook_message(wh.id, wh.token.as_ref().unwrap(), message_id) - .content(Some(&built.top_content)) - .unwrap() - .embeds(Some(&built.embeds)) - .unwrap() + .content(Some(&built.top_content))? + .embeds(Some(&built.embeds))? .await?; } else { bot.http .update_message(sb_channel_id, message_id) - .content(Some(&built.top_content)) - .unwrap() - .embeds(Some(&built.embeds)) - .unwrap() + .content(Some(&built.top_content))? + .embeds(Some(&built.embeds))? .await?; } } @@ -132,14 +122,12 @@ impl Embedder<'_, '_> { if let Some(wh) = wh { bot.http .update_webhook_message(wh.id, wh.token.as_ref().unwrap(), message_id) - .content(Some(&built.top_content)) - .unwrap() + .content(Some(&built.top_content))? .await?; } else { bot.http .update_message(sb_channel_id, message_id) - .content(Some(&built.top_content)) - .unwrap() + .content(Some(&built.top_content))? .await?; } } diff --git a/src/core/starboard/handle.rs b/src/core/starboard/handle.rs index 63aaa928..02e9a2d6 100644 --- a/src/core/starboard/handle.rs +++ b/src/core/starboard/handle.rs @@ -63,15 +63,13 @@ impl RefreshMessage<'_> { self.configs.replace(Arc::new(configs)); } - async fn get_configs(&mut self) -> sqlx::Result>> { + async fn get_configs(&mut self) -> StarboardResult>> { if self.configs.is_none() { let msg = self.get_sql_message().await?; let guild_id = msg.guild_id.into_id(); let channel_id = msg.channel_id.into_id(); - let configs = StarboardConfig::list_for_channel(self.bot, guild_id, channel_id) - .await - .unwrap(); + let configs = StarboardConfig::list_for_channel(self.bot, guild_id, channel_id).await?; self.set_configs(configs); } @@ -262,12 +260,7 @@ impl<'this, 'bot> RefreshStarboard<'this, 'bot> { return Ok(false); } - let msg = embedder - .send(self.refresh.bot) - .await? - .model() - .await - .unwrap(); + let msg = embedder.send(self.refresh.bot).await?.model().await?; StarboardMessage::create( &self.refresh.bot.pool, orig.message_id, diff --git a/src/core/starboard/webhooks.rs b/src/core/starboard/webhooks.rs index 000062f2..cefe4ced 100644 --- a/src/core/starboard/webhooks.rs +++ b/src/core/starboard/webhooks.rs @@ -28,8 +28,7 @@ pub async fn get_valid_webhook( let mut wh = bot .http - .create_webhook(starboard.channel_id.into_id(), "Starboard") - .unwrap(); + .create_webhook(starboard.channel_id.into_id(), "Starboard")?; let bot_user = bot .cache @@ -44,7 +43,7 @@ pub async fn get_valid_webhook( let Ok(wh) = wh.await else { return Ok(None); }; - let wh = Arc::new(wh.model().await.unwrap()); + let wh = Arc::new(wh.model().await?); bot.cache.webhooks.insert(wh.id, wh.clone()); diff --git a/src/database/models/autostar_channel.rs b/src/database/models/autostar_channel.rs index 7c800134..6a52ba1f 100644 --- a/src/database/models/autostar_channel.rs +++ b/src/database/models/autostar_channel.rs @@ -75,11 +75,13 @@ impl AutoStarChannel { .push_bind(self.guild_id) .push(" RETURNING *"); - builder - .build() - .fetch_optional(pool) - .await - .map(|r| r.map(|r| AutoStarChannel::from_row(&r).unwrap())) + let ret = builder.build().fetch_optional(pool).await?; + + if let Some(ret) = ret { + Ok(Some(AutoStarChannel::from_row(&ret)?)) + } else { + Ok(None) + } } pub async fn rename( diff --git a/src/errors.rs b/src/errors.rs index ff96793d..81a75eab 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -11,6 +11,12 @@ pub enum StarboardError { #[error(transparent)] TwilightHttp(#[from] twilight_http::Error), #[error(transparent)] + MessageValidationError(#[from] twilight_validate::message::MessageValidationError), + #[error(transparent)] + ValidationError(#[from] twilight_validate::request::ValidationError), + #[error(transparent)] + DeserializeBodyError(#[from] twilight_http::response::DeserializeBodyError), + #[error(transparent)] Reqwest(#[from] reqwest::Error), #[error(transparent)] InteractionParseError(#[from] twilight_interactions::error::ParseError), diff --git a/src/events/mod.rs b/src/events/mod.rs index c0ace6bc..50bb8d39 100644 --- a/src/events/mod.rs +++ b/src/events/mod.rs @@ -48,8 +48,7 @@ async fn match_events(shard_id: u64, event: Event, bot: Arc) -> St "See `/help` for more information on Starboard. If you don't see ", "slash commands, try reinviting me using the \"Add to Server\" ", "button on my profile.", - )) - .unwrap() + ))? .reply(event.id) .await; } diff --git a/src/interactions/commands/chat/overrides/edit/requirements.rs b/src/interactions/commands/chat/overrides/edit/requirements.rs index 2040c495..bae01e57 100644 --- a/src/interactions/commands/chat/overrides/edit/requirements.rs +++ b/src/interactions/commands/chat/overrides/edit/requirements.rs @@ -70,7 +70,7 @@ impl EditRequirements { let starboard = Starboard::get(&ctx.bot.pool, ov.starboard_id) .await? .unwrap(); - let mut resolved = StarboardConfig::new(starboard, vec![ov]).unwrap(); + let mut resolved = StarboardConfig::new(starboard, vec![ov])?; (resolved.overrides.remove(0), resolved.resolved) }; diff --git a/src/interactions/commands/chat/random.rs b/src/interactions/commands/chat/random.rs index 029f2f68..ee8f275d 100644 --- a/src/interactions/commands/chat/random.rs +++ b/src/interactions/commands/chat/random.rs @@ -42,7 +42,7 @@ async fn get_config( ) -> StarboardResult { let overrides = StarboardOverride::list_by_starboard_and_channels(&bot.pool, sb.id, &[channel_id]).await?; - Ok(StarboardConfig::new(sb, overrides).unwrap()) + Ok(StarboardConfig::new(sb, overrides)?) } async fn get_embedder<'config, 'bot>( diff --git a/src/interactions/context.rs b/src/interactions/context.rs index b8d645ca..d36fe3e7 100644 --- a/src/interactions/context.rs +++ b/src/interactions/context.rs @@ -11,7 +11,7 @@ use twilight_model::{ }; use twilight_util::builder::InteractionResponseDataBuilder; -use crate::client::bot::StarboardBot; +use crate::{client::bot::StarboardBot, errors::StarboardResult}; pub type CommandCtx = Ctx; pub type ComponentCtx = Ctx; @@ -25,7 +25,7 @@ pub struct Ctx { responded: bool, } -type TwResult = Result, twilight_http::Error>; +type TwResult = StarboardResult>; impl Ctx { pub fn new(shard_id: u64, bot: Arc, interaction: Interaction, data: T) -> Self { @@ -60,16 +60,16 @@ impl Ctx { followup = followup.allowed_mentions(Some(mentions)); } if let Some(attachments) = &data.attachments { - followup = followup.attachments(attachments).unwrap(); + followup = followup.attachments(attachments)?; } if let Some(components) = &data.components { - followup = followup.components(components).unwrap(); + followup = followup.components(components)?; } if let Some(content) = &data.content { - followup = followup.content(content).unwrap(); + followup = followup.content(content)?; } if let Some(embeds) = &data.embeds { - followup = followup.embeds(embeds).unwrap(); + followup = followup.embeds(embeds)?; } if let Some(flags) = data.flags { followup = followup.flags(flags); @@ -78,7 +78,7 @@ impl Ctx { followup = followup.tts(tts); } - followup.await + followup.await.map_err(|e| e.into()) } else { i.create_response( self.interaction.id, @@ -89,7 +89,9 @@ impl Ctx { self.responded = true; - i.response(&self.interaction.token).await + i.response(&self.interaction.token) + .await + .map_err(|e| e.into()) } } diff --git a/src/owner/commands/sql.rs b/src/owner/commands/sql.rs index f13ea1a5..f8307367 100644 --- a/src/owner/commands/sql.rs +++ b/src/owner/commands/sql.rs @@ -105,8 +105,7 @@ pub async fn run_sql( let ret = bot .http .update_message(channel_id, to_edit) - .content(Some(&final_result)) - .unwrap() + .content(Some(&final_result))? .await; if ret.is_ok() { @@ -117,13 +116,11 @@ pub async fn run_sql( let msg = bot .http .create_message(channel_id) - .content(&final_result) - .unwrap() + .content(&final_result)? .reply(message_id) .await? .model() - .await - .unwrap(); + .await?; bot.cache.responses.insert(message_id, msg.id, 1).await; bot.cache.responses.wait().await.unwrap(); diff --git a/src/owner/handle.rs b/src/owner/handle.rs index a29e51ff..24497131 100644 --- a/src/owner/handle.rs +++ b/src/owner/handle.rs @@ -45,8 +45,7 @@ pub async fn handle_message( if let Err(err) = ret { bot.http .create_message(channel_id) - .content(&err.to_string()) - .unwrap() + .content(&err.to_string())? .await?; } diff --git a/src/utils/dm.rs b/src/utils/dm.rs index e5f77283..7162867d 100644 --- a/src/utils/dm.rs +++ b/src/utils/dm.rs @@ -1,19 +1,15 @@ use twilight_http::request::channel::message::CreateMessage; use twilight_model::id::{marker::UserMarker, Id}; -use crate::client::bot::StarboardBot; +use crate::{client::bot::StarboardBot, errors::StarboardResult}; -pub async fn dm( - bot: &StarboardBot, - user_id: Id, -) -> Result { +pub async fn dm(bot: &StarboardBot, user_id: Id) -> StarboardResult { let dm_channel = bot .http .create_private_channel(user_id) .await? .model() - .await - .unwrap() + .await? .id; Ok(bot.http.create_message(dm_channel)) } diff --git a/src/utils/notify.rs b/src/utils/notify.rs index 64dec4d7..783e6abe 100644 --- a/src/utils/notify.rs +++ b/src/utils/notify.rs @@ -3,22 +3,21 @@ use twilight_model::{ id::{marker::UserMarker, Id}, }; -use crate::client::bot::StarboardBot; +use crate::{client::bot::StarboardBot, errors::StarboardResult}; use super::dm; -pub async fn notify(bot: &StarboardBot, user_id: Id, message: &str) { +pub async fn notify( + bot: &StarboardBot, + user_id: Id, + message: &str, +) -> StarboardResult<()> { if bot.config.development { println!("Development, skipping notification:"); println!("{message}"); - return; + return Ok(()); } - let create = match dm::dm(bot, user_id).await { - Err(_) => return, - Ok(create) => create, - }; - let comp = Component::ActionRow(ActionRow { components: vec![Component::Button(Button { label: Some("Dismiss".to_string()), @@ -30,10 +29,11 @@ pub async fn notify(bot: &StarboardBot, user_id: Id, message: &str) })], }); - let _ = create - .content(message) - .unwrap() - .components(&[comp]) - .unwrap() + let _ = dm::dm(bot, user_id) + .await? + .content(message)? + .components(&[comp])? .await; + + Ok(()) } diff --git a/src/utils/views/confirm.rs b/src/utils/views/confirm.rs index f270235d..04fe5675 100644 --- a/src/utils/views/confirm.rs +++ b/src/utils/views/confirm.rs @@ -77,7 +77,7 @@ pub async fn simple( .content(prompt) .components(components(danger)) .build(); - let msg = ctx.respond(cmd).await?.model().await.unwrap(); + let msg = ctx.respond(cmd).await?.model().await?; let ret = wait_for_result( ctx.bot.clone(), msg.id, @@ -95,10 +95,8 @@ pub async fn simple( ctx.bot .http .update_message(msg.channel_id, msg.id) - .content(Some("Canceled.")) - .unwrap() - .components(Some(&[])) - .unwrap() + .content(Some("Canceled."))? + .components(Some(&[]))? .await?; } diff --git a/src/utils/views/paginator.rs b/src/utils/views/paginator.rs index b2fd78d7..5af99ed2 100644 --- a/src/utils/views/paginator.rs +++ b/src/utils/views/paginator.rs @@ -105,7 +105,7 @@ pub async fn simple( } let msg = ctx.respond(int_data.build()).await?; - let msg_id = msg.model().await.unwrap().id; + let msg_id = msg.model().await?.id; message_id = Some(msg_id); msg_id }; @@ -140,7 +140,7 @@ pub async fn simple( let i = ctx.bot.interaction_client().await; let mut update = i.update_response(&ctx.interaction.token); let comp = components(current_page, last_page, true); - update = update.components(Some(&comp)).unwrap(); + update = update.components(Some(&comp))?; update.await?; return Ok(()); };