This repository has been archived by the owner on Oct 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
107 lines (81 loc) · 2.86 KB
/
main.rs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use handlers::setup::handle;
use poise::{serenity_prelude as serenity, Framework, FrameworkOptions};
use tokio::time::Instant;
use tracing::{debug, error, subscriber, Level};
use tracing_subscriber::{fmt::Subscriber, EnvFilter};
use ::serenity::all::GatewayIntents;
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
pub struct Data;
pub mod commands;
pub mod handlers;
pub mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
#[tokio::main]
pub async fn main() {
initialise_subscriber(Level::INFO);
let (framework, intents) = (initialise_framework().await, initialise_intents());
let mut client = initialise_client(intents, framework).await;
if let Err(reason) = client.start().await {
error!("Couldn't start client: {reason:?}");
}
}
fn initialise_intents() -> GatewayIntents {
let start_time = Instant::now();
let intents = GatewayIntents::all();
let elapsed_time = start_time.elapsed();
debug!("Initialised intents in {elapsed_time:.2?}");
intents
}
async fn initialise_client(
intents: GatewayIntents,
framework: Framework<Data, Error>,
) -> serenity::Client {
let start_time = Instant::now();
let token = std::env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN be set.");
let client = match serenity::Client::builder(token, intents)
.framework(framework)
.await
{
Ok(client) => client,
Err(reason) => {
error!("Couldn't initialise client: {reason:?}");
panic!("{reason:?}");
}
};
let elapsed_time = start_time.elapsed();
debug!("Initialised client in {elapsed_time:.2?}");
client
}
async fn initialise_framework() -> Framework<Data, Error> {
let start_time = Instant::now();
let framework = Framework::builder()
.setup(|ctx, _, _| Box::pin(handle(ctx)))
.options(FrameworkOptions {
commands: commands::get_commands(),
on_error: |e| Box::pin(handlers::error::handle(e)),
event_handler: |ctx, event, framework, data| {
Box::pin(handlers::event::handle(ctx, event, framework, data))
},
..Default::default()
})
.build();
let elapsed_time = start_time.elapsed();
debug!("Initialised framework in {elapsed_time:.2?}");
framework
}
fn initialise_subscriber(level: Level) {
let start_time = Instant::now();
let subscriber = Subscriber::builder()
.with_max_level(level)
.with_env_filter(EnvFilter::from_default_env())
.compact()
.finish();
match subscriber::set_global_default(subscriber) {
Ok(_) => (),
Err(reason) => error!("Couldn't set global default for logger: {reason:?}"),
};
let elapsed_time = start_time.elapsed();
debug!("Initialised logger in {elapsed_time:.2?}");
}