2022-04-11 19:55:10 +00:00
|
|
|
use deadpool_postgres::Pool;
|
|
|
|
use futures::StreamExt;
|
|
|
|
use std::{sync::Arc, env};
|
|
|
|
use tracing::{error, info, Level};
|
|
|
|
|
|
|
|
use twilight_gateway::{
|
|
|
|
cluster::{Events, ShardScheme},
|
2022-04-13 12:48:06 +00:00
|
|
|
Cluster, EventTypeFlags, Intents,
|
2022-04-11 19:55:10 +00:00
|
|
|
};
|
|
|
|
use twilight_http::Client as HttpClient;
|
|
|
|
|
|
|
|
mod config;
|
2022-04-13 12:48:06 +00:00
|
|
|
mod evt;
|
2022-04-11 19:55:10 +00:00
|
|
|
mod db;
|
|
|
|
mod util;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
init_tracing();
|
|
|
|
info!("starting...");
|
|
|
|
|
|
|
|
let cfg = config::load_config();
|
|
|
|
|
|
|
|
let http = Arc::new(HttpClient::new(cfg.token.clone()));
|
|
|
|
let rconn = redis::Client::open(cfg.redis_addr.clone()).unwrap();
|
|
|
|
let (_cluster, events) = init_gateway(&cfg, rconn.clone()).await?;
|
2022-04-13 12:48:06 +00:00
|
|
|
// let cache = init_cache();
|
2022-04-11 19:55:10 +00:00
|
|
|
let db = db::init_db(&cfg).await?;
|
|
|
|
|
2022-04-13 12:48:06 +00:00
|
|
|
run(http, events, db, rconn).await?;
|
2022-04-11 19:55:10 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run(
|
|
|
|
http: Arc<HttpClient>,
|
|
|
|
mut events: Events,
|
|
|
|
db: Pool,
|
|
|
|
rconn: redis::Client,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
while let Some((shard_id, event)) = events.next().await {
|
|
|
|
|
2022-04-13 12:48:06 +00:00
|
|
|
// cache.update(&event);
|
2022-04-11 19:55:10 +00:00
|
|
|
|
|
|
|
let http_cloned = http.clone();
|
|
|
|
let db_cloned = db.clone();
|
|
|
|
let rconn_cloned = rconn.clone();
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
2022-04-13 12:48:06 +00:00
|
|
|
let result = evt::handle_event(
|
2022-04-11 19:55:10 +00:00
|
|
|
shard_id,
|
|
|
|
event,
|
|
|
|
http_cloned,
|
|
|
|
db_cloned,
|
|
|
|
rconn_cloned
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Err(e) = result {
|
|
|
|
error!("error in event handler: {:?}", e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_tracing() {
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_max_level(Level::INFO)
|
|
|
|
.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn init_gateway(
|
|
|
|
cfg: &config::BotConfig,
|
|
|
|
rconn: redis::Client,
|
|
|
|
) -> anyhow::Result<(Arc<Cluster>, Events)> {
|
|
|
|
let shard_count = cfg.shard_count.clone();
|
|
|
|
|
|
|
|
let scheme: ShardScheme;
|
|
|
|
|
|
|
|
if shard_count < 16 {
|
|
|
|
scheme = ShardScheme::Auto;
|
|
|
|
} else {
|
2022-05-10 01:54:52 +00:00
|
|
|
let cluster_id = env::var("NOMAD_ALLOC_INDEX").or::<String>(Result::Ok("0".to_string())).unwrap().parse::<u64>().unwrap();
|
2022-04-11 19:55:10 +00:00
|
|
|
let first_shard_id = 16 * cluster_id;
|
|
|
|
|
|
|
|
scheme = ShardScheme::try_from((first_shard_id..first_shard_id+16, shard_count)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let queue = util::RedisQueue {
|
|
|
|
client: rconn.clone(),
|
|
|
|
concurrency: cfg.max_concurrency.clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
let (cluster, events) = Cluster::builder(
|
|
|
|
cfg.token.clone(),
|
|
|
|
Intents::GUILDS
|
|
|
|
| Intents::DIRECT_MESSAGES
|
|
|
|
| Intents::DIRECT_MESSAGE_REACTIONS
|
|
|
|
| Intents::GUILD_EMOJIS_AND_STICKERS
|
|
|
|
| Intents::GUILD_MESSAGES
|
|
|
|
| Intents::GUILD_MESSAGE_REACTIONS
|
|
|
|
| Intents::GUILD_WEBHOOKS
|
|
|
|
| Intents::MESSAGE_CONTENT
|
|
|
|
)
|
|
|
|
.shard_scheme(scheme)
|
2022-05-12 02:33:08 +00:00
|
|
|
.event_types(EventTypeFlags::all())
|
2022-04-11 19:55:10 +00:00
|
|
|
.queue(Arc::new(queue))
|
|
|
|
.build()
|
|
|
|
.await?;
|
|
|
|
let cluster = Arc::new(cluster);
|
|
|
|
|
|
|
|
let cluster_spawn = Arc::clone(&cluster);
|
|
|
|
tokio::spawn(async move {
|
|
|
|
cluster_spawn.up().await;
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok((cluster, events))
|
|
|
|
}
|