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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
mod add_points;
mod leaderboard;
mod revise;
pub use add_points::add_points;
pub use leaderboard::leaderboard;
pub use revise::revise;
use std::sync::Arc;
use twilight_model::{
application::{
command::{Command, CommandType},
interaction::application_command::{CommandData, CommandOptionValue},
},
gateway::payload::incoming::InteractionCreate,
http::interaction::InteractionResponseData,
id::{Id, marker::GuildMarker},
};
use twilight_util::builder::{
InteractionResponseDataBuilder,
command::{CommandBuilder, IntegerBuilder, RoleBuilder, StringBuilder, SubCommandBuilder},
embed::{EmbedBuilder, EmbedFieldBuilder},
};
use crate::{bail, brain::Brain, database::PermissionSetting, fail, import, success};
pub enum Commands {
About,
Leaderboard,
Points,
Revise,
Permission,
Import,
}
pub async fn handler(
brain: Arc<Brain>,
guild: Id<GuildMarker>,
create: &InteractionCreate,
command: Commands,
command_data: &CommandData,
) {
// Handle the command and create our interaction response data
let data = match command {
Commands::About => about(brain.clone(), guild),
Commands::Leaderboard => leaderboard(brain.clone(), guild, Some(command_data)),
Commands::Points => add_points(brain.clone(), guild, create, command_data).await,
Commands::Revise => revise(brain.clone(), guild, create, command_data).await,
Commands::Permission => permission(brain.clone(), guild, create, command_data).await,
Commands::Import => {
import(brain.clone(), guild, create, command_data).await;
return;
}
};
// Finally, send back the response
brain.interaction_respond(create, data).await;
}
pub async fn build() -> Vec<Command> {
let about = CommandBuilder::new(
"about",
"Get information about the bot",
CommandType::ChatInput,
)
.build();
let leaderboard = CommandBuilder::new(
"leaderboard",
"View the server leaderboard",
CommandType::ChatInput,
)
.option(
StringBuilder::new("style", "style of leaderboard to display")
.choices([("Ties Equal", "equal"), ("Ties Broken", "broken")]),
)
.build();
let points = CommandBuilder::new("points", "Add and remove points!", CommandType::ChatInput)
.option(IntegerBuilder::new("points", "number of points. - or +").required(true))
.option(
StringBuilder::new("users", "mention people to modify their points!!").required(true),
)
.validate()
.unwrap()
.build();
let revise = CommandBuilder::new(
"revise",
"Change history by adding/removing points at a specific point in time",
CommandType::ChatInput,
)
.option(IntegerBuilder::new("points", "number of points. - or +").required(true))
.option(StringBuilder::new("users", "mention people to modify their points!!").required(true))
.option(
StringBuilder::new(
"date",
"Date string in the YYYY-DD-MM format, with optional time component (HH:MM:SS, 24-hour time)",
)
.required(true),
)
.validate()
.unwrap()
.build();
let permission = CommandBuilder::new(
"permission",
"set who is allowed to change points",
CommandType::ChatInput,
)
.option(
SubCommandBuilder::new("role", "require a role to change the score")
.option(RoleBuilder::new("role", "role required").required(true)),
)
.option(SubCommandBuilder::new(
"none",
"anyone can change the score",
))
.build();
let import = CommandBuilder::new(
"import",
"import this server's emboard leaderboard by adding the points in emboard, to leaberblord's",
CommandType::ChatInput,
)
.build();
vec![about, leaderboard, points, revise, permission, import]
}
pub fn about(_brain: Arc<Brain>, _guild: Id<GuildMarker>) -> InteractionResponseData {
let embed = EmbedBuilder::new()
.title("About Leaberblord")
.description(
"A single-purpose bot for keeping score. Written in Rust using the twilight set of crates.",
).field(EmbedFieldBuilder::new("Source", "The source is available on the author's [cgit instance](https://git.dreamy.place/whimsy/leaberblord/about)"))
.field(EmbedFieldBuilder::new("Author", "Written by @gennyble. Her homepage is [dreamy.place](https://dreamy.place)"))
.color(0x33aa88).build();
InteractionResponseDataBuilder::new()
.embeds([embed])
.build()
}
pub async fn permission(
brain: Arc<Brain>,
guild: Id<GuildMarker>,
create: &InteractionCreate,
data: &CommandData,
) -> InteractionResponseData {
let member = create.member.clone().unwrap();
let permissions = member.permissions.unwrap();
if !permissions.contains(twilight_model::guild::Permissions::MANAGE_GUILD) {
bail!("You must have the Manage Server permission to perform this action");
}
for opt in &data.options {
return match opt.name.as_str() {
"none" => permission_none(brain, guild),
"role" => permission_role(brain, guild, opt.value.clone()),
_ => {
eprintln!("permission command, unknown option '{}'", opt.name);
fail!("this should be unreachable. report the bug maybe? to gennyble on discord")
}
};
}
fail!("this should be unreachable. report the bug maybe? to gennyble on discord")
}
fn permission_none(brain: Arc<Brain>, guild: Id<GuildMarker>) -> InteractionResponseData {
if brain.create_leaderboard_if_not_exists(guild) {
// If it already existed, we might not be on default none. So set it.
brain
.db
.set_leaderboard_permission(guild.get(), PermissionSetting::None)
.unwrap();
}
success!("Permission for leaderboard changed to none. Anyone may now set the score.")
}
fn permission_role(
brain: Arc<Brain>,
guild: Id<GuildMarker>,
data: CommandOptionValue,
) -> InteractionResponseData {
let role = if let CommandOptionValue::SubCommand(data) = data {
match data.iter().find(|d| d.name == "role").unwrap().value {
CommandOptionValue::Role(role) => role,
_ => panic!(),
}
} else {
panic!()
};
brain.create_leaderboard_if_not_exists(guild);
brain
.db
.set_leaderboard_permission(guild.get(), PermissionSetting::RoleRequired)
.unwrap();
brain
.db
.set_leaderboard_permission_role(guild.get(), role.get())
.unwrap();
success!(format!(
"Permission for leaderboard changed to Role Required. \
Only members of <@&{role}> may now set the score"
))
}
pub async fn import(
brain: Arc<Brain>,
guild: Id<GuildMarker>,
create: &InteractionCreate,
_data: &CommandData,
) {
let member = create.member.clone().unwrap();
let permissions = member.permissions.unwrap();
if !permissions.contains(twilight_model::guild::Permissions::MANAGE_GUILD) {
brain
.interaction_respond(
create,
fail!("You must have the Manage Server permission to perform this action"),
)
.await;
} else {
import::emboard_direct::import(brain, guild, create).await;
}
}
|