-
Notifications
You must be signed in to change notification settings - Fork 559
/
main.rs
536 lines (465 loc) · 18.9 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Requires the 'framework' feature flag be enabled in your project's
//! `Cargo.toml`.
//!
//! This can be enabled by specifying the feature in the dependency section:
//!
//! ```toml
//! [dependencies.serenity]
//! git = "https://github.com/serenity-rs/serenity.git"
//! features = ["framework", "standard_framework"]
//! ```
use std::{collections::{HashMap, HashSet}, env, fmt::Write, hash::BuildHasher, sync::Arc};
use serenity::{
client::bridge::gateway::{ShardId, ShardManager},
framework::standard::{
Args, CheckResult, CommandOptions, CommandResult, CommandGroup,
DispatchError, HelpOptions, help_commands, StandardFramework,
macros::{command, group, help, check},
},
model::{channel::{Channel, Message}, gateway::Ready, id::UserId},
utils::{content_safe, ContentSafeOptions},
};
// This imports `typemap`'s `Key` as `TypeMapKey`.
use serenity::prelude::*;
// A container type is created for inserting into the Client's `data`, which
// allows for data to be accessible across all events and framework commands, or
// anywhere else that has a copy of the `data` Arc.
struct ShardManagerContainer;
impl TypeMapKey for ShardManagerContainer {
type Value = Arc<Mutex<ShardManager>>;
}
struct CommandCounter;
impl TypeMapKey for CommandCounter {
type Value = HashMap<String, u64>;
}
struct Handler;
impl EventHandler for Handler {
fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
group!({
name: "general",
options: {},
commands: [about, say, commands, ping, some_long_command]
});
group!({
name: "emoji",
options: {
// Sets multiple prefixes for a group.
// This requires us to call commands in this group
// via `~emoji` (or `~e`) instead of just `~`.
prefixes: ["emoji", "em"],
// Set a description to appear if a user wants to display a single group
// e.g. via help using the group-name or one of its prefixes.
description: "A group with commands providing an emoji as response.",
// Sets a command that will be executed if only a group-prefix was passed.
default_command: bird,
},
commands: [cat, dog],
});
group!({
name: "math",
options: {
// Sets a single prefix for this group.
// So one has to call commands in this group
// via `~math` instead of just `~`.
prefix: "math",
},
commands: [multiply],
});
group!({
name: "owner",
options: {
owners_only: true,
// Limit all commands to be guild-restricted.
only: "guilds",
// Adds checks that need to be passed.
checks: [Admin],
},
commands: [am_i_admin, slow_mode],
});
// The framework provides two built-in help commands for you to use.
// But you can also make your own customized help command that forwards
// to the behaviour of either of them.
#[help]
// This replaces the information that a user can pass
// a command-name as argument to gain specific information about it.
#[individual_command_tip =
"Hello! こんにちは!Hola! Bonjour! 您好!\n\
If you want more information about a specific command, just pass the command as argument."]
// Some arguments require a `{}` in order to replace it with contextual information.
// In this case our `{}` refers to a command's name.
#[command_not_found_text = "Could not find: `{}`."]
// Define the maximum Levenshtein-distance between a searched command-name
// and commands. If the distance is lower than or equal the set distance,
// it will be displayed as a suggestion.
// Setting the distance to 0 will disable suggestions.
#[max_levenshtein_distance(3)]
// When you use sub-groups, Serenity will use the `indention_prefix` to indicate
// how deeply an item is indented.
// The default value is "-", it will be changed to "+".
#[indention_prefix = "+"]
// On another note, you can set up the help-menu-filter-behaviour.
// Here are all possible settings shown on all possible options.
// First case is if a user lacks permissions for a command, we can hide the command.
#[lacking_permissions = "Hide"]
// If the user is nothing but lacking a certain role, we just display it hence our variant is `Nothing`.
#[lacking_role = "Nothing"]
// The last `enum`-variant is `Strike`, which ~~strikes~~ a command.
#[wrong_channel = "Strike"]
// Serenity will automatically analyse and generate a hint/tip explaining the possible
// cases of ~~strikethrough-commands~~, but only if
// `strikethrough_commands_tip(Some(""))` keeps `Some()` wrapping an empty `String`, which is the default value.
// If the `String` is not empty, your given `String` will be used instead.
// If you pass in a `None`, no hint will be displayed at all.
fn my_help(
context: &mut Context,
msg: &Message,
args: Args,
help_options: &'static HelpOptions,
groups: &[&'static CommandGroup],
owners: HashSet<UserId, impl BuildHasher>
) -> CommandResult {
help_commands::with_embeds(context, msg, args, help_options, groups, owners)
}
fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect(
"Expected a token in the environment",
);
let mut client = Client::new(&token, Handler).expect("Err creating client");
{
let mut data = client.data.write();
data.insert::<CommandCounter>(HashMap::default());
data.insert::<ShardManagerContainer>(Arc::clone(&client.shard_manager));
}
// We will fetch your bot's owners and id
let (owners, bot_id) = match client.cache_and_http.http.get_current_application_info() {
Ok(info) => {
let mut owners = HashSet::new();
owners.insert(info.owner.id);
(owners, info.id)
},
Err(why) => panic!("Could not access application info: {:?}", why),
};
// Commands are equivalent to:
// "~about"
// "~emoji cat"
// "~emoji dog"
// "~multiply"
// "~ping"
// "~some long command"
client.with_framework(
// Configures the client, allowing for options to mutate how the
// framework functions.
//
// Refer to the documentation for
// `serenity::ext::framework::Configuration` for all available
// configurations.
StandardFramework::new()
.configure(|c| c
.with_whitespace(true)
.on_mention(Some(bot_id))
.prefix("~")
// You can set multiple delimiters via delimiters()
// or just one via delimiter(",")
// If you set multiple delimiters, the order you list them
// decides their priority (from first to last).
//
// In this case, if "," would be first, a message would never
// be delimited at ", ", forcing you to trim your arguments if you
// want to avoid whitespaces at the start of each.
.delimiters(vec![", ", ","])
// Sets the bot's owners. These will be used for commands that
// are owners only.
.owners(owners))
// Set a function to be called prior to each command execution. This
// provides the context of the command, the message that was received,
// and the full name of the command that will be called.
//
// You can not use this to determine whether a command should be
// executed. Instead, `set_check` is provided to give you this
// functionality.
.before(|ctx, msg, command_name| {
println!("Got command '{}' by user '{}'",
command_name,
msg.author.name);
// Increment the number of times this command has been run once. If
// the command's name does not exist in the counter, add a default
// value of 0.
let mut data = ctx.data.write();
let counter = data.get_mut::<CommandCounter>().expect("Expected CommandCounter in ShareMap.");
let entry = counter.entry(command_name.to_string()).or_insert(0);
*entry += 1;
true // if `before` returns false, command processing doesn't happen.
})
// Similar to `before`, except will be called directly _after_
// command execution.
.after(|_, _, command_name, error| {
match error {
Ok(()) => println!("Processed command '{}'", command_name),
Err(why) => println!("Command '{}' returned error {:?}", command_name, why),
}
})
// Set a function that's called whenever an attempted command-call's
// command could not be found.
.unrecognised_command(|_, _, unknown_command_name| {
println!("Could not find command named '{}'", unknown_command_name);
})
// Set a function that's called whenever a message is not a command.
.normal_message(|_, message| {
println!("Message is not a command '{}'", message.content);
})
// Set a function that's called whenever a command's execution didn't complete for one
// reason or another. For example, when a user has exceeded a rate-limit or a command
// can only be performed by the bot owner.
.on_dispatch_error(|ctx, msg, error| {
if let DispatchError::Ratelimited(seconds) = error {
let _ = msg.channel_id.say(&ctx.http, &format!("Try this again in {} seconds.", seconds));
}
})
.help(&MY_HELP_HELP_COMMAND)
// Can't be used more than once per 5 seconds:
.bucket("emoji", |b| b.delay(5))
// Can't be used more than 2 times per 30 seconds, with a 5 second delay:
.bucket("complicated", |b| b.delay(5).time_span(30).limit(2))
// The `group!` macro generates `static` instances of the options set for the group.
// They're made in the pattern: `#name_GROUP` for the group instance and `#name_GROUP_OPTIONS`.
// #name is turned all uppercase
.group(&GENERAL_GROUP)
.group(&EMOJI_GROUP)
.group(&MATH_GROUP)
.group(&OWNER_GROUP)
);
if let Err(why) = client.start() {
println!("Client error: {:?}", why);
}
}
// Commands can be created via the attribute `#[command]` macro.
#[command]
// Options are passed via subsequent attributes.
// Make this command use the "complicated" bucket.
#[bucket = "complicated"]
fn commands(ctx: &mut Context, msg: &Message) -> CommandResult {
let mut contents = "Commands used:\n".to_string();
let data = ctx.data.read();
let counter = data.get::<CommandCounter>().expect("Expected CommandCounter in ShareMap.");
for (k, v) in counter {
let _ = write!(contents, "- {name}: {amount}\n", name=k, amount=v);
}
if let Err(why) = msg.channel_id.say(&ctx.http, &contents) {
println!("Error sending message: {:?}", why);
}
Ok(())
}
// Repeats what the user passed as argument but ensures that user and role
// mentions are replaced with a safe textual alternative.
// In this example channel mentions are excluded via the `ContentSafeOptions`.
#[command]
fn say(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
let settings = if let Some(guild_id) = msg.guild_id {
// By default roles, users, and channel mentions are cleaned.
ContentSafeOptions::default()
// We do not want to clean channal mentions as they
// do not ping users.
.clean_channel(false)
// If it's a guild channel, we want mentioned users to be displayed
// as their display name.
.display_as_member_from(guild_id)
} else {
ContentSafeOptions::default()
.clean_channel(false)
.clean_role(false)
};
let content = content_safe(&ctx.cache, &args.rest(), &settings);
if let Err(why) = msg.channel_id.say(&ctx.http, &content) {
println!("Error sending message: {:?}", why);
}
Ok(())
}
// A function which acts as a "check", to determine whether to call a command.
//
// In this case, this command checks to ensure you are the owner of the message
// in order for the command to be executed. If the check fails, the command is
// not called.
#[check]
#[name = "Owner"]
fn owner_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult {
// Replace 7 with your ID to make this check pass.
//
// `true` will convert into `CheckResult::Success`,
//
// `false` will convert into `CheckResult::Failure(Reason::Unknown)`,
//
// and if you want to pass a reason alongside failure you can do:
// `CheckResult::new_user("Lacked admin permission.")`,
//
// if you want to mark it as something you want to log only:
// `CheckResult::new_log("User lacked admin permission.")`,
//
// and if the check's failure origin is unknown you can mark it as such (same as using `false.into`):
// `CheckResult::new_unknown()`
(msg.author.id == 7).into()
}
// A function which acts as a "check", to determine whether to call a command.
//
// This check analyses whether a guild member permissions has
// administrator-permissions.
#[check]
#[name = "Admin"]
// Whether the check shall be tested in the help-system.
#[check_in_help(true)]
// Whether the check shall be displayed in the help-system.
#[display_in_help(true)]
fn admin_check(ctx: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult {
if let Some(member) = msg.member(&ctx.cache) {
if let Ok(permissions) = member.permissions(&ctx.cache) {
return permissions.administrator().into();
}
}
false.into()
}
#[command]
fn some_long_command(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, &format!("Arguments: {:?}", args.rest())) {
println!("Error sending message: {:?}", why);
}
Ok(())
}
#[command]
// Limits the usage of this command to roles named:
#[allowed_roles("mods", "ultimate neko")]
fn about_role(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
let potential_role_name = args.rest();
if let Some(guild) = msg.guild(&ctx.cache) {
// `role_by_name()` allows us to attempt attaining a reference to a role
// via its name.
if let Some(role) = guild.read().role_by_name(&potential_role_name) {
if let Err(why) = msg.channel_id.say(&ctx.http, &format!("Role-ID: {}", role.id)) {
println!("Error sending message: {:?}", why);
}
return Ok(());
}
}
if let Err(why) = msg.channel_id.say(&ctx.http, format!("Could not find role named: {:?}", potential_role_name)) {
println!("Error sending message: {:?}", why);
}
Ok(())
}
#[command]
// Lets us also call `~math *` instead of just `~math multiply`.
#[aliases("*")]
fn multiply(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let first = args.single::<f64>()?;
let second = args.single::<f64>()?;
let res = first * second;
if let Err(why) = msg.channel_id.say(&ctx.http, &res.to_string()) {
println!("Err sending product of {} and {}: {:?}", first, second, why);
}
Ok(())
}
#[command]
fn about(ctx: &mut Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, "This is a small test-bot! : )") {
println!("Error sending message: {:?}", why);
}
Ok(())
}
#[command]
fn latency(ctx: &mut Context, msg: &Message) -> CommandResult {
// The shard manager is an interface for mutating, stopping, restarting, and
// retrieving information about shards.
let data = ctx.data.read();
let shard_manager = match data.get::<ShardManagerContainer>() {
Some(v) => v,
None => {
let _ = msg.reply(&ctx, "There was a problem getting the shard manager");
return Ok(());
},
};
let manager = shard_manager.lock();
let runners = manager.runners.lock();
// Shards are backed by a "shard runner" responsible for processing events
// over the shard, so we'll get the information about the shard runner for
// the shard this command was sent over.
let runner = match runners.get(&ShardId(ctx.shard_id)) {
Some(runner) => runner,
None => {
let _ = msg.reply(&ctx, "No shard found");
return Ok(());
},
};
let _ = msg.reply(&ctx, &format!("The shard latency is {:?}", runner.latency));
Ok(())
}
#[command]
// Limit command usage to guilds.
#[only_in(guilds)]
#[checks(Owner)]
fn ping(ctx: &mut Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong! : )") {
println!("Error sending message: {:?}", why);
}
Ok(())
}
#[command]
// Adds multiple aliases
#[aliases("kitty", "neko")]
// Make this command use the "emoji" bucket.
#[bucket = "emoji"]
// Allow only administrators to call this:
#[required_permissions("ADMINISTRATOR")]
fn cat(ctx: &mut Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, ":cat:") {
println!("Error sending message: {:?}", why);
}
Ok(())
}
#[command]
#[description = "Sends an emoji with a dog."]
#[bucket = "emoji"]
fn dog(ctx: &mut Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, ":dog:") {
println!("Error sending message: {:?}", why);
}
Ok(())
}
#[command]
fn bird(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
let say_content = if args.is_empty() {
":bird: can find animals for you.".to_string()
} else {
format!(":bird: could not find animal named: `{}`.", args.rest())
};
if let Err(why) = msg.channel_id.say(&ctx.http, say_content) {
println!("Error sending message: {:?}", why);
}
Ok(())
}
#[command]
fn am_i_admin(ctx: &mut Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, "Yes you are.") {
println!("Error sending message: {:?}", why);
}
Ok(())
}
#[command]
fn slow_mode(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let say_content = if let Ok(slow_mode_rate_seconds) = args.single::<u64>() {
if let Err(why) = msg.channel_id.edit(&ctx.http, |c| c.slow_mode_rate(slow_mode_rate_seconds)) {
println!("Error setting channel's slow mode rate: {:?}", why);
format!("Failed to set slow mode to `{}` seconds.", slow_mode_rate_seconds)
} else {
format!("Successfully set slow mode rate to `{}` seconds.", slow_mode_rate_seconds)
}
} else if let Some(Channel::Guild(channel)) = msg.channel_id.to_channel_cached(&ctx.cache) {
format!("Current slow mode rate is `{}` seconds.", channel.read().slow_mode_rate.unwrap_or(0))
} else {
"Failed to find channel in cache.".to_string()
};
if let Err(why) = msg.channel_id.say(&ctx.http, say_content) {
println!("Error sending message: {:?}", why);
}
Ok(())
}