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
|
//! 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"]
//! ```
#[macro_use]
extern crate serenity;
extern crate typemap;
use serenity::client::bridge::gateway::{ShardId, ShardManager};
use serenity::framework::standard::{Args, DispatchError, StandardFramework, HelpBehaviour, CommandOptions, help_commands};
use serenity::model::channel::Message;
use serenity::model::gateway::Ready;
use serenity::model::Permissions;
use serenity::prelude::Mutex;
use serenity::prelude::*;
use std::collections::HashMap;
use std::env;
use std::fmt::Write;
use std::sync::Arc;
use typemap::Key;
// 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 Key for ShardManagerContainer {
type Value = Arc<Mutex<ShardManager>>;
}
struct CommandCounter;
impl Key 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);
}
}
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.lock();
data.insert::<CommandCounter>(HashMap::default());
data.insert::<ShardManagerContainer>(Arc::clone(&client.shard_manager));
}
// 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
.allow_whitespace(true)
.on_mention(true)
.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![", ", ","]))
// 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.lock();
let counter = data.get_mut::<CommandCounter>().unwrap();
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 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(&format!("Try this again in {} seconds.", seconds));
}
})
// Can't be used more than once per 5 seconds:
.simple_bucket("emoji", 5)
// Can't be used more than 2 times per 30 seconds, with a 5 second delay:
.bucket("complicated", 5, 30, 2)
.command("about", |c| c.cmd(about))
// You can use the simple `help(help_commands::with_embeds)` or
// customise your help-menu via `customised_help()`.
.customised_help(help_commands::with_embeds, |c| {
// This replaces the information that a user can pass
// a command-name as argument to gain specific information about it.
c.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: `{}`.")
// 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(HelpBehaviour::Hide)
// If the user is nothing but lacking a certain role, we just display it hence our variant is `Nothing`.
.lacking_role(HelpBehaviour::Nothing)
// The last `enum`-variant is `Strike`, which ~~strikes~~ a command.
.wrong_channel(HelpBehaviour::Strike)
// Serenity will automatically analyse and generate a hint/tip explaining the possible
// cases of ~~strikethrough-commands~~, but only if
// `striked_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.
})
.command("commands", |c| c
// Make this command use the "complicated" bucket.
.bucket("complicated")
.cmd(commands))
.group("Emoji", |g| g
// Sets a single prefix for a group:
.prefix("emoji")
// Sets a command that will be executed if only a group-prefix was passed.
.default_cmd(dog)
.command("cat", |c| c
.desc("Sends an emoji with a cat.")
.batch_known_as(vec!["kitty", "neko"]) // Adds multiple aliases
.bucket("emoji") // Make this command use the "emoji" bucket.
.cmd(cat)
// Allow only administrators to call this:
.required_permissions(Permissions::ADMINISTRATOR))
.command("dog", |c| c
.desc("Sends an emoji with a dog.")
.bucket("emoji")
.cmd(dog)))
.group("Math", |g| g
// Sets multiple prefixes for a group.
// This requires us to call commands in this group
// via `~math` (or `~m`) instead of just `~`.
.prefixes(vec!["m", "math"])
.command("multiply", |c| c
.known_as("*") // Lets us also call `~math *` instead of just `~math multiply`.
.cmd(multiply)))
.command("latency", |c| c
.cmd(latency))
.command("ping", |c| c
.check(owner_check) // User needs to pass this test to run command
.cmd(ping))
.command("role", |c| c
.cmd(about_role)
// Limits the usage of this command to roles named:
.allowed_roles(vec!["mods", "ultimate neko"]))
.command("some long command", |c| c.cmd(some_long_command))
.group("Owner", |g| g
// This check applies to every command on this group.
// User needs to pass the test for the command to execute.
.check(admin_check)
.command("am i admin", |c| c
.cmd(am_i_admin))
.guild_only(true)
),
);
if let Err(why) = client.start() {
println!("Client error: {:?}", why);
}
}
// Commands can be created via the `command!` macro, to avoid manually typing
// type annotations.
//
// This may bring more features available for commands in the future. See the
// "multiply" command below for some of the power that the `command!` macro can
// bring.
command!(commands(ctx, msg, _args) {
let mut contents = "Commands used:\n".to_string();
let data = ctx.data.lock();
let counter = data.get::<CommandCounter>().unwrap();
for (k, v) in counter {
let _ = write!(contents, "- {name}: {amount}\n", name=k, amount=v);
}
if let Err(why) = msg.channel_id.say(&contents) {
println!("Error sending message: {:?}", why);
}
});
// 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.
fn owner_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> bool {
// Replace 7 with your ID
msg.author.id == 7
}
// 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.
fn admin_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> bool {
if let Some(member) = msg.member() {
if let Ok(permissions) = member.permissions() {
return permissions.administrator();
}
}
false
}
command!(some_long_command(_ctx, msg, args) {
if let Err(why) = msg.channel_id.say(&format!("Arguments: {:?}", args)) {
println!("Error sending message: {:?}", why);
}
});
command!(about_role(_ctx, msg, args) {
let potential_role_name = args.full();
if let Some(guild) = msg.guild() {
// `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(&format!("Role-ID: {}", role.id)) {
println!("Error sending message: {:?}", why);
}
return Ok(());
}
}
if let Err(why) = msg.channel_id.say(
&format!("Could not find role named: {:?}", potential_role_name)) {
println!("Error sending message: {:?}", why);
}
});
// Using the `command!` macro, commands can be created with a certain type of
// "dynamic" type checking. This is a method of requiring that the arguments
// given match the required type, and maps those arguments to the specified
// bindings.
//
// For example, the following will be correctly parsed by the macro:
//
// `~multiply 3.7 4.3`
//
// However, the following will not, as the second argument can not be an f64:
//
// `~multiply 3.7 four`
//
// Since the argument can't be converted, the command returns early.
//
// Additionally, if not enough arguments are given (e.g. `~multiply 3`), then
// the command will return early. If additional arguments are provided, they
// will be ignored.
//
// Argument type overloading is currently not supported.
command!(multiply(_ctx, msg, args) {
let first = args.single::<f64>().unwrap();
let second = args.single::<f64>().unwrap();
let res = first * second;
if let Err(why) = msg.channel_id.say(&res.to_string()) {
println!("Err sending product of {} and {}: {:?}", first, second, why);
}
});
command!(about(_ctx, msg, _args) {
if let Err(why) = msg.channel_id.say("This is a small test-bot! : )") {
println!("Error sending message: {:?}", why);
}
});
command!(latency(ctx, msg, _args) {
// The shard manager is an interface for mutating, stopping, restarting, and
// retrieving information about shards.
let data = ctx.data.lock();
let shard_manager = match data.get::<ShardManagerContainer>() {
Some(v) => v,
None => {
let _ = msg.reply("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("No shard found");
return Ok(());
},
};
let _ = msg.reply(&format!("The shard latency is {:?}", runner.latency));
});
command!(ping(_ctx, msg, _args) {
if let Err(why) = msg.channel_id.say("Pong! : )") {
println!("Error sending message: {:?}", why);
}
});
command!(am_i_admin(_ctx, msg, _args) {
if let Err(why) = msg.channel_id.say("Yes you are.") {
println!("Error sending message: {:?}", why);
}
});
command!(dog(_ctx, msg, _args) {
if let Err(why) = msg.channel_id.say(":dog:") {
println!("Error sending message: {:?}", why);
}
});
command!(cat(_ctx, msg, _args) {
if let Err(why) = msg.channel_id.say(":cat:") {
println!("Error sending message: {:?}", why);
}
});
|