2020-09-28 21:35:05 +00:00
|
|
|
//Needed for nested Result handling from tokio. Probably can go away after 1.47.0.
|
|
|
|
#![type_length_limit = "7605144"]
|
2021-06-02 21:09:58 +00:00
|
|
|
use futures::try_join;
|
2020-09-28 21:35:05 +00:00
|
|
|
use log::error;
|
2021-06-02 21:09:58 +00:00
|
|
|
use matrix_sdk::Client;
|
2021-05-17 23:12:27 +00:00
|
|
|
use std::env;
|
2020-10-17 13:30:07 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2021-05-14 22:25:09 +00:00
|
|
|
use tenebrous_dicebot::bot::DiceBot;
|
|
|
|
use tenebrous_dicebot::config::*;
|
2021-05-15 23:45:26 +00:00
|
|
|
use tenebrous_dicebot::db::sqlite::Database;
|
2021-05-14 22:25:09 +00:00
|
|
|
use tenebrous_dicebot::error::BotError;
|
2021-06-02 21:09:58 +00:00
|
|
|
use tenebrous_dicebot::rpc;
|
2021-05-14 22:25:09 +00:00
|
|
|
use tenebrous_dicebot::state::DiceBotState;
|
2021-05-17 23:12:27 +00:00
|
|
|
use tracing_subscriber::filter::EnvFilter;
|
2020-08-26 21:09:50 +00:00
|
|
|
|
2021-06-02 21:09:58 +00:00
|
|
|
/// Attempt to create config object and ddatabase connection pool from
|
|
|
|
/// the given config path. An error is returned if config creation or
|
|
|
|
/// database pool creation fails for some reason.
|
|
|
|
async fn init(config_path: &str) -> Result<(Arc<Config>, Database, Client), BotError> {
|
|
|
|
let cfg = read_config(config_path)?;
|
|
|
|
let cfg = Arc::new(cfg);
|
|
|
|
let sqlite_path = format!("{}/dicebot.sqlite", cfg.database_path());
|
|
|
|
let db = Database::new(&sqlite_path).await?;
|
2023-04-13 19:04:48 +00:00
|
|
|
let client = tenebrous_dicebot::matrix::create_client(&cfg).await?;
|
2021-06-02 21:09:58 +00:00
|
|
|
Ok((cfg, db, client))
|
|
|
|
}
|
|
|
|
|
2020-10-17 13:30:07 +00:00
|
|
|
#[tokio::main]
|
2021-06-02 21:09:58 +00:00
|
|
|
async fn main() -> Result<(), BotError> {
|
2021-05-17 23:12:27 +00:00
|
|
|
let filter = if env::var("RUST_LOG").is_ok() {
|
|
|
|
EnvFilter::from_default_env()
|
|
|
|
} else {
|
2021-06-02 21:09:58 +00:00
|
|
|
EnvFilter::new("tonic=info,tenebrous_dicebot=info,dicebot=info,refinery=info")
|
2021-05-17 23:12:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tracing_subscriber::fmt().with_env_filter(filter).init();
|
|
|
|
|
2020-09-28 21:35:05 +00:00
|
|
|
match run().await {
|
|
|
|
Ok(_) => (),
|
2020-10-15 16:52:08 +00:00
|
|
|
Err(e) => error!("Error: {}", e),
|
2021-06-02 21:09:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-09-28 21:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn run() -> Result<(), BotError> {
|
2020-04-17 22:53:27 +00:00
|
|
|
let config_path = std::env::args()
|
2020-04-17 05:25:13 +00:00
|
|
|
.skip(1)
|
|
|
|
.next()
|
2020-04-17 22:53:27 +00:00
|
|
|
.expect("Need a config as an argument");
|
2020-04-17 05:20:54 +00:00
|
|
|
|
2021-06-02 21:09:58 +00:00
|
|
|
let (cfg, db, client) = init(&config_path).await?;
|
|
|
|
let grpc = rpc::serve_grpc(&cfg, &db, &client);
|
|
|
|
let bot = run_bot(&cfg, &db, &client);
|
|
|
|
|
|
|
|
match try_join!(bot, grpc) {
|
|
|
|
Ok(_) => (),
|
2023-04-13 19:04:48 +00:00
|
|
|
Err(e) => error!("Error: {:?}", e),
|
2021-06-02 21:09:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_bot(cfg: &Arc<Config>, db: &Database, client: &Client) -> Result<(), BotError> {
|
2020-10-17 13:30:07 +00:00
|
|
|
let state = Arc::new(RwLock::new(DiceBotState::new(&cfg)));
|
2020-09-28 21:35:05 +00:00
|
|
|
|
2021-06-02 21:09:58 +00:00
|
|
|
match DiceBot::new(cfg, &state, db, client) {
|
2020-09-28 21:35:05 +00:00
|
|
|
Ok(bot) => bot.run().await?,
|
|
|
|
Err(e) => println!("Error connecting: {:?}", e),
|
|
|
|
};
|
2020-04-17 05:20:54 +00:00
|
|
|
|
2020-08-26 21:09:50 +00:00
|
|
|
Ok(())
|
2020-04-17 05:20:54 +00:00
|
|
|
}
|