|
|
|
|
@@ -1,13 +1,14 @@
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::{sync::Arc, collections::HashMap};
|
|
|
|
|
|
|
|
|
|
use serenity::{async_trait,
|
|
|
|
|
model::prelude::{ChannelId, Guild},
|
|
|
|
|
prelude::{Context, Mutex}};
|
|
|
|
|
model::{prelude::{ChannelId, Guild}, user::User},
|
|
|
|
|
prelude::{Context, Mutex, RwLock, TypeMapKey}, Client};
|
|
|
|
|
use songbird::{EventHandler, Event, EventContext,
|
|
|
|
|
model::payload::{Speaking, ClientDisconnect}, ffmpeg, create_player, Call, CoreEvent, events::context_data::ConnectData};
|
|
|
|
|
model::{payload::{Speaking, ClientDisconnect}, id::UserId}, ffmpeg, create_player, Call, CoreEvent, events::context_data::ConnectData};
|
|
|
|
|
|
|
|
|
|
struct Receiver {
|
|
|
|
|
call : Arc<Mutex<Call>>,
|
|
|
|
|
users : Arc<RwLock<HashMap<u32, Option<UserId>>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Receiver {
|
|
|
|
|
@@ -15,11 +16,23 @@ impl Receiver {
|
|
|
|
|
// You can manage state here, such as a buffer of audio packet bytes so
|
|
|
|
|
// you can later store them in intervals.
|
|
|
|
|
Self {
|
|
|
|
|
call
|
|
|
|
|
call,
|
|
|
|
|
users: Arc::new(RwLock::new(HashMap::default()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct VCUser;
|
|
|
|
|
|
|
|
|
|
impl TypeMapKey for VCUser {
|
|
|
|
|
type Value = Arc<RwLock<HashMap<String, VCUser>>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn init(client: &Client) {
|
|
|
|
|
let mut data = client.data.write().await;
|
|
|
|
|
data.insert::<VCUser>(Arc::new(RwLock::new(HashMap::default())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl EventHandler for Receiver {
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
|
@@ -40,6 +53,10 @@ impl EventHandler for Receiver {
|
|
|
|
|
// SSRCs and map the SSRC to the User ID and maintain this state.
|
|
|
|
|
// Using this map, you can map the `ssrc` in `voice_packet`
|
|
|
|
|
// to the user ID and handle their audio packets separately.
|
|
|
|
|
{
|
|
|
|
|
let mut users = self.users.write().await;
|
|
|
|
|
users.insert(ssrc.clone(), user_id.clone());
|
|
|
|
|
}
|
|
|
|
|
println!(
|
|
|
|
|
"Speaking state update: user {:?} has SSRC {:?}, using {:?}",
|
|
|
|
|
user_id,
|
|
|
|
|
@@ -50,9 +67,11 @@ impl EventHandler for Receiver {
|
|
|
|
|
Ctx::SpeakingUpdate(data) => {
|
|
|
|
|
// You can implement logic here which reacts to a user starting
|
|
|
|
|
// or stopping speaking, and to map their SSRC to User ID.
|
|
|
|
|
let users = self.users.read().await;
|
|
|
|
|
println!(
|
|
|
|
|
"Source {} has {} speaking.",
|
|
|
|
|
"Source {}/{:?} has {} speaking.",
|
|
|
|
|
data.ssrc,
|
|
|
|
|
users.get(&data.ssrc),
|
|
|
|
|
if data.speaking {"started"} else {"stopped"},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
@@ -91,17 +110,6 @@ impl EventHandler for Receiver {
|
|
|
|
|
ConnectData { channel_id, ..}
|
|
|
|
|
) => {
|
|
|
|
|
println!("VoiceDriver is connected.");
|
|
|
|
|
match channel_id {
|
|
|
|
|
Some(chan) => {
|
|
|
|
|
let ding_src =
|
|
|
|
|
std::env::var("DING_SOUND").expect("DING not found in DING_SOUND");
|
|
|
|
|
let ding = ffmpeg(ding_src).await.expect("no ding.");
|
|
|
|
|
let (audio, handle) = create_player(ding);
|
|
|
|
|
let mut call = self.call.lock().await;
|
|
|
|
|
call.play(audio);
|
|
|
|
|
},
|
|
|
|
|
None => {}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
|
|
|
|
// We won't be registering this struct for any more event classes.
|
|
|
|
|
@@ -113,6 +121,13 @@ impl EventHandler for Receiver {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn playFile(call: Arc<Mutex<Call>>, file: String) {
|
|
|
|
|
let mut call = call.lock().await;
|
|
|
|
|
let ff_src = ffmpeg(file).await.expect("Unable to find file.");
|
|
|
|
|
let (audio, handle) = create_player(ff_src);
|
|
|
|
|
call.play(audio);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn join(ctx: Context, guild: Guild, cid: ChannelId) -> Option<Arc<Mutex<Call>>> {
|
|
|
|
|
let manager = songbird::get(&ctx).await.expect("Songbird: intialization");
|
|
|
|
|
let (call, status) = manager.join(guild.id, cid).await;
|
|
|
|
|
@@ -151,12 +166,10 @@ pub async fn join(ctx: Context, guild: Guild, cid: ChannelId) -> Option<Arc<Mute
|
|
|
|
|
CoreEvent::DriverConnect.into(),
|
|
|
|
|
Receiver::new(call_handle.clone()),
|
|
|
|
|
);
|
|
|
|
|
let ding_src =
|
|
|
|
|
std::env::var("DING_SOUND").expect("DING not found in DING_SOUND");
|
|
|
|
|
let ding = ffmpeg(ding_src).await.expect("no ding.");
|
|
|
|
|
let (audio, handle) = create_player(ding);
|
|
|
|
|
call.play(audio);
|
|
|
|
|
}
|
|
|
|
|
let ding_src =
|
|
|
|
|
std::env::var("DING_SOUND").expect("DING not found in DING_SOUND");
|
|
|
|
|
playFile(call_handle, ding_src).await;
|
|
|
|
|
return Some(call);
|
|
|
|
|
}
|
|
|
|
|
Err(_err) => {
|
|
|
|
|
|