RMusicBot/src/player/player.rs

86 lines
2.3 KiB
Rust
Raw Normal View History

2019-09-14 14:34:00 +00:00
use parking_lot::lock_api::{MutexGuard, RawMutex};
2018-11-14 14:29:58 +00:00
use serenity;
2019-07-12 12:41:51 +00:00
use serenity::model::channel::Message;
use serenity::model::guild::Guild;
use serenity::model::id::GuildId;
use serenity::prelude::RwLock as SerRwLock;
2018-11-14 14:29:58 +00:00
use serenity::voice::Handler;
2019-07-12 12:41:51 +00:00
use std::sync::Arc;
2019-09-14 14:34:00 +00:00
use utilities::prelude::*;
2019-07-12 12:41:51 +00:00
// This imports `typemap`'s `Key` as `TypeMapKey`.
use serenity::prelude::*;
2018-11-23 12:18:07 +00:00
use super::prelude::*;
2019-07-12 12:41:51 +00:00
pub fn guild(ctx: &Context, msg: &Message) -> Result<Arc<SerRwLock<Guild>>, String> {
match msg.guild(&ctx.cache) {
Some(guild) => Ok(guild),
2018-11-14 14:29:58 +00:00
None => {
2019-07-12 12:41:51 +00:00
if msg
.channel_id
.say(&ctx.http, "Groups and DMs not supported")
.is_err()
{}
Err("failed getting Guild".to_string())
2018-11-14 14:29:58 +00:00
}
}
}
2019-07-12 12:41:51 +00:00
pub fn guild_id(ctx: &Context, msg: &Message) -> Result<GuildId, String> {
let guild = guild(ctx, msg)?;
let guild_read = guild.read();
Ok(guild_read.id)
}
pub fn handler<'a, T: RawMutex>(
2018-11-14 14:29:58 +00:00
guild_id: GuildId,
2019-07-12 12:41:51 +00:00
manager: &'a mut MutexGuard<'_, T, serenity::client::bridge::voice::ClientVoiceManager>,
2018-11-14 14:29:58 +00:00
) -> Option<&'a mut Handler> {
manager.get_mut(guild_id)
}
2018-11-23 12:18:07 +00:00
pub fn channel_contains_author(
ctx: &mut serenity::client::Context,
msg: &serenity::model::channel::Message,
2019-09-14 14:34:00 +00:00
) -> VerboseResult<()> {
let guild = guild(ctx, msg)?;
let guild_id = guild.read().id;
let author_channel_id = match guild
.read()
.voice_states
.get(&msg.author.id)
.and_then(|voice_state| voice_state.channel_id)
{
Some(channel) => channel,
None => create_error!("author is not in a voice channel!"),
};
2019-09-14 14:34:00 +00:00
if let Some(media) = ctx.data.read().get::<Media>() {
let mut manager = media.voice_manager.lock();
2019-09-14 14:34:00 +00:00
match manager.get(guild_id) {
Some(handler) => match handler.channel_id {
Some(bot_channel_id) => {
if bot_channel_id != author_channel_id {
create_error!("author is not in the same voice channel as the bot!");
}
}
None => {
manager.join(guild_id, author_channel_id);
}
},
None => {
manager.join(guild_id, author_channel_id);
}
2018-11-23 12:18:07 +00:00
}
}
2019-09-14 14:34:00 +00:00
Ok(())
2018-11-23 12:18:07 +00:00
}