Play api request
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
use std::env;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
use serenity::all::{Interaction, ResumedEvent};
|
||||
use serenity::async_trait;
|
||||
use serenity::model::gateway::Ready;
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::prelude::*;
|
||||
use songbird::Songbird;
|
||||
use crate::bot::commands::chat::generate_response;
|
||||
use crate::bot::oai::OAI;
|
||||
use crate::data::guilds::GuildCache;
|
||||
use crate::HttpKey;
|
||||
use super::{commands};
|
||||
use super::chat::{create_modal_response};
|
||||
|
||||
@@ -14,6 +18,43 @@ pub struct BotHandler {
|
||||
pub oai: Option<OAI>,
|
||||
}
|
||||
|
||||
static SONGBIRD: OnceLock<Arc<Songbird>> = OnceLock::new();
|
||||
static CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
|
||||
|
||||
pub fn get_songbird() -> &'static Arc<Songbird> {
|
||||
SONGBIRD.get().unwrap()
|
||||
}
|
||||
|
||||
pub fn get_client() -> &'static reqwest::Client {
|
||||
CLIENT.get().unwrap()
|
||||
}
|
||||
|
||||
impl BotHandler {
|
||||
pub fn new() -> Self {
|
||||
match env::var("OPENAI_TOKEN") {
|
||||
Ok(token) => {
|
||||
log::debug!("OpenAI functionality enabled");
|
||||
let default_model = env::var("OPENAI_MODEL").unwrap_or_else(|_| "gpt-4o-mini".to_string());
|
||||
let base_url = env::var("OPENAI_BASE_URL").unwrap();
|
||||
Self {
|
||||
oai: Some(OAI {
|
||||
client: reqwest::Client::new(),
|
||||
base_url,
|
||||
token,
|
||||
max_conversation_history: 30,
|
||||
max_tokens: 8192,
|
||||
default_model,
|
||||
}),
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
log::warn!("OpenAI functionality disabled");
|
||||
Self { oai: None }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl EventHandler for BotHandler {
|
||||
async fn message(&self, ctx: Context, msg: Message) {
|
||||
@@ -40,6 +81,20 @@ impl EventHandler for BotHandler {
|
||||
if ready.guilds.is_empty() {
|
||||
log::warn!("No ready guilds found");
|
||||
}
|
||||
|
||||
let songbird = songbird::get(&ctx).await.unwrap();
|
||||
SONGBIRD
|
||||
.set(songbird.clone())
|
||||
.expect("Songbird value could not be set");
|
||||
let http_client = {
|
||||
let data = ctx.data.read().await;
|
||||
data
|
||||
.get::<HttpKey>()
|
||||
.cloned()
|
||||
.expect("Guaranteed to exist in the typemap.")
|
||||
};
|
||||
CLIENT.set(http_client).ok();
|
||||
|
||||
log::trace!("Handling {} guilds", ready.guilds.len());
|
||||
for guild in ready.guilds {
|
||||
// Check if guild exists in database
|
||||
|
||||
Reference in New Issue
Block a user