From d6c4beeaf89940731c3f2fff14199414dc478342 Mon Sep 17 00:00:00 2001 From: Lakelezz <12222135+Lakelezz@users.noreply.github.com> Date: Mon, 12 Nov 2018 18:52:56 +0100 Subject: Update examples' unwraps to expects and imports as nested (#435) --- examples/05_command_framework/src/main.rs | 36 +++++++++++++++++-------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'examples/05_command_framework/src') 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::().unwrap(); + let counter = data.get_mut::().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::().unwrap(); + let counter = data.get::().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::().unwrap(); - let second = args.single::().unwrap(); + let first = args.single::()?; + let second = args.single::()?; let res = first * second; -- cgit v1.2.3