Compare commits

..

2 Commits

2 changed files with 51 additions and 49 deletions

View File

@@ -11,11 +11,9 @@ use songbird::{
CoreEvent, Event, EventContext, EventHandler, Songbird,
driver::{Channels, SampleRate},
};
use std::{
sync::{Arc, Mutex},
time::Instant,
};
use std::{sync::Arc, time::Instant};
use time::UtcDateTime;
use tokio::sync::RwLock;
use twilight_model::id::{
Id,
marker::{ChannelMarker, GuildMarker, UserMarker},
@@ -31,7 +29,7 @@ struct Handler {
guild_id: Id<GuildMarker>,
channel_id: Id<ChannelMarker>,
known_ssrcs: Arc<Mutex<OneToManyUniqueBTreeMap<Id<UserMarker>, u32>>>,
known_ssrcs: Arc<RwLock<OneToManyUniqueBTreeMap<Id<UserMarker>, u32>>>,
audio_channels: u16,
audio_sample_rate: u32,
@@ -53,15 +51,15 @@ impl EventHandler for Handler {
let user_id = Id::new(user_id.0);
self.known_ssrcs
.lock()
.unwrap()
.write()
.await
.insert(user_id, speaking.ssrc);
}
}
EventContext::VoiceTick(voice_tick) => {
for (ssrc, voice_data) in &voice_tick.speaking {
let user_id = {
let known_ssrcs = self.known_ssrcs.lock().unwrap();
let known_ssrcs = self.known_ssrcs.read().await;
known_ssrcs.get_left_for(ssrc).cloned()
};
@@ -126,7 +124,6 @@ impl EventHandler for Handler {
let channels = self.audio_channels;
let sample_rate = self.audio_sample_rate;
let recording_manager = self.recording_manager.clone();
let samples = pcm.clone();

View File

@@ -30,48 +30,53 @@ pub async fn heat_seek(state: State) {
let mut vcs_in_guild_senders = BTreeMap::default();
loop {
for (&guild_id, vcs_in_guild) in &*vcs_watcher.borrow() {
let vcs_in_guild_sender = vcs_in_guild_senders.entry(guild_id).or_insert_with(|| {
let (vcs_in_guild_sender, vcs_in_guild_watcher) =
watch::channel(Default::default());
let (channel_heat_sender, channel_heat_watcher) =
watch::channel(Default::default());
let (heat_map_sender, heat_map_watcher) = watch::channel(Default::default());
let (hottest_vc_sender, hottest_vc_watcher) = watch::channel(Default::default());
{
for (&guild_id, vcs_in_guild) in &*vcs_watcher.borrow() {
let vcs_in_guild_sender =
vcs_in_guild_senders.entry(guild_id).or_insert_with(|| {
let (vcs_in_guild_sender, vcs_in_guild_watcher) =
watch::channel(Default::default());
let (channel_heat_sender, channel_heat_watcher) =
watch::channel(Default::default());
let (heat_map_sender, heat_map_watcher) =
watch::channel(Default::default());
let (hottest_vc_sender, hottest_vc_watcher) =
watch::channel(Default::default());
tokio::spawn(
evaluate_heat()
.bot_manager(state.bot_manager.clone())
.bot_owner_user_id(state.discord_bot_owner_user_id)
.bot_user_id(state.discord_user_id)
.cancellation_token(state.cancellation_token.clone())
.channel_heat_sender(channel_heat_sender)
.vcs_in_guild_watcher(vcs_in_guild_watcher)
.call(),
);
tokio::spawn(
map_heat()
.cancellation_token(state.cancellation_token.clone())
.channel_heat_watcher(channel_heat_watcher)
.heat_map_sender(heat_map_sender)
.call(),
);
tokio::spawn(
track_hottest_vc()
.cancellation_token(state.cancellation_token.clone())
.heat_map_watcher(heat_map_watcher)
.hottest_vc_sender(hottest_vc_sender)
.call(),
);
tokio::spawn(follow_hottest_vc(
state.clone(),
guild_id,
hottest_vc_watcher,
));
tokio::spawn(
evaluate_heat()
.bot_manager(state.bot_manager.clone())
.bot_owner_user_id(state.discord_bot_owner_user_id)
.bot_user_id(state.discord_user_id)
.cancellation_token(state.cancellation_token.clone())
.channel_heat_sender(channel_heat_sender)
.vcs_in_guild_watcher(vcs_in_guild_watcher)
.call(),
);
tokio::spawn(
map_heat()
.cancellation_token(state.cancellation_token.clone())
.channel_heat_watcher(channel_heat_watcher)
.heat_map_sender(heat_map_sender)
.call(),
);
tokio::spawn(
track_hottest_vc()
.cancellation_token(state.cancellation_token.clone())
.heat_map_watcher(heat_map_watcher)
.hottest_vc_sender(hottest_vc_sender)
.call(),
);
tokio::spawn(follow_hottest_vc(
state.clone(),
guild_id,
hottest_vc_watcher,
));
vcs_in_guild_sender
});
vcs_in_guild_sender.send_replace(Arc::new(vcs_in_guild.clone()));
vcs_in_guild_sender
});
vcs_in_guild_sender.send_replace(Arc::new(vcs_in_guild.clone()));
}
}
if matches!(