From 9798a178d7bfea69f81f6d514cf614f059d23774 Mon Sep 17 00:00:00 2001 From: DaXcess Date: Sat, 5 Nov 2022 18:15:23 +0100 Subject: [PATCH] Fixed /join when switching channels, minor compatibility improvements --- src/bot/commands/music/join.rs | 14 +++++++++++++- src/bot/commands/music/playing.rs | 31 +++++++++++++++++++++---------- src/main.rs | 26 ++++++++++++++++++++++---- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/bot/commands/music/join.rs b/src/bot/commands/music/join.rs index 105ba99..bac365c 100644 --- a/src/bot/commands/music/join.rs +++ b/src/bot/commands/music/join.rs @@ -1,3 +1,4 @@ +use log::trace; use serenity::{ builder::CreateApplicationCommand, model::prelude::interaction::application_command::ApplicationCommandInteraction, @@ -45,7 +46,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu let mut session_manager = data.get::().unwrap().clone(); // Check if another session is already active in this server - let session_opt = session_manager.get_session(guild.id).await; + let mut session_opt = session_manager.get_session(guild.id).await; if let Some(session) = &session_opt { if let Some(owner) = session.get_owner().await { @@ -94,6 +95,17 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu defer_message(&ctx, &command, false).await; + if let Some(session) = &session_opt { + trace!("{} != {}", session.get_channel_id(), channel_id); + if session.get_channel_id() != channel_id { + session.disconnect().await; + session_opt = None; + + // Give serenity/songbird some time to register the disconnect + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + } + } + if let Some(session) = &session_opt { if let Err(why) = session.update_owner(&ctx, command.user.id).await { // Need to link first diff --git a/src/bot/commands/music/playing.rs b/src/bot/commands/music/playing.rs index 8994a87..0ff0407 100644 --- a/src/bot/commands/music/playing.rs +++ b/src/bot/commands/music/playing.rs @@ -11,7 +11,10 @@ use serenity::{ use crate::{ bot::commands::{respond_message, CommandOutput}, session::manager::SessionManager, - utils::{embed::{EmbedBuilder, Status}, self}, + utils::{ + self, + embed::{EmbedBuilder, Status}, + }, }; pub const NAME: &str = "playing"; @@ -81,7 +84,11 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu }; // Create title - let title = format!("{} - {}", pbi.get_artists().unwrap(), pbi.get_name().unwrap()); + let title = format!( + "{} - {}", + pbi.get_artists().unwrap(), + pbi.get_name().unwrap() + ); // Create description let mut description = String::new(); @@ -100,7 +107,11 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu } description.push_str("\n:alarm_clock: "); - description.push_str(&format!("{} / {}", utils::time_to_str(position / 1000), utils::time_to_str(pbi.duration_ms / 1000))); + description.push_str(&format!( + "{} / {}", + utils::time_to_str(position / 1000), + utils::time_to_str(pbi.duration_ms / 1000) + )); // Get owner of session let owner = match utils::discord::get_user(&ctx, owner).await { @@ -116,7 +127,10 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu &command, EmbedBuilder::new() .title("[INTERNAL ERROR] Cannot get track info") - .description(format!("Could not find user with id {}\nThis is an issue with the bot!", owner)) + .description(format!( + "Could not find user with id {}\nThis is an issue with the bot!", + owner + )) .status(Status::Error) .build(), true, @@ -136,18 +150,15 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu .kind(InteractionResponseType::ChannelMessageWithSource) .interaction_response_data(|message| { message - .embed(|embed| - embed - .author(|author| - author + .embed(|embed| embed + .author(|author| author .name("Currently Playing") .icon_url("https://www.freepnglogos.com/uploads/spotify-logo-png/file-spotify-logo-png-4.png") ) .title(title) .url(format!("https://open.spotify.com/{}/{}", audio_type, spotify_id.to_base62().unwrap())) .description(description) - .footer(|footer| - footer + .footer(|footer| footer .text(&owner.name) .icon_url(owner.face()) ) diff --git a/src/main.rs b/src/main.rs index aabe8a9..92e5dcc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,14 +4,16 @@ use dotenv::dotenv; use log::*; use serenity::{framework::StandardFramework, prelude::GatewayIntents, Client}; use songbird::SerenityInit; -use std::{env, process::exit}; -use tokio::signal::unix::SignalKind; +use std::{any::Any, env, process::exit}; use crate::{ bot::commands::CommandManager, database::Database, session::manager::SessionManager, stats::StatsManager, }; +#[cfg(unix)] +use tokio::signal::unix::SignalKind; + mod audio; mod bot; mod consts; @@ -103,8 +105,14 @@ async fn main() { let shard_manager = client.shard_manager.clone(); let cache = client.cache_and_http.cache.clone(); + let mut term: Option>; + #[cfg(unix)] - let mut sigterm = tokio::signal::unix::signal(SignalKind::terminate()).unwrap(); + { + term = Some(Box::new( + tokio::signal::unix::signal(SignalKind::terminate()).unwrap(), + )); + } // Background tasks tokio::spawn(async move { @@ -134,7 +142,17 @@ async fn main() { break; } - _ = sigterm.recv() => { + _ = async { + match term { + Some(ref mut term) => { + let term = term.downcast_mut::().unwrap(); + + term.recv().await + } + + _ => None + } + }, if term.is_some() => { info!("Received terminate signal, shutting down..."); shard_manager.lock().await.shutdown_all().await;