Reduce the amount of panics

main
DaXcess 2024-08-15 08:35:55 +02:00
parent ef86e1e2d3
commit 83e432254b
No known key found for this signature in database
GPG Key ID: CF78CC72F0FD5EAD
8 changed files with 43 additions and 14 deletions

View File

@ -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

2
Cargo.lock generated
View File

@ -3721,7 +3721,7 @@ dependencies = [
[[package]]
name = "spoticord"
version = "2.2.0"
version = "2.2.1"
dependencies = [
"anyhow",
"dotenvy",

View File

@ -1,6 +1,6 @@
[package]
name = "spoticord"
version = "2.2.0"
version = "2.2.1"
edition = "2021"
rust-version = "1.75.0"

View File

@ -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(

View File

@ -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),

View File

@ -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(

View File

@ -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(

View File

@ -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(