Formatting code
This commit is contained in:
@@ -5,74 +5,104 @@ use serde::{Serialize, Deserialize};
|
||||
use serenity::model::prelude::{GuildChannel, ChannelType};
|
||||
use siren::{ServiceError, Response};
|
||||
|
||||
use crate::{AppState, bot::commands::audio::{play::play_track, join}, bot::guilds::QueryGuild, auth::{Auth, verify_role}};
|
||||
use crate::{
|
||||
AppState,
|
||||
bot::commands::audio::{play::play_track, join},
|
||||
bot::guilds::QueryGuild,
|
||||
auth::{Auth, verify_role},
|
||||
};
|
||||
|
||||
#[get("/guilds")]
|
||||
async fn get_guilds(data: web::Data<Arc<AppState>>, auth: Auth) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let guild_results = &data.http.get_guilds(None, None).await;
|
||||
let guilds = match guild_results {
|
||||
Ok(guilds) => guilds,
|
||||
Err(err) => return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
HttpResponse::Ok().json(Response {
|
||||
data: guilds,
|
||||
metadata: None
|
||||
metadata: None,
|
||||
})
|
||||
}
|
||||
|
||||
#[get("/{id}/text")]
|
||||
async fn get_text_channels(id: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Auth) -> HttpResponse {
|
||||
async fn get_text_channels(
|
||||
id: web::Path<String>,
|
||||
data: web::Data<Arc<AppState>>,
|
||||
auth: Auth,
|
||||
) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let channel_results = &data.http.get_channels(id.parse::<u64>().unwrap()).await;
|
||||
let channels = match channel_results {
|
||||
Ok(channels) => channels.iter().filter(|c| c.kind == ChannelType::Text).collect::<Vec<&GuildChannel>>(),
|
||||
Err(err) => return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
Ok(channels) => channels
|
||||
.iter()
|
||||
.filter(|c| c.kind == ChannelType::Text)
|
||||
.collect::<Vec<&GuildChannel>>(),
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
HttpResponse::Ok().json(Response {
|
||||
data: channels,
|
||||
metadata: None
|
||||
metadata: None,
|
||||
})
|
||||
}
|
||||
|
||||
#[get("/{id}/voice")]
|
||||
async fn get_voice_channels(id: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Auth) -> HttpResponse {
|
||||
async fn get_voice_channels(
|
||||
id: web::Path<String>,
|
||||
data: web::Data<Arc<AppState>>,
|
||||
auth: Auth,
|
||||
) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let channel_results = &data.http.get_channels(id.parse::<u64>().unwrap()).await;
|
||||
let channels = match channel_results {
|
||||
Ok(channels) => channels.iter().filter(|c| c.kind == ChannelType::Voice).collect::<Vec<&GuildChannel>>(),
|
||||
Err(err) => return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
Ok(channels) => channels
|
||||
.iter()
|
||||
.filter(|c| c.kind == ChannelType::Voice)
|
||||
.collect::<Vec<&GuildChannel>>(),
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
HttpResponse::Ok().json(Response {
|
||||
data: channels,
|
||||
metadata: None
|
||||
metadata: None,
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct ChannelMessage {
|
||||
message: String
|
||||
message: String,
|
||||
}
|
||||
|
||||
#[post("/{guild_id}/text/{channel_id}/message")]
|
||||
async fn send_message(path: web::Path<(String, String)>, text: web::Json<ChannelMessage>, data: web::Data<Arc<AppState>>, auth: Auth) -> HttpResponse {
|
||||
async fn send_message(
|
||||
path: web::Path<(String, String)>,
|
||||
text: web::Json<ChannelMessage>,
|
||||
data: web::Data<Arc<AppState>>,
|
||||
auth: Auth,
|
||||
) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let (guild_id, channel_id) = path.into_inner();
|
||||
let guild_id = match guild_id.parse::<u64>() {
|
||||
@@ -80,7 +110,7 @@ async fn send_message(path: web::Path<(String, String)>, text: web::Json<Channel
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
@@ -89,7 +119,7 @@ async fn send_message(path: web::Path<(String, String)>, text: web::Json<Channel
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
@@ -99,26 +129,29 @@ async fn send_message(path: web::Path<(String, String)>, text: web::Json<Channel
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
let channel = match channels.iter().find(|c| c.id.0 == channel_id) {
|
||||
Some(channel) => channel,
|
||||
None => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: format!("Could not find channel with id {}", channel_id)
|
||||
message: format!("Could not find channel with id {}", channel_id),
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(err) = channel.say(&Pin::new(&data.http).get_ref(), &text.message).await {
|
||||
if let Err(err) = channel
|
||||
.say(&Pin::new(&data.http).get_ref(), &text.message)
|
||||
.await
|
||||
{
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
message: err.to_string(),
|
||||
});
|
||||
};
|
||||
|
||||
HttpResponse::Ok().finish()
|
||||
@@ -126,38 +159,55 @@ async fn send_message(path: web::Path<(String, String)>, text: web::Json<Channel
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PlayRequest {
|
||||
track_url: String
|
||||
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>>, auth: Auth) -> HttpResponse {
|
||||
async fn play(
|
||||
path: web::Path<(String, String)>,
|
||||
play_request: web::Json<PlayRequest>,
|
||||
data: web::Data<Arc<AppState>>,
|
||||
auth: Auth,
|
||||
) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let (guild_id, channel_id) = path.into_inner();
|
||||
let guild_id = match guild_id.parse::<u64>() {
|
||||
Ok(id) => id,
|
||||
Err(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) => {
|
||||
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
|
||||
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) => {
|
||||
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
|
||||
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) => {
|
||||
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
@@ -165,15 +215,22 @@ async fn play(path: web::Path<(String, String)>, play_request: web::Json<PlayReq
|
||||
|
||||
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 {
|
||||
match play_track(
|
||||
Arc::clone(&data.songbird),
|
||||
guild.id,
|
||||
play_request.track_url.to_string(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => HttpResponse::Ok().finish(),
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&err)
|
||||
}
|
||||
Err(err) => return ResponseError::error_response(&err),
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError { status: 500, message: err.to_string() })
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 500,
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,7 +238,7 @@ async fn play(path: web::Path<(String, String)>, play_request: web::Json<PlayReq
|
||||
#[post("/{guild_id}/voice/stop")]
|
||||
async fn stop(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Auth) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let guild_id = path.into_inner();
|
||||
let guild_id = match guild_id.parse::<u64>() {
|
||||
@@ -189,7 +246,7 @@ async fn stop(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Aut
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
@@ -203,9 +260,13 @@ async fn stop(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Aut
|
||||
}
|
||||
|
||||
#[post("/{guild_id}/voice/resume")]
|
||||
async fn resume(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Auth) -> HttpResponse {
|
||||
async fn resume(
|
||||
path: web::Path<String>,
|
||||
data: web::Data<Arc<AppState>>,
|
||||
auth: Auth,
|
||||
) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let guild_id = path.into_inner();
|
||||
let guild_id = match guild_id.parse::<u64>() {
|
||||
@@ -213,7 +274,7 @@ async fn resume(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: A
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
@@ -223,8 +284,8 @@ async fn resume(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: A
|
||||
if let Err(err) = handler.queue().resume() {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
message: err.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,9 +293,13 @@ async fn resume(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: A
|
||||
}
|
||||
|
||||
#[post("/{guild_id}/voice/pause")]
|
||||
async fn pause(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Auth) -> HttpResponse {
|
||||
async fn pause(
|
||||
path: web::Path<String>,
|
||||
data: web::Data<Arc<AppState>>,
|
||||
auth: Auth,
|
||||
) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let guild_id = path.into_inner();
|
||||
let guild_id = match guild_id.parse::<u64>() {
|
||||
@@ -242,7 +307,7 @@ async fn pause(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Au
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
@@ -252,8 +317,8 @@ async fn pause(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Au
|
||||
if let Err(err) = handler.queue().pause() {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
message: err.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,13 +327,13 @@ async fn pause(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Au
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct SetVolume {
|
||||
volume: String
|
||||
volume: String,
|
||||
}
|
||||
|
||||
#[get("/{guild_id}/voice/volume")]
|
||||
async fn get_volume(path: web::Path<String>, auth: Auth) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let guild_id = path.into_inner();
|
||||
let guild_id = match guild_id.parse::<u64>() {
|
||||
@@ -276,7 +341,7 @@ async fn get_volume(path: web::Path<String>, auth: Auth) -> HttpResponse {
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
@@ -286,7 +351,7 @@ async fn get_volume(path: web::Path<String>, auth: Auth) -> HttpResponse {
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
@@ -295,9 +360,14 @@ async fn get_volume(path: web::Path<String>, auth: Auth) -> HttpResponse {
|
||||
}
|
||||
|
||||
#[post("/{guild_id}/voice/volume")]
|
||||
async fn set_volume(path: web::Path<String>, volume: web::Json::<SetVolume>, data: web::Data<Arc<AppState>>, auth: Auth) -> HttpResponse {
|
||||
async fn set_volume(
|
||||
path: web::Path<String>,
|
||||
volume: web::Json<SetVolume>,
|
||||
data: web::Data<Arc<AppState>>,
|
||||
auth: Auth,
|
||||
) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let guild_id = path.into_inner();
|
||||
let guild_id = match guild_id.parse::<u64>() {
|
||||
@@ -305,7 +375,7 @@ async fn set_volume(path: web::Path<String>, volume: web::Json::<SetVolume>, dat
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
@@ -316,7 +386,10 @@ async fn set_volume(path: web::Path<String>, volume: web::Json::<SetVolume>, dat
|
||||
let guild = match http.get_guild(guild_id).await {
|
||||
Ok(guild) => guild,
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
crate::bot::commands::audio::volume::set_volume(manager, guild.id, volume).await;
|
||||
@@ -327,7 +400,7 @@ async fn set_volume(path: web::Path<String>, volume: web::Json::<SetVolume>, dat
|
||||
#[post("/{guild_id}/voice/skip")]
|
||||
async fn skip(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Auth) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
return ResponseError::error_response(&err)
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
let guild_id = path.into_inner();
|
||||
let guild_id = match guild_id.parse::<u64>() {
|
||||
@@ -335,7 +408,7 @@ async fn skip(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Aut
|
||||
Err(err) => {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
message: err.to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
@@ -345,8 +418,8 @@ async fn skip(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Aut
|
||||
if let Err(err) = handler.queue().skip() {
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
message: err.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,9 +427,8 @@ async fn skip(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Aut
|
||||
}
|
||||
|
||||
pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
config
|
||||
.service(get_guilds)
|
||||
.service(web::scope("guilds")
|
||||
config.service(get_guilds).service(
|
||||
web::scope("guilds")
|
||||
.service(get_text_channels)
|
||||
.service(get_voice_channels)
|
||||
.service(send_message)
|
||||
@@ -366,6 +438,6 @@ pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
.service(pause)
|
||||
.service(set_volume)
|
||||
.service(get_volume)
|
||||
.service(skip)
|
||||
.service(skip),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user