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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
//! Requires the "cache", "methods", and "voice" features be enabled in your
//! Cargo.toml, like so:
//!
//! ```toml
//! [dependencies.serenity]
//! git = "https://github.com/serenity-rs/serenity.git"
//! features = ["cache", "framework", "standard_framework", "voice"]
//! ```
#[macro_use] extern crate serenity;
extern crate typemap;
use std::{env, sync::Arc};
// Import the client's bridge to the voice manager. Since voice is a standalone
// feature, it's not as ergonomic to work with as it could be. The client
// provides a clean bridged integration with voice.
use serenity::client::bridge::voice::ClientVoiceManager;
// Import the `Context` from the client and `parking_lot`'s `Mutex`.
//
// `parking_lot` offers much more efficient implementations of `std::sync`'s
// types. You can read more about it here:
//
// <https://github.com/Amanieu/parking_lot#features>
use serenity::{client::{Context}, prelude::Mutex};
use serenity::{
client::{CACHE, Client, EventHandler},
framework::StandardFramework,
model::{channel::Message, gateway::Ready, misc::Mentionable},
Result as SerenityResult,
voice,
};
use typemap::Key;
struct VoiceManager;
impl Key for VoiceManager {
type Value = Arc<Mutex<ClientVoiceManager>>;
}
struct Handler;
impl EventHandler for Handler {
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");
// Obtain a lock to the data owned by the client, and insert the client's
// voice manager into it. This allows the voice manager to be accessible by
// event handlers and framework commands.
{
let mut data = client.data.lock();
data.insert::<VoiceManager>(Arc::clone(&client.voice_manager));
}
client.with_framework(StandardFramework::new()
.configure(|c| c
.prefix("~")
.on_mention(true))
.cmd("deafen", deafen)
.cmd("join", join)
.cmd("leave", leave)
.cmd("mute", mute)
.cmd("play", play)
.cmd("ping", ping)
.cmd("undeafen", undeafen)
.cmd("unmute", unmute));
let _ = client.start().map_err(|why| println!("Client ended: {:?}", why));
}
command!(deafen(ctx, msg) {
let guild_id = match CACHE.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
check_msg(msg.channel_id.say("Groups and DMs not supported"));
return Ok(());
},
};
let mut manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
let mut manager = manager_lock.lock();
let handler = match manager.get_mut(guild_id) {
Some(handler) => handler,
None => {
check_msg(msg.reply("Not in a voice channel"));
return Ok(());
},
};
if handler.self_deaf {
check_msg(msg.channel_id.say("Already deafened"));
} else {
handler.deafen(true);
check_msg(msg.channel_id.say("Deafened"));
}
});
command!(join(ctx, msg) {
let guild = match msg.guild() {
Some(guild) => guild,
None => {
check_msg(msg.channel_id.say("Groups and DMs not supported"));
return Ok(());
}
};
let guild_id = guild.read().id;
let channel_id = guild
.read()
.voice_states.get(&msg.author.id)
.and_then(|voice_state| voice_state.channel_id);
let connect_to = match channel_id {
Some(channel) => channel,
None => {
check_msg(msg.reply("Not in a voice channel"));
return Ok(());
}
};
let mut manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().expect("Expected VoiceManager in ShareMap.");
let mut manager = manager_lock.lock();
if manager.join(guild_id, connect_to).is_some() {
check_msg(msg.channel_id.say(&format!("Joined {}", connect_to.mention())));
} else {
check_msg(msg.channel_id.say("Error joining the channel"));
}
});
command!(leave(ctx, msg) {
let guild_id = match CACHE.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
check_msg(msg.channel_id.say("Groups and DMs not supported"));
return Ok(());
},
};
let mut manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().expect("Expected VoiceManager in ShareMap.");
let mut manager = manager_lock.lock();
let has_handler = manager.get(guild_id).is_some();
if has_handler {
manager.remove(guild_id);
check_msg(msg.channel_id.say("Left voice channel"));
} else {
check_msg(msg.reply("Not in a voice channel"));
}
});
command!(mute(ctx, msg) {
let guild_id = match CACHE.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
check_msg(msg.channel_id.say("Groups and DMs not supported"));
return Ok(());
},
};
let mut manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().expect("Expected VoiceManager in ShareMap.");
let mut manager = manager_lock.lock();
let handler = match manager.get_mut(guild_id) {
Some(handler) => handler,
None => {
check_msg(msg.reply("Not in a voice channel"));
return Ok(());
},
};
if handler.self_mute {
check_msg(msg.channel_id.say("Already muted"));
} else {
handler.mute(true);
check_msg(msg.channel_id.say("Now muted"));
}
});
command!(ping(_context, msg) {
check_msg(msg.channel_id.say("Pong!"));
});
command!(play(ctx, msg, args) {
let url = match args.single::<String>() {
Ok(url) => url,
Err(_) => {
check_msg(msg.channel_id.say("Must provide a URL to a video or audio"));
return Ok(());
},
};
if !url.starts_with("http") {
check_msg(msg.channel_id.say("Must provide a valid URL"));
return Ok(());
}
let guild_id = match CACHE.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
check_msg(msg.channel_id.say("Error finding channel info"));
return Ok(());
},
};
let mut manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().expect("Expected VoiceManager in ShareMap.");
let mut manager = manager_lock.lock();
if let Some(handler) = manager.get_mut(guild_id) {
let source = match voice::ytdl(&url) {
Ok(source) => source,
Err(why) => {
println!("Err starting source: {:?}", why);
check_msg(msg.channel_id.say("Error sourcing ffmpeg"));
return Ok(());
},
};
handler.play(source);
check_msg(msg.channel_id.say("Playing song"));
} else {
check_msg(msg.channel_id.say("Not in a voice channel to play in"));
}
});
command!(undeafen(ctx, msg) {
let guild_id = match CACHE.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
check_msg(msg.channel_id.say("Error finding channel info"));
return Ok(());
},
};
let mut manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().expect("Expected VoiceManager in ShareMap.");
let mut manager = manager_lock.lock();
if let Some(handler) = manager.get_mut(guild_id) {
handler.deafen(false);
check_msg(msg.channel_id.say("Undeafened"));
} else {
check_msg(msg.channel_id.say("Not in a voice channel to undeafen in"));
}
});
command!(unmute(ctx, msg) {
let guild_id = match CACHE.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
check_msg(msg.channel_id.say("Error finding channel info"));
return Ok(());
},
};
let mut manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().expect("Expected VoiceManager in ShareMap.");
let mut manager = manager_lock.lock();
if let Some(handler) = manager.get_mut(guild_id) {
handler.mute(false);
check_msg(msg.channel_id.say("Unmuted"));
} else {
check_msg(msg.channel_id.say("Not in a voice channel to undeafen in"));
}
});
/// Checks that a message successfully sent; if not, then logs why to stdout.
fn check_msg(result: SerenityResult<Message>) {
if let Err(why) = result {
println!("Error sending message: {:?}", why);
}
}
|