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
use crate::discord_bot::commands::{get_handler, register_commands_for_guild, InteractionHandler};
use crate::env::global_config;
use serenity::client::{Context, EventHandler};
use serenity::model::gateway::Ready;
use serenity::model::guild::Guild;
use serenity::model::interactions::Interaction;
pub struct Handler;
#[serenity::async_trait]
impl EventHandler for Handler {
async fn guild_create(&self, mut ctx: Context, guild: Guild, is_new: bool) {
info!(
"{}uild connected: {} (ID: {})",
is_new.then(|| "NEW g").unwrap_or("G"),
guild.name,
guild.id
);
if global_config().discord_config.rcos_guild_id == guild.id.to_string() {
info!(
"Registering telescope's Discord commands for guild \"{}\" (ID: {})",
guild.name, guild.id
);
register_commands_for_guild(&mut ctx, &guild)
.await
.unwrap_or_else(|err| {
error!(
"Could not register a command on guild with ID {}: {}",
guild.id, err
);
});
} else {
warn!(
"Non-RCOS guild connected: \"{}\" (ID: {})",
guild.name, guild.id
);
}
}
async fn ready(&self, ctx: Context, ready: Ready) {
info!(
"{} is connected! (user id: {})",
ready.user.tag(),
ready.user.id
);
ctx.http
.get_global_application_commands()
.await
.map(|list| {
info!(
"{} global application commands registered: {:#?}",
list.len(),
list
);
})
.unwrap_or_else(|err| {
error!("Could not get list of global application commands: {}", err);
});
}
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
match interaction {
Interaction::ApplicationCommand(command) => {
let command_name = command.data.name.clone();
let handler: Option<InteractionHandler> = get_handler(command_name.as_str());
if handler.is_none() {
error!(
"Handler not found for '/{}'. Command: {:#?}",
command_name, command
);
return;
}
let result: serenity::Result<()> = (handler.unwrap())(&ctx, &command).await;
if let Err(err) = result {
error!("'/{}' handler returned an error: {}", command_name, err);
}
}
other => warn!("Unhandled interaction: {:?}", other),
}
}
}