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 color_eyre::eyre::{eyre, Context as _, Report, Result};
use log::*;
use poise::{
serenity_prelude as serenity, EditTracker, Framework, FrameworkOptions, PrefixFrameworkOptions,
};
use settings::Settings;
mod api;
mod colors;
mod commands;
mod consts;
mod handlers;
mod settings;
mod utils;
type Context<'a> = poise::Context<'a, Data, Report>;
#[derive(Clone)]
pub struct Data {
redis: redis::Client,
}
impl Data {
pub fn new() -> Result<Self> {
let redis_url = std::env::var("REDIS_URL")
.wrap_err_with(|| eyre!("Couldn't find Redis URL in environment!"))?;
let redis = redis::Client::open(redis_url)?;
Ok(Self { redis })
}
}
#[tokio::main]
async fn main() -> Result<()> {
dotenvy::dotenv().ok();
color_eyre::install()?;
env_logger::init();
let token =
std::env::var("TOKEN").wrap_err_with(|| eyre!("Couldn't find token in environment!"))?;
let intents =
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
let options = FrameworkOptions {
commands: commands::to_global_commands(),
on_error: |error| Box::pin(handlers::handle_error(error)),
command_check: Some(|ctx| {
Box::pin(async move { Ok(ctx.author().id != ctx.framework().bot_id) })
}),
event_handler: |ctx, event, framework, data| {
Box::pin(handlers::handle_event(ctx, event, framework, data))
},
prefix_options: PrefixFrameworkOptions {
prefix: Some("!".into()),
edit_tracker: Some(EditTracker::for_timespan(Duration::from_secs(3600))),
..Default::default()
},
..Default::default()
};
let framework = Framework::builder()
.token(token)
.intents(intents)
.options(options)
.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 to {}", consts::TEAWIE_GUILD);
let data = Data::new()?;
Ok(data)
})
});
tokio::select! {
result = framework.run() => { result.map_err(Report::from) },
_ = tokio::signal::ctrl_c() => {
info!("Interrupted! Exiting...");
std::process::exit(130);
}
}
}
|