From 83e432254b20d5b0aee24db7ed2a8e4ff9f752c6 Mon Sep 17 00:00:00 2001 From: DaXcess Date: Thu, 15 Aug 2024 08:35:55 +0200 Subject: [PATCH] Reduce the amount of panics --- CHANGELOG.md | 7 +++++- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/music/disconnect.rs | 2 +- src/commands/music/join.rs | 38 ++++++++++++++++++++++++++------ src/commands/music/lyrics.rs | 2 +- src/commands/music/playing.rs | 2 +- src/commands/music/stop.rs | 2 +- 8 files changed, 43 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c23f017..1937c8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog -## 2.2.0 | TBD +## 2.2.1 | TBD + +- Fixed a bug where uncached guilds would panic the bot +- Fixed small issue with embed styling + +## 2.2.0 | August 13th 2024 ### Changes diff --git a/Cargo.lock b/Cargo.lock index a8274f6..6c808fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3721,7 +3721,7 @@ dependencies = [ [[package]] name = "spoticord" -version = "2.2.0" +version = "2.2.1" dependencies = [ "anyhow", "dotenvy", diff --git a/Cargo.toml b/Cargo.toml index 7fa4cfb..deee9a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "spoticord" -version = "2.2.0" +version = "2.2.1" edition = "2021" rust-version = "1.75.0" diff --git a/src/commands/music/disconnect.rs b/src/commands/music/disconnect.rs index b36d999..5c45701 100644 --- a/src/commands/music/disconnect.rs +++ b/src/commands/music/disconnect.rs @@ -9,7 +9,7 @@ use crate::bot::Context; #[poise::command(slash_command, guild_only)] pub async fn disconnect(ctx: Context<'_>) -> Result<(), Error> { let manager = ctx.data(); - let guild = ctx.guild().expect("poise lied to me").id; + let guild = ctx.guild_id().expect("poise lied to me"); let Some(session) = manager.get_session(SessionQuery::Guild(guild)) else { ctx.send( diff --git a/src/commands/music/join.rs b/src/commands/music/join.rs index 258fa6e..d522f1a 100644 --- a/src/commands/music/join.rs +++ b/src/commands/music/join.rs @@ -4,7 +4,7 @@ use anyhow::Result; use log::error; use poise::CreateReply; use serenity::all::{ - Channel, ChannelId, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter, Guild, UserId, + Channel, ChannelId, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter, UserId, }; use spoticord_database::error::DatabaseError; use spoticord_session::manager::SessionQuery; @@ -15,9 +15,30 @@ use crate::bot::Context; /// Join the current voice channel #[poise::command(slash_command, guild_only)] pub async fn join(ctx: Context<'_>) -> Result<()> { - let guild: Guild = ctx.guild().expect("poise lied to me").clone(); + let guild = ctx.guild_id().expect("poise lied to me"); let manager = ctx.data(); + let Some(guild) = guild + .to_guild_cached(ctx.serenity_context()) + .map(|guild| guild.clone()) + else { + error!("Unable to fetch guild from cache, how did we get here?"); + + ctx.send( + CreateReply::default() + .embed( + CreateEmbed::new() + .title("An error occured") + .description("This server hasn't been cached yet?") + .color(Colors::Error), + ) + .ephemeral(true), + ) + .await?; + + return Ok(()); + }; + let Some(channel) = guild .voice_states .get(&ctx.author().id) @@ -98,8 +119,6 @@ pub async fn join(ctx: Context<'_>) -> Result<()> { return Ok(()); } - ctx.defer().await?; - let mut session_opt = manager.get_session(SessionQuery::Guild(guild.id)); // Check if this server already has a session active @@ -134,7 +153,8 @@ pub async fn join(ctx: Context<'_>) -> Result<()> { "You are already using Spoticord in `{}`\n\n\ Stop playing in that server first before starting a new session.", spoticord_utils::discord::escape(server_name) - )), + )) + .color(Colors::Error), ) .ephemeral(true), ) @@ -143,6 +163,8 @@ pub async fn join(ctx: Context<'_>) -> Result<()> { return Ok(()); } + ctx.defer().await?; + if let Some(session) = &session_opt { if session.voice_channel() != channel { session.disconnect().await; @@ -163,7 +185,7 @@ pub async fn join(ctx: Context<'_>) -> Result<()> { CreateEmbed::new() .title("Failed to reactivate session") .description( - "An error occured whilst trying to reactivate the session.", + "An error occured whilst trying to reactivate the session. Please try again.", ) .color(Colors::Error), ) @@ -190,7 +212,9 @@ pub async fn join(ctx: Context<'_>) -> Result<()> { .embed( CreateEmbed::new() .title("Failed to create session") - .description("An error occured whilst trying to create a session.") + .description( + "An error occured whilst trying to create a session. Please try again.", + ) .color(Colors::Error), ) .ephemeral(true), diff --git a/src/commands/music/lyrics.rs b/src/commands/music/lyrics.rs index a2a5428..1021ee6 100644 --- a/src/commands/music/lyrics.rs +++ b/src/commands/music/lyrics.rs @@ -10,7 +10,7 @@ use crate::bot::Context; #[poise::command(slash_command, guild_only)] pub async fn lyrics(ctx: Context<'_>) -> Result<()> { let manager = ctx.data(); - let guild = ctx.guild().expect("poise lied to me").id; + let guild = ctx.guild_id().expect("poise lied to me"); let Some(session) = manager.get_session(SessionQuery::Guild(guild)) else { ctx.send( diff --git a/src/commands/music/playing.rs b/src/commands/music/playing.rs index b6d46f4..45f31d8 100644 --- a/src/commands/music/playing.rs +++ b/src/commands/music/playing.rs @@ -10,7 +10,7 @@ use crate::bot::Context; #[poise::command(slash_command, guild_only)] pub async fn playing(ctx: Context<'_>) -> Result<()> { let manager = ctx.data(); - let guild = ctx.guild().expect("poise lied to me").id; + let guild = ctx.guild_id().expect("poise lied to me"); let Some(session) = manager.get_session(SessionQuery::Guild(guild)) else { ctx.send( diff --git a/src/commands/music/stop.rs b/src/commands/music/stop.rs index 90e3a2b..e044fe8 100644 --- a/src/commands/music/stop.rs +++ b/src/commands/music/stop.rs @@ -9,7 +9,7 @@ use crate::bot::Context; #[poise::command(slash_command, guild_only)] pub async fn stop(ctx: Context<'_>) -> Result<(), Error> { let manager = ctx.data(); - let guild = ctx.guild().expect("poise lied to me").id; + let guild = ctx.guild_id().expect("poise lied to me"); let Some(session) = manager.get_session(SessionQuery::Guild(guild)) else { ctx.send(