join_channel method. configure to drop incoming packets

This commit is contained in:
jellywx 2021-01-23 23:26:21 +00:00
parent 7466c3d75b
commit 619678de36

View File

@ -20,14 +20,19 @@ use serenity::{
model::{ model::{
channel::{Channel, Message}, channel::{Channel, Message},
guild::Guild, guild::Guild,
id::{GuildId, RoleId}, id::{ChannelId, GuildId, RoleId},
voice::VoiceState, voice::VoiceState,
}, },
prelude::*, prelude::*,
utils::shard_id, utils::shard_id,
}; };
use songbird::{Call, SerenityInit}; use songbird::{
create_player,
driver::{Config, CryptoMode, DecodeMode},
error::JoinResult,
Call, SerenityInit,
};
type CheckResult = Result<(), Reason>; type CheckResult = Result<(), Reason>;
@ -307,7 +312,7 @@ impl EventHandler for Handler {
.cloned() .cloned()
.expect("Could not get SQLPool from data"); .expect("Could not get SQLPool from data");
let guild_data_opt = GuildData::get_from_id(guild, pool.clone()).await; let guild_data_opt = GuildData::get_from_id(guild.clone(), pool.clone()).await;
if let Some(guild_data) = guild_data_opt { if let Some(guild_data) = guild_data_opt {
if guild_data.allow_greets { if guild_data.allow_greets {
@ -338,9 +343,7 @@ SELECT name, id, plays, public, server_id, uploader_id
.await .await
.unwrap(); .unwrap();
let voice_manager = songbird::get(&ctx).await.unwrap(); let (handler, _) = join_channel(&ctx, guild, user_channel).await;
let (handler, _) = voice_manager.join(guild_id, user_channel).await;
let _ = let _ =
play_audio(&mut sound, guild_data, &mut handler.lock().await, pool) play_audio(&mut sound, guild_data, &mut handler.lock().await, pool)
@ -356,12 +359,17 @@ SELECT name, id, plays, public, server_id, uploader_id
async fn play_audio( async fn play_audio(
sound: &mut Sound, sound: &mut Sound,
guild: GuildData, guild: GuildData,
handler: &mut MutexGuard<'_, Call>, call_handler: &mut MutexGuard<'_, Call>,
mysql_pool: MySqlPool, mysql_pool: MySqlPool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let audio = handler.play_source(sound.store_sound_source(mysql_pool.clone()).await?); {
let (track, track_handler) =
create_player(sound.store_sound_source(mysql_pool.clone()).await?);
let _ = audio.set_volume(guild.volume as f32 / 100.0); let _ = track_handler.set_volume(guild.volume as f32 / 100.0);
call_handler.play(track);
}
sound.plays += 1; sound.plays += 1;
sound.commit(mysql_pool).await?; sound.commit(mysql_pool).await?;
@ -369,6 +377,32 @@ async fn play_audio(
Ok(()) Ok(())
} }
async fn join_channel(
ctx: &Context,
guild: Guild,
channel_id: ChannelId,
) -> (Arc<Mutex<Call>>, JoinResult<()>) {
let songbird = songbird::get(ctx).await.unwrap();
let current_user = ctx.cache.current_user_id().await;
let current_voice_state = guild
.voice_states
.get(&current_user)
.and_then(|voice_state| voice_state.channel_id);
if current_voice_state == Some(channel_id) {
let call_opt = songbird.get(guild.id);
if let Some(call) = call_opt {
(call, Ok(()))
} else {
songbird.join(guild.id, channel_id).await
}
} else {
songbird.join(guild.id, channel_id).await
}
}
#[hook] #[hook]
async fn log_errors(_: &Context, m: &Message, cmd_name: &str, error: Result<(), CommandError>) { async fn log_errors(_: &Context, m: &Message, cmd_name: &str, error: Result<(), CommandError>) {
if let Err(e) = error { if let Err(e) = error {
@ -460,7 +494,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
) )
.framework(framework) .framework(framework)
.event_handler(Handler) .event_handler(Handler)
.register_songbird() .register_songbird_with({
let songbird = songbird::Songbird::serenity();
songbird.set_config(Config {
crypto_mode: CryptoMode::Normal,
decode_mode: DecodeMode::Pass,
preallocated_tracks: 0,
});
songbird
})
.await .await
.expect("Error occurred creating client"); .expect("Error occurred creating client");
@ -525,13 +569,16 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
match sound_res { match sound_res {
Some(sound) => { Some(sound) => {
let voice_manager = songbird::get(ctx).await.unwrap(); {
let (call_handler, _) =
join_channel(ctx, guild.clone(), user_channel).await;
let (call_handler, _) = voice_manager.join(guild_id, user_channel).await; let guild_data = GuildData::get_from_id(guild, pool.clone()).await.unwrap();
let guild_data = GuildData::get_from_id(guild, pool.clone()).await.unwrap(); let mut lock = call_handler.lock().await;
play_audio(sound, guild_data, &mut call_handler.lock().await, pool).await?; play_audio(sound, guild_data, &mut lock, pool).await?;
}
msg.channel_id msg.channel_id
.say( .say(