aboutsummaryrefslogtreecommitdiff
path: root/examples/05_command_framework/src
diff options
context:
space:
mode:
authorLakelezz <[email protected]>2018-11-12 18:52:56 +0100
committerAlex M. M <[email protected]>2018-11-12 18:52:56 +0100
commitd6c4beeaf89940731c3f2fff14199414dc478342 (patch)
tree91f3a1fca396d55e80995c87eb2233f34f1b2f2d /examples/05_command_framework/src
parentAdd Link to the `Voice on Windows`-Wiki-Entry (#434) (diff)
downloadserenity-d6c4beeaf89940731c3f2fff14199414dc478342.tar.xz
serenity-d6c4beeaf89940731c3f2fff14199414dc478342.zip
Update examples' unwraps to expects and imports as nested (#435)
Diffstat (limited to 'examples/05_command_framework/src')
-rw-r--r--examples/05_command_framework/src/main.rs36
1 files changed, 20 insertions, 16 deletions
diff --git a/examples/05_command_framework/src/main.rs b/examples/05_command_framework/src/main.rs
index 843fb43..3fec94b 100644
--- a/examples/05_command_framework/src/main.rs
+++ b/examples/05_command_framework/src/main.rs
@@ -13,18 +13,18 @@
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 serenity::utils::{content_safe, ContentSafeOptions};
-use std::collections::HashMap;
-use std::env;
-use std::fmt::Write;
-use std::sync::Arc;
+use std::{collections::HashMap, env, fmt::Write, sync::Arc};
+
+use serenity::{
+ client::bridge::gateway::{ShardId, ShardManager},
+ framework::standard::{
+ help_commands, Args, CommandOptions, DispatchError, HelpBehaviour, StandardFramework,
+ },
+ model::{channel::Message, gateway::Ready, Permissions},
+ prelude::*,
+ utils::{content_safe, ContentSafeOptions},
+};
+
use typemap::Key;
// A container type is created for inserting into the Client's `data`, which
@@ -111,7 +111,7 @@ fn main() {
// 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 counter = data.get_mut::<CommandCounter>().expect("Expected CommandCounter in ShareMap.");
let entry = counter.entry(command_name.to_string()).or_insert(0);
*entry += 1;
@@ -130,6 +130,10 @@ fn main() {
.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.
+ .message_without_command(|_, 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.
@@ -244,7 +248,7 @@ command!(commands(ctx, msg, _args) {
let mut contents = "Commands used:\n".to_string();
let data = ctx.data.lock();
- let counter = data.get::<CommandCounter>().unwrap();
+ 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);
@@ -354,8 +358,8 @@ command!(about_role(_ctx, msg, args) {
//
// 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 first = args.single::<f64>()?;
+ let second = args.single::<f64>()?;
let res = first * second;