summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 2d4bd0cff31b7fb6c3a31739131f633ec31a5f5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
mod client;
mod commands;
mod consts;
mod events;
mod http;
mod storage;
mod utils;

use anyhow::Result;

use tokio::signal::ctrl_c;
#[cfg(target_family = "unix")]
use tokio::signal::unix::{signal, SignalKind};
#[cfg(target_family = "windows")]
use tokio::signal::windows::ctrl_close;

#[tokio::main]
async fn main() -> Result<()> {
	dotenvy::dotenv().ok();
	env_logger::init();

	let mut client = client::get().await?;

	let shard_manager = client.shard_manager.clone(); // We need this to shut down the bot
	#[cfg(target_family = "unix")]
	let mut sigterm = signal(SignalKind::terminate())?;
	#[cfg(target_family = "windows")]
	let mut sigterm = ctrl_close()?;

	tokio::select! {
		result = client.start() => result.map_err(anyhow::Error::from),
		_ = sigterm.recv() => {
			client::handle_shutdown(shard_manager, "Received SIGTERM").await;
			println!("Everything is shutdown. Goodbye!");
			std::process::exit(0);
		},
		_ = ctrl_c() => {
			client::handle_shutdown(shard_manager, "Interrupted").await;
			println!("Everything is shutdown. Goodbye!");
			std::process::exit(130);
		}
	}
}