96 lines
3.0 KiB
Rust
96 lines
3.0 KiB
Rust
use std::sync::Arc;
|
|
use axum::extract::{Path, State};
|
|
use axum::middleware::from_extractor;
|
|
use axum::{Extension, Json, Router};
|
|
use axum::response::IntoResponse;
|
|
use axum::routing::post;
|
|
use reqwest::StatusCode;
|
|
use serde::Deserialize;
|
|
use crate::api::auth::{AuthCredential, AuthorizationMiddleware, Session};
|
|
use crate::AppState;
|
|
use crate::bot::commands::audio::join_voice_channel;
|
|
use crate::bot::commands::audio::pause::pause_track;
|
|
use crate::bot::commands::audio::play::enqueue_track;
|
|
use crate::bot::commands::audio::resume::resume_track;
|
|
use crate::bot::handler::get_songbird;
|
|
use crate::error::{Error, SirenResult};
|
|
|
|
pub fn get_routes() -> Router<Arc<AppState>> {
|
|
Router::new()
|
|
.route("/play", post(play_audio))
|
|
.route_layer(from_extractor::<AuthorizationMiddleware>())
|
|
.route("/pause", post(pause_audio))
|
|
.route_layer(from_extractor::<AuthorizationMiddleware>())
|
|
.route("/resume", post(resume_audio))
|
|
.route_layer(from_extractor::<AuthorizationMiddleware>())
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct PlayTrackRequest {
|
|
url: String,
|
|
}
|
|
|
|
async fn play_audio(
|
|
Extension(credential): Extension<AuthCredential>,
|
|
State(state): State<Arc<AppState>>,
|
|
Path(guild_id): Path<u64>,
|
|
Json(payload): Json<PlayTrackRequest>,
|
|
) -> SirenResult<()> {
|
|
log::debug!("Playing audio in guild: {}", guild_id);
|
|
|
|
// Check if the user exists in the cache
|
|
let user_id = credential.user_id();
|
|
let user_id = match state.cache.user(user_id) {
|
|
Some(user) => user.id,
|
|
None => return Err(Error::not_found("User not found".to_string())),
|
|
};
|
|
|
|
// Validate if the guild exists in the cache
|
|
let guild_id = match state.cache.guild(guild_id) {
|
|
Some(guild) => guild.id,
|
|
None => return Err(Error::not_found("Guild not found".to_string())),
|
|
};
|
|
|
|
// Play the track
|
|
let manager = get_songbird();
|
|
let _channel_id = join_voice_channel(&state.cache, &manager, &guild_id, &user_id).await?;
|
|
enqueue_track(manager, guild_id.to_owned(), &payload.url).await?;
|
|
Ok(())
|
|
}
|
|
|
|
async fn pause_audio(
|
|
Extension(_): Extension<AuthCredential>,
|
|
State(state): State<Arc<AppState>>,
|
|
Path(guild_id): Path<u64>,
|
|
) -> SirenResult<()> {
|
|
log::debug!("Pausing audio in guild: {}", guild_id);
|
|
|
|
// Validate if the guild exists in the cache
|
|
let guild_id = match state.cache.guild(guild_id) {
|
|
Some(guild) => guild.id,
|
|
None => return Err(Error::not_found("Guild not found".to_string())),
|
|
};
|
|
|
|
// Pause the track
|
|
let manager = get_songbird();
|
|
pause_track(manager, &guild_id).await
|
|
}
|
|
|
|
async fn resume_audio(
|
|
Extension(_): Extension<AuthCredential>,
|
|
State(state): State<Arc<AppState>>,
|
|
Path(guild_id): Path<u64>,
|
|
) -> SirenResult<()> {
|
|
log::debug!("Pausing audio in guild: {}", guild_id);
|
|
|
|
// Validate if the guild exists in the cache
|
|
let guild_id = match state.cache.guild(guild_id) {
|
|
Some(guild) => guild.id,
|
|
None => return Err(Error::not_found("Guild not found".to_string())),
|
|
};
|
|
|
|
// Pause the track
|
|
let manager = get_songbird();
|
|
resume_track(manager, &guild_id).await
|
|
}
|