Slight chat/play refactor, add roll command
This commit is contained in:
@@ -9,9 +9,10 @@ use serenity::model::application::interaction::application_command::ApplicationC
|
||||
use siren::ServiceError;
|
||||
use songbird::{EventHandler, Songbird};
|
||||
|
||||
use crate::bot::ytdlp::PlaylistItem;
|
||||
use crate::bot::{guilds::QueryGuild, commands::audio::{leave, get_playlist_urls, add_song, get_songbird}};
|
||||
|
||||
use super::{create_response, edit_response, join_by_user};
|
||||
use super::{create_response, edit_response, is_valid_url, join_by_user};
|
||||
|
||||
pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
// Get the track url
|
||||
@@ -65,8 +66,14 @@ pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
debug!("Play command executed with track: {:?}", track_url);
|
||||
let manager = get_songbird(ctx).await;
|
||||
match play_track(manager, guild_id, track_url).await {
|
||||
Ok(_) => {
|
||||
if let Err(why) = edit_response(&ctx, &command, "Playing track".to_string()).await {
|
||||
Ok(count) => {
|
||||
let mut message = format!("Playing {} tracks", count);
|
||||
if count == 0 {
|
||||
message = "No tracks were played".to_string();
|
||||
} else if count == 1 {
|
||||
message = "Playing 1 track".to_string();
|
||||
}
|
||||
if let Err(why) = edit_response(&ctx, &command, message).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
},
|
||||
@@ -95,15 +102,32 @@ pub async fn play_track(manager: Arc<Songbird>, guild_id: GuildId, track_url: St
|
||||
call_handler.queue().is_empty()
|
||||
};
|
||||
let guild = QueryGuild::get(guild_id.0 as i64)?;
|
||||
let track_urls = match get_playlist_urls(&track_url) {
|
||||
Ok(urls) => urls,
|
||||
Err(err) => {
|
||||
warn!("Failed to get playlist urls: {}", err);
|
||||
return Err(ServiceError { status: 422, message: err.to_string() })
|
||||
}
|
||||
};
|
||||
for url in track_urls {
|
||||
match add_song(handler_lock.clone(), &url, is_queue_empty, Some(guild.volume as f32 / 100.0)).await {
|
||||
let valid = is_valid_url(&track_url);
|
||||
if !valid.0 {
|
||||
warn!("Invalid track url: {}", track_url);
|
||||
return Err(ServiceError { status: 422, message: format!("Invalid track url: {}", track_url) })
|
||||
}
|
||||
let mut playlist_items: Vec<PlaylistItem> = Vec::new();
|
||||
if valid.1 {
|
||||
playlist_items = match get_playlist_urls(&track_url) {
|
||||
Ok(items) => items,
|
||||
Err(err) => {
|
||||
warn!("Failed to get playlist urls: {}", err);
|
||||
return Err(ServiceError { status: 422, message: err.to_string() })
|
||||
}
|
||||
};
|
||||
} else {
|
||||
let playlist_item = PlaylistItem {
|
||||
id: "".to_string(),
|
||||
url: track_url,
|
||||
title: "".to_string(),
|
||||
duration: 0,
|
||||
playlist_index: 0
|
||||
};
|
||||
playlist_items.push(playlist_item);
|
||||
}
|
||||
for item in playlist_items {
|
||||
match add_song(handler_lock.clone(), &item.url, is_queue_empty, Some(guild.volume as f32 / 100.0)).await {
|
||||
Ok(added_song) => {
|
||||
let track_title = added_song.title.unwrap();
|
||||
debug!("Added track: {}", track_title);
|
||||
|
||||
Reference in New Issue
Block a user