Cleanup, working on getting play to work
This commit is contained in:
@@ -6,7 +6,7 @@ use serde::{Serialize, Deserialize};
|
||||
use serenity::model::prelude::{GuildChannel, ChannelType};
|
||||
use siren::ServiceError;
|
||||
|
||||
use crate::AppState;
|
||||
use crate::{AppState, bot::commands::audio::{play::play_track, join}};
|
||||
|
||||
#[get("/guilds")]
|
||||
async fn get_guilds(data: web::Data<Arc<AppState>>) -> HttpResponse {
|
||||
@@ -109,32 +109,61 @@ async fn send_message(path: web::Path<(String, String)>, text: web::Json<Channel
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
#[post("/{guild_id}/voice/play")]
|
||||
async fn play(path: web::Path<String>, data: web::Data<Arc<AppState>>) -> HttpResponse {
|
||||
let guild_id = path.into_inner();
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PlayRequest {
|
||||
track_url: String
|
||||
}
|
||||
|
||||
#[post("/{guild_id}/voice/{channel_id}/play")]
|
||||
async fn play(path: web::Path<(String, String)>, play_request: web::Json<PlayRequest>, data: web::Data<Arc<AppState>>) -> HttpResponse {
|
||||
let (guild_id, channel_id) = path.into_inner();
|
||||
let guild_id = match guild_id.parse::<u64>() {
|
||||
Ok(id) => id,
|
||||
Err(err) => {
|
||||
warn!("Could not parse guild id: {:?}", err);
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
|
||||
}
|
||||
};
|
||||
let channel_id = match channel_id.parse::<u64>() {
|
||||
Ok(id) => id,
|
||||
Err(err) => {
|
||||
warn!("Could not parse channel id: {:?}", err);
|
||||
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
|
||||
}
|
||||
};
|
||||
let http = Pin::new(&data.http).get_ref();
|
||||
let guild = match http.get_guild(guild_id).await {
|
||||
Ok(guild) => guild,
|
||||
Err(err) => {
|
||||
warn!("Could not get guild: {:?}", err);
|
||||
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
|
||||
}
|
||||
};
|
||||
let channel = match http.get_channel(channel_id).await {
|
||||
Ok(channel) => channel,
|
||||
Err(err) => {
|
||||
warn!("Could not get channel: {:?}", err);
|
||||
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(handler_lock) = data.songbird.get(guild_id) {
|
||||
let handler = handler_lock.lock().await;
|
||||
if let Err(err) = handler.queue().pause() {
|
||||
warn!("Could not pause track: {:?}", err);
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
let manager = Arc::clone(&data.songbird);
|
||||
|
||||
match join(Arc::clone(&manager), &guild.id, &channel.id()).await {
|
||||
Ok(_) => {
|
||||
match play_track(Arc::clone(&data.songbird), guild.id, play_request.track_url.to_string()).await {
|
||||
Ok(_) => HttpResponse::Ok().finish(),
|
||||
Err(err) => {
|
||||
warn!("Could not play track: {:?}", err);
|
||||
return ResponseError::error_response(&err)
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("Could not join channel: {:?}", err);
|
||||
return ResponseError::error_response(&ServiceError { status: 500, message: err.to_string() })
|
||||
}
|
||||
}
|
||||
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
#[post("/{guild_id}/voice/pause")]
|
||||
@@ -173,5 +202,6 @@ pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
.service(get_voice_channels)
|
||||
.service(send_message)
|
||||
.service(pause)
|
||||
.service(play)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user