aboutsummaryrefslogtreecommitdiff
path: root/examples/04_message_builder/src/main.rs
blob: 41f390bdd49939816392e99dd073fe9a48403497 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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();
}