summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: a93e102964f02a725e98c814a056bcf089b42207 (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
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
use std::{error, time};

use log::*;
use poise::serenity_prelude as serentiy;
use settings::Settings;

mod api;
mod colors;
mod commands;
mod consts;
mod handler;
mod settings;
mod utils;

type Error = Box<dyn error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;

#[derive(Clone)]
pub struct Data {
	settings: Option<Settings>,
}

impl Data {
	pub fn new() -> Self {
		let settings = Settings::new();

		Self { settings }
	}
}

impl Default for Data {
	fn default() -> Self {
		Self::new()
	}
}

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().ok();

	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(time::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(std::env::var("TOKEN").expect("couldn't find token in environment."))
		.intents(
			serentiy::GatewayIntents::non_privileged() | serentiy::GatewayIntents::MESSAGE_CONTENT,
		)
		.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()
}