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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::api::rcos::users::discord_whois::DiscordWhoIs;
use crate::discord_bot::commands::InteractionResult;
use crate::env::global_config;
use serenity::builder::{CreateApplicationCommand, CreateApplicationCommandOption, CreateEmbed};
use serenity::client::Context;
use serenity::model::interactions::application_command::ApplicationCommandInteraction;
use serenity::model::interactions::{
application_command::ApplicationCommandOptionType, InteractionResponseType,
};
use serenity::model::prelude::InteractionApplicationCommandCallbackDataFlags;
use serenity::utils::Color;
use serenity::Result as SerenityResult;
pub const COMMAND_NAME: &'static str = "whois";
pub const OPTION_NAME: &'static str = "user";
pub const ERROR_COLOR: Color = Color::new(0xDC3545);
fn whois_option(obj: &mut CreateApplicationCommandOption) -> &mut CreateApplicationCommandOption {
obj.name(OPTION_NAME)
.kind(ApplicationCommandOptionType::User)
.description("The user to get information about")
.required(true)
}
pub fn create_whois(obj: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
obj.name(COMMAND_NAME)
.description("Get information about a member of RCOS")
.create_option(whois_option)
}
pub fn handle_whois<'a>(
ctx: &'a Context,
interaction: &'a ApplicationCommandInteraction,
) -> InteractionResult<'a> {
return Box::pin(async move { handle(ctx, interaction).await });
}
async fn handle(ctx: &Context, interaction: &ApplicationCommandInteraction) -> SerenityResult<()> {
let user_id = interaction
.data
.options
.get(0)
.filter(|opt| opt.name == OPTION_NAME)
.and_then(|opt| opt.value.as_ref())
.and_then(|val| val.as_str())
.and_then(|string| string.parse::<u64>().ok())
.ok_or_else(|| {
error!(
"'/whois' command missing user option. Interaction: {:#?}",
interaction
);
})
.unwrap();
let rcos_api_response = DiscordWhoIs::send(user_id)
.await
.map_err(|err| {
error!("Could not query the RCOS API: {}", err);
err
});
if let Err(err) = rcos_api_response {
return interaction
.create_interaction_response(&ctx.http, |create_response| {
create_response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|rdata| {
rdata
.allowed_mentions(|am| am.empty_parse())
.flags(InteractionApplicationCommandCallbackDataFlags::EPHEMERAL)
.create_embed(|embed| {
embed_common(embed)
.color(ERROR_COLOR)
.title("RCOS API Error")
.description(
"We could not get data about this user because the \
RCOS API responded with an error. Please contact a coordinator and \
report this error on Telescope's GitHub.",
)
.field("Error Message", err, false)
})
})
})
.await;
}
let rcos_user: Option<_> = rcos_api_response.unwrap().get_user();
return interaction
.create_interaction_response(&ctx.http, |create_response| {
create_response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|rdata| {
rdata
.allowed_mentions(|am| am.empty_parse())
.flags(InteractionApplicationCommandCallbackDataFlags::EPHEMERAL)
.create_embed(|create_embed| {
embed_common(create_embed);
if let Some(u) = rcos_user {
create_embed
.title(format!("{} {}", u.first_name, u.last_name))
.url(format!("{}/user/{}", global_config().telescope_url, u.id))
.field("User Role", u.role, true);
let rcs_id = u
.rcs_id
.get(0)
.map(|o| format!("{}@rpi.edu", o.account_id))
.unwrap_or("RPI CAS not linked to this user.".into());
create_embed.field("RPI Email", rcs_id, true)
} else {
create_embed
.color(ERROR_COLOR)
.description("User not found in RCOS database.")
}
})
})
})
.await;
}
fn embed_common(create_embed: &mut CreateEmbed) -> &mut CreateEmbed {
create_embed
.timestamp(&chrono::Utc::now())
.footer(|create_footer| {
create_footer.text(format!("Telescope {}", env!("CARGO_PKG_VERSION")))
})
.author(|create_author| {
create_author
.name("Telescope")
.url("https://github.com/rcos/Telescope")
})
}