forked from Headline/discord-compiler-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
148 lines (125 loc) · 4.27 KB
/
main.rs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![type_length_limit = "1146253"]
mod apis;
mod boilerplate;
mod cache;
mod commands;
mod cppeval;
mod events;
mod managers;
mod slashcmds;
mod stats;
mod tests;
mod utls;
use serenity::framework::{standard::macros::group, StandardFramework};
use serenity::http::Http;
use serenity::prelude::GatewayIntents;
use std::collections::HashSet;
use std::{env, error::Error};
use crate::apis::dbl::BotsListApi;
#[macro_use]
extern crate log;
extern crate pretty_env_logger;
/** Command Registration **/
use crate::commands::{
asm::*, block::*, botinfo::*, compile::*, compilers::*, cpp::*, format::*, formats::*, help::*,
invite::*, languages::*, ping::*, unblock::*,
};
use crate::utls::discordhelpers::embeds::panic_embed;
use crate::utls::discordhelpers::manual_dispatch;
#[group]
#[commands(
botinfo, compile, languages, compilers, ping, help, asm, block, unblock, invite, cpp, formats,
format
)]
struct General;
/** Spawn bot **/
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
if let Err(e) = dotenv::dotenv() {
error!("Unable to find .env configuration file: {}", e);
}
pretty_env_logger::init();
let token = env::var("BOT_TOKEN").expect("Expected bot token in .env file");
let http = Http::new(&token);
let (owners, bot_id) = match http.get_current_application_info().await {
Ok(info) => {
let mut owners = HashSet::new();
owners.insert(info.owner.id);
if let Some(team) = info.team {
for member in &team.members {
owners.insert(member.user.id);
}
}
(owners, info.id)
}
Err(why) => {
warn!("Could not access application info: {:?}", why);
warn!("Trying environment variable for bot id...");
let id = env::var("BOT_ID").expect("Unable to find BOT_ID environment variable");
let bot_id = id.parse::<u64>().expect("Invalid bot id");
(HashSet::new(), serenity::model::id::ApplicationId(bot_id))
}
};
info!(
"Registering owner(s): {}",
owners
.iter()
.map(|o| format!("{}", o.0))
.collect::<Vec<String>>()
.join(", ")
);
if cfg!(debug_assertions) {
warn!("Running bot in DEBUG mode...");
}
let prefix = env::var("BOT_PREFIX").expect("Expected bot prefix in .env file");
let app_id = env::var("APPLICATION_ID").expect("Expected application id in .env file");
let framework = StandardFramework::new()
.before(events::before)
.after(events::after)
.configure(|c| c.owners(owners).prefix(&prefix))
.group(&GENERAL_GROUP)
.bucket("nospam", |b| b.delay(3).time_span(10).limit(3))
.await
.on_dispatch_error(events::dispatch_error);
let intents = GatewayIntents::GUILDS
| GatewayIntents::MESSAGE_CONTENT
| GatewayIntents::GUILD_INTEGRATIONS
| GatewayIntents::GUILD_MESSAGE_REACTIONS
| GatewayIntents::GUILD_MESSAGES;
let mut client = serenity::Client::builder(token, intents)
.framework(framework)
.event_handler(events::Handler)
.application_id(app_id.parse::<u64>().unwrap())
.await?;
cache::fill(
client.data.clone(),
&prefix,
bot_id.0,
client.shard_manager.clone(),
)
.await?;
if let Ok(plog) = env::var("PANIC_LOG") {
let default_panic = std::panic::take_hook();
let http = client.cache_and_http.http.clone();
std::panic::set_hook(Box::new(move |info| {
let http = http.clone();
if let Ok(plog_parse) = plog.parse::<u64>() {
let panic_str = info.to_string();
tokio::spawn({
async move { manual_dispatch(http, plog_parse, panic_embed(panic_str)).await }
});
} else {
warn!("Unable to parse channel id64 from PANIC_LOG, is it valid?");
}
default_panic(info);
}));
}
let dbl = BotsListApi::new();
if dbl.should_spawn() {
dbl.spawn(client.cache_and_http.http.clone(), client.data.clone());
}
if let Err(why) = client.start_autosharded().await {
error!("Client error: {:?}", why);
}
Ok(())
}