refactor, cleanup
This commit is contained in:
@@ -3,19 +3,17 @@ use std::sync::Arc;
|
||||
use serenity::all::{CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption};
|
||||
use serenity::model::prelude::GuildId;
|
||||
use serenity::{prelude::*, async_trait};
|
||||
use songbird::input::{AuxMetadata, Input, YoutubeDl};
|
||||
use songbird::input::{Input, YoutubeDl};
|
||||
use songbird::tracks::TrackHandle;
|
||||
use songbird::{Call, Event, EventHandler, Songbird, TrackEvent};
|
||||
use songbird::{Event, EventHandler, Songbird, TrackEvent};
|
||||
|
||||
use crate::database::guilds::GuildCache;
|
||||
use crate::bot::commands::audio::leave_voice_channel;
|
||||
use crate::data::guilds::GuildCache;
|
||||
use crate::bot::ytdlp::{PlaylistItem, YtDlp};
|
||||
use crate::error::{SirenResult, Error as SirenError};
|
||||
use crate::HttpKey;
|
||||
|
||||
use super::{
|
||||
create_response, edit_response, get_songbird, is_valid_url, join_voice_channel,
|
||||
leave_voice_channel,
|
||||
};
|
||||
use super::{create_response, edit_response, get_songbird, is_valid_url, join_voice_channel};
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
// Process the command options
|
||||
@@ -87,10 +85,7 @@ pub async fn play_track(
|
||||
) -> SirenResult<i32> {
|
||||
let mut track_count = 0;
|
||||
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 mut handler = handler_lock.lock().await;
|
||||
let guild = GuildCache::get_by_id(guild_id.get() as i64).await?.unwrap();
|
||||
let valid = is_valid_url(&track_url);
|
||||
// Check if the URL is valid
|
||||
@@ -123,71 +118,49 @@ pub async fn play_track(
|
||||
}
|
||||
// Add each track to the queue
|
||||
for item in playlist_items {
|
||||
match add_song(
|
||||
ctx,
|
||||
handler_lock.clone(),
|
||||
&item.url,
|
||||
is_queue_empty,
|
||||
guild.volume as f32 / 100.0,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(metadata) => {
|
||||
let track_title = metadata.title.unwrap();
|
||||
log::debug!("Added track: {}", track_title);
|
||||
let mut handler = handler_lock.lock().await;
|
||||
// handler.remove_all_global_events();
|
||||
handler.add_global_event(
|
||||
Event::Track(TrackEvent::End),
|
||||
TrackEndNotifier {
|
||||
guild_id,
|
||||
call: manager.clone(),
|
||||
},
|
||||
);
|
||||
track_count += 1;
|
||||
}
|
||||
let volume = guild.volume as f32 / 100.0;
|
||||
let http_client = {
|
||||
let data = ctx.data.read().await;
|
||||
data
|
||||
.get::<HttpKey>()
|
||||
.cloned()
|
||||
.expect("Guaranteed to exist in the typemap.")
|
||||
};
|
||||
let source = YoutubeDl::new(http_client, item.url.to_owned());
|
||||
let mut input: Input = source.into();
|
||||
let metadata = match input.aux_metadata().await {
|
||||
Ok(metadata) => metadata,
|
||||
Err(err) => {
|
||||
log::warn!("Failed to add song: {}", err);
|
||||
if let Err(why) = leave_voice_channel(&manager, &guild_id).await {
|
||||
log::error!("Failed to leave voice channel: {}", why);
|
||||
}
|
||||
log::warn!("Failed to get metadata for track: {err}");
|
||||
let _ = leave_voice_channel(&manager, &guild_id).await;
|
||||
return Err(SirenError::new(422, err.to_string()));
|
||||
}
|
||||
};
|
||||
let track_handle: TrackHandle;
|
||||
let is_queue_empty = handler.queue().is_empty();
|
||||
if is_queue_empty {
|
||||
track_handle = handler.play_input(input);
|
||||
} else {
|
||||
track_handle = handler.enqueue_input(input).await;
|
||||
}
|
||||
// Set the volume
|
||||
let _ = track_handle.set_volume(volume);
|
||||
let track_title = metadata.title.unwrap();
|
||||
log::debug!("<{guild_id}> Added track: {}", track_title);
|
||||
handler.remove_all_global_events();
|
||||
handler.add_global_event(
|
||||
Event::Track(TrackEvent::End),
|
||||
TrackEndNotifier {
|
||||
guild_id,
|
||||
call: manager.clone(),
|
||||
},
|
||||
);
|
||||
track_count += 1;
|
||||
}
|
||||
}
|
||||
Ok(track_count)
|
||||
}
|
||||
|
||||
async fn add_song(
|
||||
ctx: &Context,
|
||||
call: Arc<Mutex<Call>>,
|
||||
url: &str,
|
||||
lazy: bool,
|
||||
volume: f32,
|
||||
) -> SirenResult<AuxMetadata> {
|
||||
let http_client = {
|
||||
let data = ctx.data.read().await;
|
||||
data
|
||||
.get::<HttpKey>()
|
||||
.cloned()
|
||||
.expect("Guaranteed to exist in the typemap.")
|
||||
};
|
||||
let source = YoutubeDl::new(http_client, url.to_owned());
|
||||
let mut handler = call.lock().await;
|
||||
let mut input: Input = source.into();
|
||||
let metadata = input.aux_metadata().await.unwrap();
|
||||
let track_handle: TrackHandle;
|
||||
if lazy {
|
||||
track_handle = handler.play_input(input);
|
||||
} else {
|
||||
track_handle = handler.enqueue_input(input).await;
|
||||
}
|
||||
// Set the volume
|
||||
let _ = track_handle.set_volume(volume);
|
||||
Ok(metadata)
|
||||
}
|
||||
|
||||
pub fn get_playlist_urls(url: &str) -> SirenResult<Vec<PlaylistItem>> {
|
||||
let output = YtDlp::new()
|
||||
.arg("--flat-playlist")
|
||||
|
||||
Reference in New Issue
Block a user