User ID Storage
This commit is contained in:
@@ -56,9 +56,8 @@ async fn main() {
|
||||
.await
|
||||
.expect("Err creating client");
|
||||
|
||||
// TODO: do checks when getting data in add guild etc.
|
||||
println!("Initialize guild voice popin state");
|
||||
utils::guild_popin::init(&client).await;
|
||||
vc::init(&client).await;
|
||||
|
||||
if let Err(why) = client.start().await {
|
||||
println!("Client error: {:?}", why);
|
||||
|
||||
@@ -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"},
|
||||
);
|
||||
},
|
||||
@@ -113,6 +132,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 +177,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) => {
|
||||
|
||||
Reference in New Issue
Block a user