-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
101 lines (84 loc) · 2.83 KB
/
main.rs
File metadata and controls
101 lines (84 loc) · 2.83 KB
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
pub mod check;
pub mod command;
pub mod config;
pub mod database;
pub mod event_handler;
pub mod message;
pub mod state;
pub mod util;
use std::sync::Arc;
use std::time::Duration;
use poise::serenity_prelude::{Client, GatewayIntents};
use poise::{CreateReply, Framework, FrameworkOptions};
pub use crate::config::Config;
pub use crate::message::Message;
pub use crate::state::State;
pub type Context<'a> = poise::Context<'a, State, anyhow::Error>;
pub async fn build_bot() -> anyhow::Result<()> {
let config = Config::new()?;
let prefix = config.bot_prefix.clone();
let token = config.bot_token.clone();
let options = FrameworkOptions {
commands: vec![
command::ping(),
command::help(),
command::add(),
command::delete(),
command::verify(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some(prefix),
edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan(
Duration::from_secs(3600),
))),
..Default::default()
},
event_handler: |ctx, event, framework, data| {
Box::pin(event_handler::event_handler(ctx, event, framework, data))
},
on_error: |error| {
Box::pin(async move {
if let poise::FrameworkError::Command { ctx, .. } = error {
let _ = ctx
.send(
CreateReply::default()
.content(Message::Error)
.ephemeral(true),
)
.await;
return;
}
if let poise::FrameworkError::Setup { error, .. } = error {
tracing::error!("Framework setup error: {:?}", error);
return;
}
tracing::error!("Other framework error (no user context to reply to).");
})
},
..Default::default()
};
let framework = Framework::builder()
.setup(move |ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
State::new(config).await
})
})
.options(options)
.build();
let intents = GatewayIntents::all() | GatewayIntents::GUILD_MESSAGE_REACTIONS;
let mut client = Client::builder(&token, intents)
.framework(framework)
.await?;
client.start().await?;
Ok(())
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.pretty()
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::rfc_3339())
.init();
build_bot().await
}