use std::{str::FromStr, sync::Arc}; use crate::{config::Config, handler::Handler}; use anyhow::Context; use dashmap::DashMap; use figment::{ Figment, providers::{Env, Format, Toml}, }; use secrecy::ExposeSecret; use serenity::{ Client, all::{GatewayIntents, Token}, cache, }; use sqlx::{migrate, postgres::PgPoolOptions}; use tracing::Level; mod commands; mod config; mod db; mod handler; mod modal; #[macro_use] extern crate tracing; #[tokio::main] async fn main() -> anyhow::Result<()> { let mut fmt = tracing_subscriber::fmt(); let figment = Figment::new() .merge(Toml::file("config.toml")) .merge(Env::prefixed("PB_").split("__")); let config: Config = figment.extract()?; if config.debug.unwrap_or(false) { fmt = fmt.with_max_level(Level::DEBUG); } fmt.init(); info!("config: {config:?}"); let db = PgPoolOptions::new() .max_connections(5) .connect(config.db.url.expose_secret()) .await .context("unable to connect to database")?; let migrator = migrate!("./migrations"); migrator.run(&db).await.context("failed to migrate")?; info!("migrated database"); let mut cache_settings = cache::Settings::default(); cache_settings.max_messages = 100; let mut client = Client::builder( Token::from_str(config.bot.token.expose_secret()).unwrap(), GatewayIntents::GUILD_MESSAGES | GatewayIntents::GUILD_MESSAGE_REACTIONS | GatewayIntents::GUILDS | GatewayIntents::MESSAGE_CONTENT, ) .event_handler(Arc::new(Handler { db, message_lock: Arc::new(DashMap::new()), })) .cache_settings(cache_settings) .await .expect("Err creating client"); client.start().await?; Ok(()) }