// src/commands/leave.rs use super::{CommandHandler, SlashCommand}; use anyhow::{Context as _, Result}; use lavalink_rs::LavalinkClient; use serenity::{ builder::{CreateApplicationCommandOption, EditInteractionResponse}, client::Context, model::prelude::interaction::application_command::ApplicationCommandInteraction, }; use tracing::instrument; use futures::future::BoxFuture; inventory::submit! { SlashCommand { name: "leave", description: "Makes the bot leave the current voice channel.", options: || Vec::new(), // No options handler: handle_leave, } } #[instrument(skip(ctx, interaction, lavalink))] fn handle_leave<'a>( ctx: &'a Context, interaction: &'a ApplicationCommandInteraction, lavalink: &'a LavalinkClient, ) -> BoxFuture<'a, Result<()>> { Box::pin(async move { let guild_id = interaction.guild_id.context("Command must be used in a guild")?; // Check if connected before trying to leave if lavalink.get_player(guild_id).is_none() { interaction.edit_original_response(&ctx.http, EditInteractionResponse::new().content("Bot is not in a voice channel.") ).await?; return Ok(()); // Not an error, just inform user } // Destroy the Lavalink session for this guild lavalink.destroy_session(guild_id).await?; interaction.edit_original_response(&ctx.http, EditInteractionResponse::new().content("Left the voice channel.") ).await?; Ok(()) }) }