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
|
use std::time::Duration;
use std::{env, error};
use handler::pinboard::PinBoard;
use log::*;
use poise::serenity_prelude as serentiy;
mod api;
mod commands;
mod consts;
mod handler;
mod utils;
type Error = Box<dyn error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
#[derive(Clone)]
pub struct Data {
pin_board: Option<PinBoard>,
}
impl Default for Data {
fn default() -> Self {
Self::new()
}
}
impl Data {
pub fn new() -> Self {
let pin_board = PinBoard::new();
Self { pin_board }
}
}
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
match error {
poise::FrameworkError::Setup { error, .. } => panic!("failed to start bot: {error:?}"),
poise::FrameworkError::Command { error, ctx } => {
error!("error in command {}: {:?}", ctx.command().name, error);
}
error => {
if let Err(e) = poise::builtins::on_error(error).await {
error!("error while handling an error: {}", e);
}
}
}
}
#[tokio::main]
async fn main() {
env_logger::init();
dotenvy::dotenv().unwrap();
let options = poise::FrameworkOptions {
commands: commands::to_global_commands(),
event_handler: |ctx, event, framework, data| {
Box::pin(handler::handle(ctx, event, framework, data))
},
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("!".into()),
edit_tracker: Some(poise::EditTracker::for_timespan(Duration::from_secs(3600))),
..Default::default()
},
on_error: |error| Box::pin(on_error(error)),
command_check: Some(|ctx| {
Box::pin(async move { Ok(ctx.author().id != ctx.framework().bot_id) })
}),
..Default::default()
};
let framework = poise::Framework::builder()
.options(options)
.token(env::var("TOKEN").expect("couldn't find token in environment."))
.intents(serentiy::GatewayIntents::all())
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
info!("registered global commands!");
poise::builtins::register_in_guild(
ctx,
&commands::to_guild_commands(),
consts::TEAWIE_GUILD,
)
.await?;
info!("registered guild commands!");
Ok(Data::new())
})
});
framework.run().await.unwrap()
}
|