Added bot api

+ Right now can just play voices
+ Added commands to add api keys
+ clippy + fmt
This commit is contained in:
2023-01-21 15:36:14 -07:00
parent 6896ee610c
commit 41213db8d4
10 changed files with 396 additions and 39 deletions
+57
View File
@@ -0,0 +1,57 @@
use crate::config::GlobalData;
use crate::discord::voices::speak;
use crate::models::api_key::Apikey;
use axum::extract::State;
use axum::{http::StatusCode, response::IntoResponse, routing::post, Json, Router};
use serde::Deserialize;
use serenity::prelude::Context;
pub async fn web_server(ctx: Context) {
let addr = {
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
global_data.cfg.api_addr
};
let app = Router::new()
.route("/play", post(play_sound))
.with_state(ctx);
println!("Serving bot api on: {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
#[derive(Deserialize)]
struct SoundPayload {
pub api_key: String,
pub voice: String,
pub phrase: String,
}
async fn play_sound(
State(ctx): State<Context>,
Json(payload): Json<SoundPayload>,
) -> impl IntoResponse {
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
if let Some(api_key) = Apikey::find_key_from_secret(&global_data.db, &payload.api_key).unwrap()
{
if let Some(user_id) = api_key.user_id {
speak(
&ctx,
global_data.cfg.guild_id,
user_id,
&payload.voice,
&payload.phrase,
)
.await
.unwrap();
}
}
StatusCode::ACCEPTED
}