Cleanup commands

This commit is contained in:
2024-09-05 13:12:39 -04:00
parent 399eb8a29d
commit 3994c18c49
9 changed files with 182 additions and 278 deletions

View File

@@ -5,63 +5,45 @@ use serenity::model::prelude::GuildId;
use serenity::{prelude::*, async_trait};
use songbird::input::{AuxMetadata, Input, YoutubeDl};
use songbird::tracks::TrackHandle;
use songbird::{Call, EventHandler, Songbird};
use songbird::{Call, Event, EventHandler, Songbird, TrackEvent};
use crate::bot::guilds::GuildCache;
use crate::bot::ytdlp::{PlaylistItem, YtDlp};
use crate::bot::commands::audio::{create_response, edit_response, leave_voice_channel};
use crate::error::{SirenResult, Error as SirenError};
use crate::HttpKey;
use super::{get_songbird, is_valid_url, join_by_user};
use super::{create_response, edit_response, get_songbird, is_valid_url, join_voice_channel, leave_voice_channel};
pub async fn run(ctx: &Context, command: &CommandInteraction) {
// Get the track url
let track_url = match command.data.options.get(0) {
Some(o) => match &o.value.as_str() {
Some(s) => s.to_owned(),
None => {
log::warn!("Missing track option");
if let Err(why) =
create_response(&ctx, &command, format!("Track option is missing")).await
{
log::error!("Failed to create response message: {}", why);
}
return;
}
},
// Process the command options
let track_url = match command.data.options.first() {
Some(o) => &o.value.as_str().unwrap(),
None => {
log::warn!("Missing track option");
if let Err(why) = create_response(&ctx, &command, format!("Track option is missing")).await {
log::error!("Failed to create response message: {}", why);
}
log::warn!("{} attempted to play a track without a track option", command.user.id.get());
create_response(&ctx, &command, format!("Track option is missing")).await;
return;
}
};
// Create the initial response
if let Err(why) = create_response(&ctx, &command, format!("Processing command...")).await {
log::error!("Failed to create response message: {}", why);
return;
}
create_response(&ctx, &command, format!(".....")).await;
// Get the songbird manager
let manager = get_songbird(ctx).await;
// Extract the guild ID
let guild_id = match &command.guild_id {
Some(guild_id) => guild_id,
None => {
if let Err(why) =
edit_response(&ctx, &command, "Unable to join voice channel".to_string()).await
{
log::error!("Failed to edit response message: {}", why);
}
edit_response(&ctx, &command, "Unable to find the current server ID".to_string()).await;
return;
}
};
// Join the user's voice channel
match join_by_user(&ctx.cache, &manager, guild_id, &command.user).await {
Ok(_) => {
log::debug!("Play command executed with track: {:?}", track_url);
match join_voice_channel(&ctx.cache, &manager, guild_id, &command.user).await {
Ok(channel_id) => {
log::debug!("<{guild_id}> Play command executed on {channel_id} with track: {track_url:?}");
// Handle the track url
match play_track(ctx, manager, guild_id.to_owned(), track_url).await {
Ok(count) => {
@@ -71,25 +53,17 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
} else if count == 1 {
message = "Playing 1 track".to_string();
}
if let Err(why) = edit_response(&ctx, &command, message).await {
log::error!("Failed to edit response message: {}", why);
}
edit_response(&ctx, &command, message).await;
}
Err(err) => {
log::warn!("Failed to play track: {}", err);
if let Err(why) =
edit_response(&ctx, &command, format!("Failed to play track: {}", err)).await
{
log::error!("Failed to edit response message: {}", why);
}
edit_response(&ctx, &command, format!("Failed to play track: {}", err)).await;
}
};
}
Err(err) => {
log::warn!("{}", err);
if let Err(why) = edit_response(&ctx, &command, format!("{}", err)).await {
log::error!("Failed to edit response message: {}", why);
}
edit_response(&ctx, &command, format!("{}", err)).await;
}
}
}
@@ -140,17 +114,17 @@ pub async fn play_track(
handler_lock.clone(),
&item.url,
is_queue_empty,
Some(guild.volume as f32 / 100.0),
guild.volume as f32 / 100.0,
)
.await
{
Ok(added_song) => {
let track_title = added_song.title.unwrap();
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.remove_all_global_events();
handler.add_global_event(
songbird::Event::Track(songbird::TrackEvent::End),
Event::Track(TrackEvent::End),
TrackEndNotifier {
guild_id,
call: manager.clone(),
@@ -160,7 +134,7 @@ pub async fn play_track(
}
Err(err) => {
log::warn!("Failed to add song: {}", err);
if let Err(why) = leave_voice_channel(manager, &Some(guild_id)).await {
if let Err(why) = leave_voice_channel(&manager, &guild_id).await {
log::error!("Failed to leave voice channel: {}", why);
}
return Err(SirenError::new(422, err.to_string()));
@@ -176,9 +150,8 @@ async fn add_song(
call: Arc<Mutex<Call>>,
url: &str,
lazy: bool,
volume: Option<f32>,
volume: f32,
) -> SirenResult<AuxMetadata> {
// let source = Restartable::ytdl(url.to_owned(), lazy).await?;
let http_client = {
let data = ctx.data.read().await;
data.get::<HttpKey>()
@@ -187,17 +160,16 @@ async fn add_song(
};
let source = YoutubeDl::new(http_client, url.to_owned());
let mut handler = call.lock().await;
let mut track: Input = source.into();
let metadata = track.aux_metadata().await.unwrap();
let mut input: Input = source.into();
let metadata = input.aux_metadata().await.unwrap();
let track_handle: TrackHandle;
if lazy {
track_handle = handler.play_input(track);
track_handle = handler.play_input(input);
} else {
track_handle = handler.enqueue_input(track).await;
}
if let Some(volume) = volume {
let _ = track_handle.set_volume(volume);
track_handle = handler.enqueue_input(input).await;
}
// Set the volume
let _ = track_handle.set_volume(volume);
Ok(metadata)
}
@@ -236,8 +208,8 @@ pub fn register() -> CreateCommand {
}
struct TrackEndNotifier {
pub call: std::sync::Arc<songbird::Songbird>,
pub guild_id: serenity::model::id::GuildId,
pub call: Arc<Songbird>,
pub guild_id: GuildId,
}
#[async_trait]