Reformat, working on roll
This commit is contained in:
120
src/main.rs
120
src/main.rs
@@ -6,14 +6,17 @@ use serenity::http::Http;
|
||||
use serenity::prelude::*;
|
||||
use songbird::{SerenityInit, Songbird};
|
||||
use reqwest::Client as HttpClient;
|
||||
use serenity::all::{ShardManager, UserId};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
use crate::bot::handler::Handler;
|
||||
use crate::bot::oai::OAI;
|
||||
|
||||
mod api;
|
||||
mod bot;
|
||||
mod data;
|
||||
mod error;
|
||||
mod utils;
|
||||
|
||||
pub struct HttpKey;
|
||||
|
||||
@@ -25,15 +28,14 @@ impl TypeMapKey for HttpKey {
|
||||
async fn main() {
|
||||
dotenv::dotenv().ok();
|
||||
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,siren=info"));
|
||||
|
||||
if let Err(err) = data::initialize().await {
|
||||
log::error!("Failed to initialize database: {err}");
|
||||
return;
|
||||
};
|
||||
|
||||
// Start API server
|
||||
tokio::spawn(async move {
|
||||
start_api().await;
|
||||
});
|
||||
tokio::spawn(start_api());
|
||||
|
||||
// Start Discord bot
|
||||
start_bot().await;
|
||||
@@ -41,8 +43,10 @@ async fn main() {
|
||||
|
||||
async fn start_api() {
|
||||
let app = Router::new();
|
||||
let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
|
||||
log::debug!("listening on {}", listener.local_addr().unwrap());
|
||||
let addr: String = "127.0.0.1:3000".parse().unwrap();
|
||||
|
||||
let listener = TcpListener::bind(&addr).await.unwrap();
|
||||
log::debug!("API is listening on {}", &addr);
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -51,45 +55,21 @@ async fn start_bot() {
|
||||
let intents: GatewayIntents = GatewayIntents::all();
|
||||
|
||||
let http: Http = Http::new(&token);
|
||||
let (_owners, _bot_id) = match http.get_current_application_info().await {
|
||||
Ok(info) => {
|
||||
let mut owners: HashSet<serenity::model::id::UserId> = HashSet::new();
|
||||
if let Some(team) = info.team {
|
||||
owners.insert(team.owner_user_id);
|
||||
} else {
|
||||
owners.insert(info.owner.unwrap().id);
|
||||
}
|
||||
match http.get_current_user().await {
|
||||
Ok(bot) => (owners, bot.id),
|
||||
Err(why) => panic!("Could not access the bot id: {why:?}"),
|
||||
}
|
||||
}
|
||||
Err(why) => panic!("Could not access application info: {why:?}"),
|
||||
};
|
||||
let (owners, bot_id) = get_bot_info(&http).await;
|
||||
|
||||
let handler = match env::var("OPENAI_API_KEY") {
|
||||
Ok(token) => {
|
||||
log::info!("OpenAI functionality enabled");
|
||||
let default_model = env::var("OPENAI_API_MODEL").unwrap_or("gpt-4o-mini".to_string());
|
||||
Handler {
|
||||
oai: Some(bot::oai::OAI {
|
||||
client: reqwest::Client::new(),
|
||||
base_url: "https://api.openai.com/v1".to_string(),
|
||||
// max_attempts: 5,
|
||||
token,
|
||||
max_conversation_history: 30,
|
||||
max_tokens: 8192,
|
||||
default_model,
|
||||
}),
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
log::trace!("No OPENAI_API_KEY found: {err}");
|
||||
log::warn!("OpenAI functionality disabled");
|
||||
Handler { oai: None }
|
||||
}
|
||||
};
|
||||
log::debug!(
|
||||
"Starting Discord bot with ID: {bot_id} and owners: {}",
|
||||
owners
|
||||
.iter()
|
||||
.map(|id| id.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
);
|
||||
|
||||
// Set up handler with optional OpenAI integration
|
||||
let handler = configure_handler();
|
||||
|
||||
// Set up Songbird for voice functionality
|
||||
let songbird = Songbird::serenity();
|
||||
|
||||
let mut client = Client::builder(token, intents)
|
||||
@@ -100,14 +80,64 @@ async fn start_bot() {
|
||||
.await
|
||||
.expect("Error creating client");
|
||||
|
||||
// Handle shutdown signals
|
||||
// Spawn shutdown signal handling
|
||||
let shard_manager = Arc::clone(&client.shard_manager);
|
||||
tokio::spawn(async move {
|
||||
shard_manager.shutdown_all().await;
|
||||
signal_shutdown(shard_manager).await;
|
||||
});
|
||||
|
||||
// Start Discord bot
|
||||
// Start the bot
|
||||
if let Err(why) = client.start_autosharded().await {
|
||||
log::error!("Client error: {why:?}");
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_bot_info(http: &Http) -> (HashSet<UserId>, UserId) {
|
||||
match http.get_current_application_info().await {
|
||||
Ok(info) => {
|
||||
let mut owners = HashSet::new();
|
||||
if let Some(team) = info.team {
|
||||
owners.insert(team.owner_user_id);
|
||||
} else if let Some(owner) = info.owner {
|
||||
owners.insert(owner.id);
|
||||
}
|
||||
match http.get_current_user().await {
|
||||
Ok(bot) => (owners, bot.id),
|
||||
Err(why) => panic!("Could not access the bot id: {why:?}"),
|
||||
}
|
||||
}
|
||||
Err(why) => panic!("Could not access application info: {why:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn configure_handler() -> Handler {
|
||||
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();
|
||||
Handler {
|
||||
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");
|
||||
Handler { oai: None }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn signal_shutdown(shard_manager: Arc<ShardManager>) {
|
||||
tokio::signal::ctrl_c()
|
||||
.await
|
||||
.expect("Failed to listen for shutdown signal");
|
||||
shard_manager.shutdown_all().await;
|
||||
log::info!("Bot shutdown gracefully.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user