chore: refactor VCs to use a watch channel internally (but I'm going to rework this in the next commit anyway)
This commit is contained in:
@@ -2,6 +2,7 @@ use std::collections::BTreeMap;
|
||||
|
||||
use dashmap::DashMap;
|
||||
use futures::{StreamExt, stream::FuturesUnordered};
|
||||
use tokio::sync::watch;
|
||||
use twilight_model::{
|
||||
gateway::payload::incoming::VoiceStateUpdate,
|
||||
id::{
|
||||
@@ -16,7 +17,37 @@ pub type GuildVoiceChannelToTextChannel =
|
||||
BTreeMap<Id<GuildMarker>, OneToOneBTreeMap<Id<ChannelMarker>, Id<ChannelMarker>>>;
|
||||
|
||||
type VCsInGuild = OneToManyUniqueBTreeMapWithData<Id<ChannelMarker>, Id<UserMarker>, UserInVCData>;
|
||||
pub type VCs = DashMap<Id<GuildMarker>, VCsInGuild>;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct VCs(DashMap<Id<GuildMarker>, watch::Sender<VCsInGuild>>);
|
||||
|
||||
impl Extend<(Id<GuildMarker>, VCsInGuild)> for VCs {
|
||||
fn extend<T: IntoIterator<Item = (Id<GuildMarker>, VCsInGuild)>>(&mut self, iter: T) {
|
||||
for (id, guild_vcs) in iter {
|
||||
self.0.insert(id, watch::Sender::new(guild_vcs));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VCs {
|
||||
pub fn with_guild<R>(&self, id: Id<GuildMarker>, f: impl FnOnce(&VCsInGuild) -> R) -> R {
|
||||
f(&*self.0.entry(id).or_default().borrow())
|
||||
}
|
||||
|
||||
pub fn update_guild<R>(&self, id: Id<GuildMarker>, f: impl FnOnce(&mut VCsInGuild) -> R) -> R {
|
||||
let mut ret_opt = None;
|
||||
self.0.entry(id).or_default().send_modify(|guild_vcs| {
|
||||
let ret = f(guild_vcs);
|
||||
_ = ret_opt.insert(ret);
|
||||
});
|
||||
let ret = ret_opt.unwrap();
|
||||
ret
|
||||
}
|
||||
|
||||
pub fn subscribe_to_guild<R>(&self, id: Id<GuildMarker>) -> watch::Receiver<VCsInGuild> {
|
||||
self.0.entry(id).or_default().subscribe()
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(discord_client), ret)]
|
||||
async fn initialize_user_in_vc(
|
||||
@@ -107,9 +138,9 @@ pub fn update_vcs(voice_state_update: &VoiceStateUpdate, vcs: &VCs) {
|
||||
.build();
|
||||
let user_in_vc_data = voice_status.into();
|
||||
|
||||
vcs.entry(guild_id)
|
||||
.or_default()
|
||||
.insert(channel_id, user_id, user_in_vc_data);
|
||||
vcs.update_guild(guild_id, |guild_vcs| {
|
||||
guild_vcs.insert(channel_id, user_id, user_in_vc_data)
|
||||
});
|
||||
|
||||
tracing::info!(
|
||||
?guild_id,
|
||||
@@ -120,9 +151,7 @@ pub fn update_vcs(voice_state_update: &VoiceStateUpdate, vcs: &VCs) {
|
||||
}
|
||||
|
||||
None => {
|
||||
if let Some(mut channel_vcers) = vcs.get_mut(&guild_id) {
|
||||
channel_vcers.remove_right(&user_id);
|
||||
}
|
||||
vcs.update_guild(guild_id, |guild_vcs| guild_vcs.remove_right(&user_id));
|
||||
|
||||
tracing::info!(?guild_id, ?user_id, "disconnected");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user