aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-09-19 09:00:03 -0700
committerAustin Hellyer <[email protected]>2016-10-18 11:14:27 -0700
commit8fc8c81403c3daa187ba96a7d488a64db21463bf (patch)
tree81bc4890c28b08ce806f69084617066bce863c2d /examples
downloadserenity-8fc8c81403c3daa187ba96a7d488a64db21463bf.tar.xz
serenity-8fc8c81403c3daa187ba96a7d488a64db21463bf.zip
Initial commit
Diffstat (limited to 'examples')
-rw-r--r--examples/01_basic_ping_bot.rs23
-rw-r--r--examples/02_transparent_guild_sharding.rs55
-rw-r--r--examples/03_struct_utilities.rs23
-rw-r--r--examples/04_message_builder.rs41
-rw-r--r--examples/05_user_login.rs17
-rw-r--r--examples/06_command_framework.rs62
6 files changed, 221 insertions, 0 deletions
diff --git a/examples/01_basic_ping_bot.rs b/examples/01_basic_ping_bot.rs
new file mode 100644
index 0000000..8db4614
--- /dev/null
+++ b/examples/01_basic_ping_bot.rs
@@ -0,0 +1,23 @@
+extern crate serenity;
+
+use serenity::Client;
+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_message(|context, message| {
+ if message.content == "!ping" {
+ let _ = context.say("Pong!");
+ }
+ });
+
+ client.on_ready(|_context, ready| {
+ println!("{} is connected!", ready.user.name);
+ });
+
+ let _ = client.start();
+}
diff --git a/examples/02_transparent_guild_sharding.rs b/examples/02_transparent_guild_sharding.rs
new file mode 100644
index 0000000..d257373
--- /dev/null
+++ b/examples/02_transparent_guild_sharding.rs
@@ -0,0 +1,55 @@
+extern crate serenity;
+
+use serenity::Client;
+use std::env;
+
+// Serenity implements transparent sharding in a way that you do not need to
+// manually handle separate processes or connections manually.
+//
+// Transparent sharding is useful for a shared state. Instead of having states
+// with duplicated data, a shared state means all your data can be easily
+// accessible across all shards.
+//
+// If your bot is on many guilds - or over the maximum of 2500 - then you
+// should/must use guild sharding.
+//
+// This is an example file showing how guild sharding works. For this to
+// properly be able to be seen in effect, your bot should be in at least 2
+// guilds.
+//
+// Taking a scenario of 2 guilds, try saying "!ping" in one guild. It should
+// print either "0" or "1" in the console. Saying "!ping" in the other guild,
+// it should state the other number in the console. This confirms that guild
+// sharding works.
+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_message(|context, message| {
+ if message.content == "!ping" {
+ {
+ let connection = context.connection.lock().unwrap();
+
+ if let Some(shard_info) = connection.shard_info() {
+ println!("Shard {}", shard_info[0]);
+ }
+ }
+
+ let _ = context.say("Pong!");
+ }
+ });
+
+ client.on_ready(|_context, ready| {
+ println!("{} is connected!", ready.user.name);
+ });
+
+ // The total number of shards to use. The "current shard number" of a
+ // connection - that is, the shard it is assigned to - is indexed at 0,
+ // while the total shard count is indexed at 1.
+ //
+ // This means if you have 5 shards, your total shard count will be 5, while
+ // each shard will be assigned numbers 0 through 4.
+ let _ = client.start_shards(2);
+}
diff --git a/examples/03_struct_utilities.rs b/examples/03_struct_utilities.rs
new file mode 100644
index 0000000..af00abb
--- /dev/null
+++ b/examples/03_struct_utilities.rs
@@ -0,0 +1,23 @@
+extern crate serenity;
+
+use serenity::Client;
+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_message(|_context, message| {
+ if message.content == "!messageme" {
+ let _ = message.author.dm("Hello!");
+ }
+ });
+
+ client.on_ready(|_context, ready| {
+ println!("{} is connected!", ready.user.name);
+ });
+
+ let _ = client.start();
+}
diff --git a/examples/04_message_builder.rs b/examples/04_message_builder.rs
new file mode 100644
index 0000000..41f390b
--- /dev/null
+++ b/examples/04_message_builder.rs
@@ -0,0 +1,41 @@
+extern crate serenity;
+
+use serenity::Client;
+use serenity::utils::MessageBuilder;
+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_message(|context, message| {
+ if message.content == "!ping" {
+ let channel = match context.get_channel(message.channel_id) {
+ Ok(channel) => channel,
+ Err(why) => {
+ println!("Error getting channel: {:?}", why);
+
+ return;
+ },
+ };
+
+ let response = MessageBuilder::new()
+ .push("User ")
+ .mention(message.author)
+ .push(" used the 'ping' command in the ")
+ .mention(channel)
+ .push(" channel")
+ .build();
+
+ let _ = context.say(&response);
+ }
+ });
+
+ client.on_ready(|_context, ready| {
+ println!("{} is connected!", ready.user.name);
+ });
+
+ let _ = client.start();
+}
diff --git a/examples/05_user_login.rs b/examples/05_user_login.rs
new file mode 100644
index 0000000..bbc9303
--- /dev/null
+++ b/examples/05_user_login.rs
@@ -0,0 +1,17 @@
+extern crate serenity;
+
+use serenity::Client;
+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_user(&token);
+
+ client.on_ready(|_context, ready| {
+ println!("{} is connected!", ready.user.name);
+ });
+
+ println!("{:?}", client.start());
+}
diff --git a/examples/06_command_framework.rs b/examples/06_command_framework.rs
new file mode 100644
index 0000000..5bf156c
--- /dev/null
+++ b/examples/06_command_framework.rs
@@ -0,0 +1,62 @@
+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_message(|_context, message| {
+ println!("Received message: {:?}", message);
+ });
+
+ client.on_ready(|_context, ready| {
+ println!("{} is connected!", ready.user.name);
+ });
+
+ // Commands are equivilant to:
+ // "~about"
+ // "~ping"
+ // "~emoji cat"
+ // "~emoji dog"
+ // "~some complex group"
+ client.with_framework(|f| f
+ .configure(|c| c
+ .on_mention(true)
+ .prefix("~"))
+ .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("some complex command", some_complex_command)
+ // Commands can be in closure-form as well
+ .on("about", |context, _message| drop(context.say("A test bot"))));
+
+ let _ = client.start();
+}
+
+fn cat_command(context: Context, _message: Message) {
+ let _ = context.say(":cat:");
+}
+
+fn dog_command(context: Context, _message: Message) {
+ let _ = context.say(":dog:");
+}
+
+fn ping_command(_context: Context, message: Message) {
+ let _ = message.reply("Pong!");
+}
+
+fn owner_check(_context: &Context, message: &Message) -> bool {
+ // Replace 7 with your ID
+ message.author.id.0 == 7u64
+}
+
+fn some_complex_command(context: Context, _message: Message) {
+ let _ = context.say("This is a command in a complex group");
+}