aboutsummaryrefslogtreecommitdiff
path: root/examples/06_voice/src
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-04-05 08:14:43 -0700
committerZeyla Hellyer <[email protected]>2017-04-05 08:55:01 -0700
commitd9118c081742d6654dc0a4f60228a7a212ca436e (patch)
tree003f49f54769314c1111e942d77f57513406fb5e /examples/06_voice/src
parentAdd a sample bot structure example (diff)
downloadserenity-d9118c081742d6654dc0a4f60228a7a212ca436e.tar.xz
serenity-d9118c081742d6654dc0a4f60228a7a212ca436e.zip
Remove selfbot support
While selfbots have always been "roughly tolerated", lately they have been tolerated to less of a degree. The simple answer is to no longer support selfbots in any form. This is done for a few of reasons: 1) in anticipation of selfbots no longer being tolerated; 2) there are few reasons why one should make a selfbot in Rust and not a scripting language; 3) there are alternatives (i.e. discord-rs) that still support userbots. Selfbots are simply not a goal of the maintainer of serenity. Upgrade path: Don't use selfbots with serenity. Use discord-rs instead. The following has been removed: Enums: - `RelationshipType` Structs: - `FriendSourceFlags` - `ReadState` - `Relationship` - `SearchResult` - `SuggestionReason` - `Tutorial` - `UserConnection` - `UserGuildSettings` - `UserSettings` Removed the following fields: - `CurrentUser::mobile` - Ready::{ analytics_token, experiments, friend_suggestion_count, notes, read_state, relationships, tutorial, user_guild_settings, user_settings, } Removed the following methods: - `Client::login_user` Deprecated `Client::login_bot` in favour of `Client::login`. Removed `client::LoginType`. The following no longer take a `login_type` parameter: - `Context::new` - `Shard::new` `Shard::sync_guilds` has been removed. The `client::Error::{InvalidOperationAsBot, InvalidOperationAsUser}` variants have been removed. The following event handlers on `Client` have been removed: - `on_friend_suggestion_create` - `on_friend_suggestion_delete` - `on_relationship_add` - `on_relationship_remove` - `on_user_guild_settings_update` - `on_note_update` - `on_user_settings_update` The following `client::rest` functions have been removed: - `ack_message` - `edit_note` - `get_user_connections` - `search_channel_messages` - `search_guild_messages` The following `client::rest::ratelimiting::Route` variants have been removed: - `ChannelsIdMessagesSearch` - `GuildsIdMessagesSearch` - `UsersMeConnections` The following fields on `ext::cache::Cache` have been removed: - `guild_settings` - `relationships` - `settings` while the following methods have also been removed: - `update_with_relationship_add` - `update_with_relationship_remove` - `update_with_user_guild_settings_update` - `update_with_user_note_update` - `update_with_user_settings_update` The following methods have been removed across models: - `ChannelId::{ack, search}` - `Channel::{ack, search}` - `Group::{ack, search}` - `GuildChannel::{ack, search}` - `GuildId::{search, search_channels}` - `Guild::{search, search_channels}` - `Message::ack` - `PartialGuild::{search, search_channels}` - `PrivateChannel::{ack, search}` - `UserId::{delete_note, edit_note}` - `User::{delete_note, edit_note}` The following events in `model::events` have been removed: - `FriendSuggestionCreateEvent` - `FriendSuggestionDeleteEvent` - `MessageAckEvent` - `RelationshipAddEvent` - `RelationshipRemoveEvent` - `UserGuildSettingsUpdateEvent` - `UserNoteUpdateEvent` - `UserSettingsUpdateEvent` Consequently, the following variants on `model::event::Event` have been removed: - `FriendSuggestionCreate` - `FriendSuggestionDelete` - `MessageAdd` - `RelationshipAdd` - `RelationshipRemove` - `UserGuildSettingUpdate` - `UserNoteUpdate` - `UserSettingsUpdate` The `utils::builder::Search` search builder has been removed.
Diffstat (limited to 'examples/06_voice/src')
-rw-r--r--examples/06_voice/src/main.rs251
1 files changed, 251 insertions, 0 deletions
diff --git a/examples/06_voice/src/main.rs b/examples/06_voice/src/main.rs
new file mode 100644
index 0000000..979ea1f
--- /dev/null
+++ b/examples/06_voice/src/main.rs
@@ -0,0 +1,251 @@
+//! Requires the "cache", "methods", and "voice" features be enabled in your
+//! Cargo.toml, like so:
+//!
+//! ```toml
+//! [dependencies.serenity]
+//! git = "https://github.com/zeyla/serenity.git"
+//! features = ["cache", "framework", "voice"]
+//! ```
+
+#[macro_use]
+extern crate serenity;
+
+use serenity::client::{CACHE, Client};
+use serenity::ext::voice;
+use serenity::model::{ChannelId, Message, Mentionable};
+use serenity::Result as SerenityResult;
+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(&token);
+
+ client.with_framework(|f| f
+ .configure(|c| c
+ .prefix("~")
+ .on_mention(true))
+ .on("deafen", deafen)
+ .on("join", join)
+ .on("leave", leave)
+ .on("mute", mute)
+ .on("play", play)
+ .on("ping", ping)
+ .on("undeafen", undeafen)
+ .on("unmute", unmute));
+
+ client.on_ready(|_context, ready| {
+ println!("{} is connected!", ready.user.name);
+ });
+
+ let _ = client.start().map_err(|why| println!("Client ended: {:?}", why));
+}
+
+command!(deafen(ctx, msg) {
+ let guild_id = match CACHE.read().unwrap().get_guild_channel(msg.channel_id) {
+ Some(channel) => channel.read().unwrap().guild_id,
+ None => {
+ check_msg(msg.channel_id.say("Groups and DMs not supported"));
+
+ return Ok(());
+ },
+ };
+
+ let mut shard = ctx.shard.lock().unwrap();
+
+ let handler = match shard.manager.get(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, args) {
+ let connect_to = match args.get(0) {
+ Some(arg) => match arg.parse::<u64>() {
+ Ok(id) => ChannelId(id),
+ Err(_why) => {
+ check_msg(msg.reply("Invalid voice channel ID given"));
+
+ return Ok(());
+ },
+ },
+ None => {
+ check_msg(msg.reply("Requires a voice channel ID be given"));
+
+ return Ok(());
+ },
+ };
+
+ let guild_id = match CACHE.read().unwrap().get_guild_channel(msg.channel_id) {
+ Some(channel) => channel.read().unwrap().guild_id,
+ None => {
+ check_msg(msg.channel_id.say("Groups and DMs not supported"));
+
+ return Ok(());
+ },
+ };
+
+ let mut shard = ctx.shard.lock().unwrap();
+ shard.manager.join(Some(guild_id), connect_to);
+
+ check_msg(msg.channel_id.say(&format!("Joined {}", connect_to.mention())));
+});
+
+command!(leave(ctx, msg) {
+ let guild_id = match CACHE.read().unwrap().get_guild_channel(msg.channel_id) {
+ Some(channel) => channel.read().unwrap().guild_id,
+ None => {
+ check_msg(msg.channel_id.say("Groups and DMs not supported"));
+
+ return Ok(());
+ },
+ };
+
+ let mut shard = ctx.shard.lock().unwrap();
+ let has_handler = shard.manager.get(guild_id).is_some();
+
+ if has_handler {
+ shard.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().unwrap().get_guild_channel(msg.channel_id) {
+ Some(channel) => channel.read().unwrap().guild_id,
+ None => {
+ check_msg(msg.channel_id.say("Groups and DMs not supported"));
+
+ return Ok(());
+ },
+ };
+
+ let mut shard = ctx.shard.lock().unwrap();
+
+ let handler = match shard.manager.get(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.get(0) {
+ Some(url) => url,
+ None => {
+ 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().unwrap().get_guild_channel(msg.channel_id) {
+ Some(channel) => channel.read().unwrap().guild_id,
+ None => {
+ check_msg(msg.channel_id.say("Error finding channel info"));
+
+ return Ok(());
+ },
+ };
+
+ if let Some(handler) = ctx.shard.lock().unwrap().manager.get(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().unwrap().get_guild_channel(msg.channel_id) {
+ Some(channel) => channel.read().unwrap().guild_id,
+ None => {
+ check_msg(msg.channel_id.say("Error finding channel info"));
+
+ return Ok(());
+ },
+ };
+
+ if let Some(handler) = ctx.shard.lock().unwrap().manager.get(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().unwrap().get_guild_channel(msg.channel_id) {
+ Some(channel) => channel.read().unwrap().guild_id,
+ None => {
+ check_msg(msg.channel_id.say("Error finding channel info"));
+
+ return Ok(());
+ },
+ };
+
+ if let Some(handler) = ctx.shard.lock().unwrap().manager.get(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);
+ }
+}