forked from serenity-rs/serenity
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create message builder example (serenity-rs#268)
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "11_create_message_builder" | ||
version = "0.1.0" | ||
authors = ["my name <[email protected]>"] | ||
|
||
[dependencies] | ||
serenity = { path = "../../" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
extern crate serenity; | ||
|
||
use serenity::model::channel::Message; | ||
use serenity::model::gateway::Ready; | ||
use serenity::prelude::*; | ||
use std::env; | ||
|
||
struct Handler; | ||
|
||
impl EventHandler for Handler { | ||
fn message(&self, _: Context, msg: Message) { | ||
if msg.content == "!hello" { | ||
// The create message builder allows you to easily create embeds and messages | ||
// using a builder syntax. | ||
// This example will create a message that says "Hello, World!", with an embed that has | ||
// a title, description, and footer. | ||
if let Err(why) = msg.channel_id.send_message(|m| m | ||
.content("Hello, World!") | ||
.embed(|e| e | ||
.title("This is a title") | ||
.description("This is a description") | ||
.footer(|f| f | ||
.text("This is a footer")))) { | ||
println!("Error sending message: {:?}", why); | ||
} | ||
} | ||
} | ||
|
||
fn ready(&self, _: Context, ready: Ready) { | ||
println!("{} is connected!", ready.user.name); | ||
} | ||
} | ||
|
||
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::new(&token, Handler).expect("Err creating client"); | ||
|
||
if let Err(why) = client.start() { | ||
println!("Client error: {:?}", why); | ||
} | ||
} |