From 6a68f68e6cb95af38666a4f5d9a6ad4b39fa88c6 Mon Sep 17 00:00:00 2001 From: Lakelezz <12222135+Lakelezz@users.noreply.github.com> Date: Mon, 15 Oct 2018 20:48:49 +0200 Subject: Prefix only Command (#416) --- examples/05_command_framework/src/main.rs | 3 +++ 1 file changed, 3 insertions(+) (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 1f442c3..e69934c 100644 --- a/examples/05_command_framework/src/main.rs +++ b/examples/05_command_framework/src/main.rs @@ -81,6 +81,9 @@ fn main() { .allow_whitespace(true) .on_mention(true) .prefix("~") + // A command that will be executed + // if nothing but a prefix is passed. + .prefix_only_cmd(about) // You can set multiple delimiters via delimiters() // or just one via delimiter(",") // If you set multiple delimiters, the order you list them -- cgit v1.2.3 From 867a744720c46c0b04a2d34c2119ad366aa440ef Mon Sep 17 00:00:00 2001 From: Lakelezz <12222135+Lakelezz@users.noreply.github.com> Date: Tue, 30 Oct 2018 15:10:10 +0100 Subject: Add Function to neutralise Mentions (#414) --- examples/05_command_framework/src/main.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (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 e69934c..843fb43 100644 --- a/examples/05_command_framework/src/main.rs +++ b/examples/05_command_framework/src/main.rs @@ -20,6 +20,7 @@ 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; @@ -175,6 +176,10 @@ fn main() { // Make this command use the "complicated" bucket. .bucket("complicated") .cmd(commands)) + // Command that will repeat passed arguments and remove user and + // role mentions with safe alternative. + .command("say", |c| c + .cmd(say)) .group("Emoji", |g| g // Sets multiple prefixes for a group. // This requires us to call commands in this group @@ -250,6 +255,32 @@ command!(commands(ctx, msg, _args) { } }); +// 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!(say(_ctx, msg, args) { + let mut 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 mut content = content_safe(&args.full(), &settings); + + if let Err(why) = msg.channel_id.say(&content) { + 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 -- cgit v1.2.3 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