[][src]Struct serenity::framework::standard::StandardFramework

pub struct StandardFramework {
    pub initialized: bool,
    // some fields omitted
}

A utility for easily managing dispatches to commands.

Refer to the module-level documentation for more information.

Fields

Whether the framework has been "initialized".

The framework is initialized once one of the following occurs:

  • configuration has been set;
  • a command handler has been set;
  • a command check has been set.

This is used internally to determine whether or not - in addition to dispatching to the EventHandler::message handler - to have the framework check if a Event::MessageCreate should be processed by itself.

Methods

impl StandardFramework
[src]

Configures the framework, setting non-default values. All fields are optional. Refer to Configuration::default for more information on the default values.

Examples

Configuring the framework for a Client, setting the depth to 3, allowing whitespace, and setting the prefix to "~":

use serenity::Client;
use serenity::framework::StandardFramework;
use std::env;

let token = env::var("DISCORD_TOKEN").unwrap();
let mut client = Client::new(&token, Handler).unwrap();
client.with_framework(StandardFramework::new()
    .configure(|c| c
        .depth(3)
        .allow_whitespace(true)
        .prefix("~")));

Defines a bucket with delay between each command, and the limit of uses per time_span.

Examples

Create and use a bucket that limits a command to 3 uses per 10 seconds with a 2 second delay inbetween invocations:

use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new()
    .bucket("basic", 2, 10, 3)
    .command("ping", |c| c
        .bucket("basic")
        .exec(|_, msg, _| {
            msg.channel_id.say("pong!")?;

            Ok(())
        })));

Same as bucket but with a check added.

Examples

use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new()
    .complex_bucket("basic", 2, 10, 3, |_, guild_id, channel_id, user_id| {
        // Our bucket is very strict. It cannot apply in DMs.
        // And can only apply if it's in the specific guild, channel and by the specific user.
        guild_id.is_some() && guild_id.unwrap() == 123 && channel_id == 456
        && user_id == 789
    })
    .command("ping", |c| c
        .bucket("basic")
        .exec(|_, msg, _| {
            msg.channel_id.say("pong!")?;

            Ok(())
        })
    )
);

Defines a bucket with only a delay between each command.

Examples

Create and use a simple bucket that has a 2 second delay between invocations:

use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new()
    .simple_bucket("simple", 2)
    .command("ping", |c| c
        .bucket("simple")
        .exec(|_, msg, _| { msg.channel_id.say("pong!")?; Ok(()) })));

Adds a function to be associated with a command, which will be called when a command is used in a message.

This requires that a check - if one exists - passes, prior to being called.

Prior to v0.2.0, you will need to use the command builder via the command method to set checks. This command will otherwise only be for simple commands.

Refer to the module-level documentation for more information and usage.

Examples

Create and use a simple command:

use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new().on("ping", |_, msg, _| {
    msg.channel_id.say("pong!")?;

    Ok(())
}));

Same as on, but accepts a Command directly.

Adds a command using command builder.

Examples

This example is not tested
framework.command("ping", |c| c
    .desc("Responds with 'pong'.")
    .exec(|ctx, _, _| {
        let _ = ctx.say("pong");
    }));

Adds a group which can organize several related commands. Groups are taken into account when using serenity::framework::standard::help_commands.

Examples

Creating a simple group:

use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new()
    .group("ping-pong", |g| g
        .on("ping", |_, msg, _| { msg.channel_id.say("pong!")?; Ok(()) })
        .on("pong", |_, msg, _| { msg.channel_id.say("ping!")?; Ok(()) })));

Specify the function that's called in case a command wasn't executed for one reason or another.

DispatchError represents all possible fail conditions.

Examples

Making a simple argument error responder:

use serenity::framework::standard::DispatchError::{NotEnoughArguments,
TooManyArguments};
use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new()
    .on_dispatch_error(|_, msg, error| {
        match error {
            NotEnoughArguments { min, given } => {
                let s = format!("Need {} arguments, but only got {}.", min, given);

                let _ = msg.channel_id.say(&s);
            },
            TooManyArguments { max, given } => {
                let s = format!("Max arguments allowed is {}, but got {}.", max, given);

                let _ = msg.channel_id.say(&s);
            },
            _ => println!("Unhandled dispatch error."),
        }
    }));

Specify the function to be called prior to every command's execution. If that function returns true, the command will be executed.

Examples

Using before to log command usage:

use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new()
    .before(|ctx, msg, cmd_name| {
        println!("Running command {}", cmd_name);
        true
    }));

Using before to prevent command usage:

use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new()
    .before(|_, msg, cmd_name| {
        if let Ok(channel) = msg.channel_id.to_channel() {
            //  Don't run unless in nsfw channel
            if !channel.is_nsfw() {
                return false;
            }
        }

        println!("Running command {}", cmd_name);

        true
    }));

Specify the function to be called after every command's execution. Fourth argument exists if command returned an error which you can handle.

Examples

Using after to log command usage:

use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new()
    .after(|ctx, msg, cmd_name, error| {
        //  Print out an error if it happened
        if let Err(why) = error {
            println!("Error in {}: {:?}", cmd_name, why);
        }
    }));

Specify the function to be called if no command could be dispatched.

Examples

Using unrecognised_command:

use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new()
    .unrecognised_command(|ctx, msg, unrecognised_command_name| { }));

Specify the function to be called if a message contains no command.

Examples

Using message_without_command:

use serenity::framework::StandardFramework;

client.with_framework(StandardFramework::new()
    .message_without_command(|ctx, msg| { }));

Sets what code should be executed when a user sends (prefix)help.

If a command named help was set with command, then this takes precedence first.

Sets what code should be executed when sends (prefix)help. Additionally takes a closure with a CreateHelpCommand in order to alter help-commands.

Trait Implementations

impl Framework for StandardFramework
[src]

impl Default for StandardFramework
[src]

Returns the "default value" for a type. Read more

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

Performs the conversion.

impl<T> From for T
[src]

Performs the conversion.

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Borrow for T where
    T: ?Sized
[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut for T where
    T: ?Sized
[src]

Mutably borrows from an owned value. Read more

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more

impl<T> Erased for T

impl<T> Typeable for T where
    T: Any

Get the TypeId of this object.

impl<T> UnsafeAny for T where
    T: Any