Bot messages work
This commit is contained in:
118
service/src/bot/api/routes.rs
Normal file
118
service/src/bot/api/routes.rs
Normal file
@@ -0,0 +1,118 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use actix_web::{get, post, put, delete, web, HttpResponse, HttpRequest, ResponseError};
|
||||
use log::warn;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serenity::{http::Http, model::prelude::{GuildChannel, ChannelType}};
|
||||
use siren::ServiceError;
|
||||
|
||||
#[get("/guilds")]
|
||||
async fn get_guilds(data: web::Data<Arc<Http>>) -> HttpResponse {
|
||||
let guild_results = &data.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()
|
||||
})
|
||||
};
|
||||
HttpResponse::Ok().json(guilds)
|
||||
}
|
||||
|
||||
#[get("/{id}/text")]
|
||||
async fn get_text_channels(id: web::Path<String>, data: web::Data<Arc<Http>>) -> HttpResponse {
|
||||
let channel_results = &data.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()
|
||||
})
|
||||
};
|
||||
HttpResponse::Ok().json(channels)
|
||||
}
|
||||
|
||||
#[get("/{id}/voice")]
|
||||
async fn get_voice_channels(id: web::Path<String>, data: web::Data<Arc<Http>>) -> HttpResponse {
|
||||
let channel_results = &data.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()
|
||||
})
|
||||
};
|
||||
HttpResponse::Ok().json(channels)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct ChannelMessage {
|
||||
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<Http>>) -> 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()
|
||||
})
|
||||
}
|
||||
};
|
||||
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 channel_results = &data.get_channels(guild_id).await;
|
||||
let channels = match channel_results {
|
||||
Ok(channels) => channels,
|
||||
Err(err) => {
|
||||
warn!("Could not get channels: {:?}", err);
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
let channel = match channels.iter().find(|c| c.id.0 == channel_id) {
|
||||
Some(channel) => channel,
|
||||
None => {
|
||||
warn!("Could not find channel with id {}", channel_id);
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: format!("Could not find channel with id {}", channel_id)
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(err) = channel.say(&data.get_ref(), &text.message).await {
|
||||
warn!("Could not send message: {:?}", err);
|
||||
return ResponseError::error_response(&ServiceError {
|
||||
status: 422,
|
||||
message: err.to_string()
|
||||
})
|
||||
};
|
||||
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
config
|
||||
.service(get_guilds)
|
||||
.service(web::scope("guilds")
|
||||
.service(get_text_channels)
|
||||
.service(get_voice_channels)
|
||||
.service(send_message)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user