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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#![feature(proc_macro, generators)]
extern crate futures_await as futures;
extern crate serenity;
extern crate tokio_core;
use futures::prelude::*;
use serenity::gateway::Shard;
use serenity::model::event::{Event, GatewayEvent};
use std::error::Error;
use std::env;
use std::rc::Rc;
use tokio_core::reactor::{Core, Handle};
fn main() {
let mut core = Core::new().expect("Error creating event loop");
let future = try_main(core.handle());
core.run(future).expect("Error running event loop");
}
#[async]
fn try_main(handle: Handle) -> Result<(), Box<Error + 'static>> {
// Configure the client with your Discord bot token in the environment.
let token = Rc::new(env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment"));
// Create a new shard, specifying the token, the ID of the shard (0 of 1),
// and a handle to the event loop
let mut shard = await!(Shard::new(token, [0, 1], handle))?;
// Loop over websocket messages.
#[async]
for message in shard.messages() {
// Parse the websocket message into a serenity GatewayEvent.
let event = shard.parse(message)?;
// Have the shard process the WebSocket event, in case it needs to
// mutate its state, send a packet, etc.
shard.process(&event);
// Now you can do whatever you want with the event.
match event {
GatewayEvent::Dispatch(_, Event::MessageCreate(ev)) => {
if ev.message.content == "!ping" {
println!("Pong!");
}
},
GatewayEvent::Dispatch(_, Event::Ready(_)) => {
println!("Connected to Discord!");
},
_ => {
// Ignore all other messages.
},
}
}
Ok(())
}
|