aboutsummaryrefslogtreecommitdiff
path: root/examples/06_command_framework
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-11-29 20:51:10 -0800
committerAustin Hellyer <[email protected]>2016-11-29 22:27:59 -0800
commit93b990d8d1bc9df69b8e27a3db61da570822aad6 (patch)
tree6305cf635df90681527a8e736f65ff19f21fd8bc /examples/06_command_framework
parentAdd more shiny readme badges (diff)
downloadserenity-93b990d8d1bc9df69b8e27a3db61da570822aad6.tar.xz
serenity-93b990d8d1bc9df69b8e27a3db61da570822aad6.zip
Clean up the codebase
Diffstat (limited to 'examples/06_command_framework')
-rw-r--r--examples/06_command_framework/Cargo.toml7
-rw-r--r--examples/06_command_framework/src/main.rs88
2 files changed, 95 insertions, 0 deletions
diff --git a/examples/06_command_framework/Cargo.toml b/examples/06_command_framework/Cargo.toml
new file mode 100644
index 0000000..fa93475
--- /dev/null
+++ b/examples/06_command_framework/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "06_command_framework"
+version = "0.1.0"
+authors = ["my name <[email protected]>"]
+
+[dependencies]
+serenity = { git = "https://github.com/zeyla/serenity.rs.git" }
diff --git a/examples/06_command_framework/src/main.rs b/examples/06_command_framework/src/main.rs
new file mode 100644
index 0000000..95d832c
--- /dev/null
+++ b/examples/06_command_framework/src/main.rs
@@ -0,0 +1,88 @@
+//! Requires the 'methods' feature flag be enabled in your project's Cargo.toml.
+//!
+//! This can be activated by specifying the feature in the dependency section:
+//!
+//! ```toml
+//! [dependencies.serenity]
+//! git = "https://github.com/zeyla/serenity.rs.git"
+//! features = ["framework", methods"]
+//! ```
+
+#[macro_use]
+extern crate serenity;
+
+use serenity::client::Context;
+use serenity::Client;
+use serenity::model::Message;
+use std::env;
+
+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::login_bot(&token);
+
+ client.on_ready(|_context, ready| {
+ println!("{} is connected!", ready.user.name);
+ });
+
+ // Commands are equivilant to:
+ // "~about"
+ // "~emoji cat"
+ // "~emoji dog"
+ // "~ping"
+ // "~some complex command"
+ client.with_framework(|f| f
+ .configure(|c| c
+ .allow_whitespace(true)
+ .on_mention(true)
+ .prefix("~"))
+ .before(|_context, message, command_name| {
+ println!("Got command '{}' by user '{}'",
+ command_name,
+ message.author.name);
+ })
+ .after(|_context, _message, command_name| {
+ println!("Processed command '{}'", command_name)
+ })
+ .on("ping", ping_command)
+ .set_check("ping", owner_check) // Ensure only the owner can run this
+ .on("emoji cat", cat_command)
+ .on("emoji dog", dog_command)
+ .on("multiply", multiply)
+ .on("some complex command", some_complex_command)
+ // Commands can be in closure-form as well
+ .on("about", |context, _message, _args| drop(context.say("A test bot"))));
+
+ let _ = client.start();
+}
+
+command!(cat_command(context, _msg, _arg) {
+ let _ = context.say(":cat:");
+});
+
+fn dog_command(context: &Context, _msg: &Message, _args: Vec<String>) {
+ let _ = context.say(":dog:");
+}
+
+// `Message::reply` is only compiled if the `methods` feature flag is enabled.
+fn ping_command(_context: &Context, message: &Message, _args: Vec<String>) {
+ let _ = message.reply("Pong!");
+}
+
+fn owner_check(_context: &Context, message: &Message) -> bool {
+ // Replace 7 with your ID
+ message.author.id == 7
+}
+
+fn some_complex_command(context: &Context, _msg: &Message, args: Vec<String>) {
+ let _ = context.say(&format!("Arguments: {:?}", args));
+}
+
+command!(multiply(context, _message, args, first: f64, second: f64) {
+ let res = first * second;
+
+ let _ = context.say(&res.to_string());
+
+ println!("{:?}", args);
+});