Cleanup, working on getting play to work
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use log::debug;
|
||||
use log::{debug, warn};
|
||||
|
||||
use serenity::client::Cache;
|
||||
use serenity::model::application::interaction::{InteractionResponseType, application_command::ApplicationCommandInteraction};
|
||||
use serenity::model::prelude::{GuildId, ChannelId};
|
||||
use serenity::model::user::User;
|
||||
use serenity::prelude::*;
|
||||
use siren::ServiceError;
|
||||
use songbird::{Call, Songbird};
|
||||
use songbird::input::{Restartable, Input, Metadata, error::Error as SongbirdError};
|
||||
|
||||
@@ -18,36 +18,29 @@ pub mod skip;
|
||||
pub mod stop;
|
||||
pub mod volume;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AudioConfigs;
|
||||
|
||||
impl TypeMapKey for AudioConfigs {
|
||||
type Value = Arc<RwLock<HashMap<GuildId, AudioConfig>>>;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AudioConfig {
|
||||
pub volume: f32
|
||||
}
|
||||
|
||||
pub async fn join(cache: &Arc<Cache>, manager: Arc<Songbird>, guild_id_option: &Option<GuildId>, user: &User) -> Result<(), String> {
|
||||
pub async fn join_by_user(cache: &Arc<Cache>, manager: Arc<Songbird>, guild_id_option: &Option<GuildId>, user: &User) -> Result<(), ServiceError> {
|
||||
let guild_id = match guild_id_option {
|
||||
Some(g) => g,
|
||||
None => {
|
||||
return Err(format!("{}", "No guild ID set"));
|
||||
}
|
||||
None => return Err(ServiceError { status: 422, message: format!("{}", "No guild ID set") })
|
||||
};
|
||||
|
||||
let channel_id = match find_voice_channel(cache, &guild_id, &user) {
|
||||
Ok(channel) => channel,
|
||||
Err(err) => return Err(format!("{}", err))
|
||||
Err(err) => return Err(ServiceError { status: 500, message: err.to_string() })
|
||||
};
|
||||
|
||||
debug!("<{}> Joining channel {}", guild_id.0, channel_id);
|
||||
join(manager, guild_id, &channel_id).await
|
||||
}
|
||||
|
||||
pub async fn join(manager: Arc<Songbird>, guild_id: &GuildId, channel_id: &ChannelId) -> Result<(), ServiceError> {
|
||||
debug!("<{}> Joining channel {}", guild_id.0, channel_id.0);
|
||||
let (_handle_lock, success) = manager.join(guild_id.to_owned(), channel_id.to_owned()).await;
|
||||
match success {
|
||||
Ok(s) => Ok(s),
|
||||
Err(err) => Err(format!("{}", err))
|
||||
Err(err) => {
|
||||
warn!("Failed to join channel: {:?}", err);
|
||||
Err(ServiceError { status: 500, message: err.to_string() })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,18 +3,16 @@ use std::sync::Arc;
|
||||
use log::{debug, warn, error};
|
||||
|
||||
use serenity::model::prelude::GuildId;
|
||||
use serenity::model::user::User;
|
||||
use serenity::{prelude::*, async_trait};
|
||||
use serenity::builder::CreateApplicationCommand;
|
||||
use serenity::model::application::interaction::application_command::ApplicationCommandInteraction;
|
||||
use siren::ServiceError;
|
||||
use songbird::EventHandler;
|
||||
use songbird::{EventHandler, Songbird};
|
||||
|
||||
use crate::AppState;
|
||||
use crate::bot::commands::audio::{join, leave, add_song, get_songbird};
|
||||
use crate::bot::commands::audio::{leave, add_song, get_songbird};
|
||||
use crate::db::guilds::QueryGuild;
|
||||
|
||||
use super::{create_response, edit_response};
|
||||
use super::{create_response, edit_response, join_by_user};
|
||||
|
||||
pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
// Get the track url
|
||||
@@ -54,7 +52,7 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
}
|
||||
|
||||
let manager = get_songbird(ctx).await;
|
||||
match join(&ctx.cache, manager,&command.guild_id, &command.user).await {
|
||||
match join_by_user(&ctx.cache, manager,&command.guild_id, &command.user).await {
|
||||
Ok(_) => {
|
||||
let guild_id = match command.guild_id {
|
||||
Some(g) => g,
|
||||
@@ -66,37 +64,20 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
}
|
||||
};
|
||||
debug!("Play command executed with track: {:?}", track_url);
|
||||
|
||||
let manager = get_songbird(ctx).await;
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let is_queue_empty = {
|
||||
let call_handler = handler_lock.lock().await;
|
||||
call_handler.queue().is_empty()
|
||||
};
|
||||
let guild = QueryGuild::get(guild_id.0 as i64).unwrap();
|
||||
match add_song(handler_lock.clone(), &track_url, is_queue_empty, Some(guild.volume)).await {
|
||||
Ok(added_song) => {
|
||||
let track_title = added_song.title.unwrap();
|
||||
debug!("Added track: {}", track_title);
|
||||
if let Err(why) = edit_response(&ctx, &command, format!("Added track to queue: {}", track_title)).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
let mut handler = handler_lock.lock().await;
|
||||
handler.remove_all_global_events();
|
||||
handler.add_global_event(songbird::Event::Track(songbird::TrackEvent::End), TrackEndNotifier { guild_id, call: manager })
|
||||
match play_track(manager, guild_id, track_url).await {
|
||||
Ok(_) => {
|
||||
if let Err(why) = edit_response(&ctx, &command, "Playing track".to_string()).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
Err(why) => {
|
||||
warn!("Failed to add song: {}", why);
|
||||
if let Err(why) = edit_response(&ctx, &command, format!("Failed to add song: {}", why)).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
if let Err(why) = leave(manager, &command.guild_id).await {
|
||||
error!("Failed to leave voice channel: {}", why);
|
||||
}
|
||||
return;
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("Failed to play track: {}", err);
|
||||
if let Err(why) = edit_response(&ctx, &command, format!("Failed to play track: {}", err)).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("{}", err);
|
||||
@@ -107,38 +88,31 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn play(state: Arc<AppState>, guild_id: Option<GuildId>, user: &User, track_url: String) -> Result<(), ServiceError> {
|
||||
match join(&state.cache, Arc::clone(&state.songbird), &guild_id, user).await {
|
||||
Ok(_) => {
|
||||
let guild_id = match guild_id {
|
||||
Some(g) => g,
|
||||
None => {
|
||||
return Err(ServiceError {
|
||||
status: 422,
|
||||
message: "No guild ID set".to_string()
|
||||
});
|
||||
}
|
||||
};
|
||||
if let Some(handler_lock) = state.songbird.get(guild_id) {
|
||||
let is_queue_empty = {
|
||||
let call_handler = handler_lock.lock().await;
|
||||
call_handler.queue().is_empty()
|
||||
};
|
||||
let guild = QueryGuild::get(guild_id.0 as i64)?;
|
||||
match add_song(handler_lock.clone(), &track_url, is_queue_empty, Some(guild.volume)).await {
|
||||
Ok(_) => {},
|
||||
Err(_) => {}
|
||||
pub async fn play_track(manager: Arc<Songbird>, guild_id: GuildId, track_url: String) -> Result<(), ServiceError> {
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let is_queue_empty = {
|
||||
let call_handler = handler_lock.lock().await;
|
||||
call_handler.queue().is_empty()
|
||||
};
|
||||
let guild = QueryGuild::get(guild_id.0 as i64)?;
|
||||
match add_song(handler_lock.clone(), &track_url, is_queue_empty, Some(guild.volume as f32)).await {
|
||||
Ok(added_song) => {
|
||||
let track_title = added_song.title.unwrap();
|
||||
debug!("Added track: {}", track_title);
|
||||
let mut handler = handler_lock.lock().await;
|
||||
handler.remove_all_global_events();
|
||||
handler.add_global_event(songbird::Event::Track(songbird::TrackEvent::End), TrackEndNotifier { guild_id, call: manager })
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("Failed to add song: {}", err);
|
||||
if let Err(why) = leave(manager, &Some(guild_id)).await {
|
||||
error!("Failed to leave voice channel: {}", why);
|
||||
}
|
||||
return Err(ServiceError { status: 422, message: err.to_string() })
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
Err(err) => {
|
||||
return Err(ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
|
||||
@@ -57,7 +57,7 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
return;
|
||||
}
|
||||
};
|
||||
let _ = InsertGuild::update_audio(guild_id.0 as i64, bound_volume);
|
||||
let _ = InsertGuild::update_audio(guild_id.0 as i64, bound_volume as f64);
|
||||
let manager = get_songbird(ctx).await;
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let handler = handler_lock.lock().await;
|
||||
|
||||
Reference in New Issue
Block a user