paringboard/src/bot/main.rs
paring c0296f698a
All checks were successful
Build / Build (push) Successful in 2m1s
Build / docker (push) Successful in 20s
feat: handle reaction remove, fix deadlock
2026-01-07 15:42:47 +09:00

74 lines
1.7 KiB
Rust

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},
};
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 client = Client::builder(
Token::from_str(config.bot.token.expose_secret()).unwrap(),
GatewayIntents::GUILD_MESSAGES
| GatewayIntents::GUILD_MESSAGE_REACTIONS
| GatewayIntents::GUILDS
| GatewayIntents::GUILD_MESSAGES
| GatewayIntents::MESSAGE_CONTENT,
)
.event_handler(Arc::new(Handler {
db,
message_lock: Arc::new(DashMap::new()),
}))
.await
.expect("Err creating client");
client.start().await?;
Ok(())
}