summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 749daaecdb36e9c306a19d0173730ca3342593ca (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::time::Duration;
use std::{env, error};

use crate::commands::*;
use crate::consts::*;
use crate::pinboard::PinBoard;
use log::*;
use poise::serenity_prelude as serentiy;
use poise::serenity_prelude::*;

mod api;
mod commands;
mod consts;
mod handler;
mod pinboard;
mod utils;

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

#[derive(Clone)]
pub struct Data {
	bot: serentiy::UserId,
	pin_board: Option<PinBoard>,
}

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

impl Data {
	pub fn new() -> Self {
		let bot = utils::parse_snowflake_from_env("BOT", UserId).unwrap_or(consts::BOT);
		let pin_board = PinBoard::new();

		Self { bot, 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 guild_commands = vec![copypasta::copypasta(), teawiespam::teawiespam()];

	let options = poise::FrameworkOptions {
		commands: vec![
			ask::ask(),
			bing::bing(),
			bottom::bottom(),
			convert::convert(),
			random_lore::random_lore(),
			random_shiggy::random_shiggy(),
			random_teawie::random_teawie(),
			copypasta::copypasta(),
			teawiespam::teawiespam(),
			version::version(),
		],
		event_handler: |ctx, event, _, data| {
			Box::pin(async move {
				// yes this is dumb. no i don't care.
				let handler = handler::Handler::new(data.clone());
				event.clone().dispatch(ctx.clone(), &handler).await;
				Ok(())
			})
		},
		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 && ctx.author().id != consts::BOT)
			})
		}),
		..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 {
				info!("logged in as {}", _ready.user.name);

				poise::builtins::register_globally(ctx, &framework.options().commands).await?;
				info!("registered global commands!");
				poise::builtins::register_in_guild(ctx, &guild_commands, TEAWIE_GUILD).await?;
				info!("registered guild commands!");

				Ok(Data::new())
			})
		});

	framework.run().await.unwrap()
}