format and restructure, began working on schedule

This commit is contained in:
2024-09-05 17:10:56 -04:00
parent 0f1a97770a
commit 794d8cc34e
34 changed files with 561 additions and 212 deletions

View File

@@ -7,19 +7,25 @@ use songbird::input::{AuxMetadata, Input, YoutubeDl};
use songbird::tracks::TrackHandle;
use songbird::{Call, Event, EventHandler, Songbird, TrackEvent};
use crate::bot::guilds::GuildCache;
use crate::database::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,
leave_voice_channel,
};
pub async fn run(ctx: &Context, command: &CommandInteraction) {
// Process the command options
let track_url = match command.data.options.first() {
Some(o) => &o.value.as_str().unwrap(),
Some(o) => o.value.as_str().unwrap(),
None => {
log::warn!("{} attempted to play a track without a track option", command.user.id.get());
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;
}
@@ -35,7 +41,12 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
let guild_id = match &command.guild_id {
Some(guild_id) => guild_id,
None => {
edit_response(&ctx, &command, "Unable to find the current server ID".to_string()).await;
edit_response(
&ctx,
&command,
"Unable to find the current server ID".to_string(),
)
.await;
return;
}
};
@@ -85,7 +96,10 @@ pub async fn play_track(
// Check if the URL is valid
if !valid.0 {
log::warn!("Invalid track url: {}", track_url);
return Err(SirenError::new(422, format!("Invalid track url: {}", track_url)));
return Err(SirenError::new(
422,
format!("Invalid track url: {}", track_url),
));
}
let mut playlist_items: Vec<PlaylistItem> = Vec::new();
// Check if the URL is a playlist or a single track
@@ -94,7 +108,7 @@ pub async fn play_track(
Ok(items) => items,
Err(err) => {
log::warn!("Failed to get playlist urls: {}", err);
return Err(SirenError::new(422,err.to_string()));
return Err(SirenError::new(422, err.to_string()));
}
};
} else {
@@ -154,10 +168,11 @@ async fn add_song(
) -> SirenResult<AuxMetadata> {
let http_client = {
let data = ctx.data.read().await;
data.get::<HttpKey>()
.cloned()
.expect("Guaranteed to exist in the typemap.")
};
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();
@@ -186,7 +201,8 @@ pub fn get_playlist_urls(url: &str) -> SirenResult<Vec<PlaylistItem>> {
None
} else {
Some(
serde_json::from_slice::<PlaylistItem>(line.as_bytes()).map_err(|err| SirenError::new(500, err.to_string())),
serde_json::from_slice::<PlaylistItem>(line.as_bytes())
.map_err(|err| SirenError::new(500, err.to_string())),
)
}
})
@@ -204,7 +220,10 @@ pub fn get_playlist_urls(url: &str) -> SirenResult<Vec<PlaylistItem>> {
pub fn register() -> CreateCommand {
CreateCommand::new("play")
.description("Plays the given track")
.add_option(CreateCommandOption::new(CommandOptionType::String, "track", "The track to be played").required(true))
.add_option(
CreateCommandOption::new(CommandOptionType::String, "track", "The track to be played")
.required(true),
)
}
struct TrackEndNotifier {