track how many sounds are playing in a channel

This commit is contained in:
2021-04-10 20:06:10 +01:00
parent 909e8e351a
commit d70f790b3d
4 changed files with 162 additions and 65 deletions

40
src/event_handlers.rs Normal file
View File

@ -0,0 +1,40 @@
use serenity::async_trait;
use serenity::model::id::GuildId;
use songbird::Event;
use songbird::EventContext;
use songbird::EventHandler as SongbirdEventHandler;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
pub struct RestartTrack;
#[async_trait]
impl SongbirdEventHandler for RestartTrack {
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
if let EventContext::Track(&[(_state, track)]) = ctx {
let _ = track.seek_time(Default::default());
}
None
}
}
pub struct UpdateTrackCount {
pub guild_id: GuildId,
pub track_count: Arc<RwLock<HashMap<GuildId, u32>>>,
}
#[async_trait]
impl SongbirdEventHandler for UpdateTrackCount {
async fn act(&self, _ctx: &EventContext<'_>) -> Option<Event> {
{
let mut write_lock = self.track_count.write().await;
let current = write_lock.get(&self.guild_id).cloned();
write_lock.insert(self.guild_id, current.unwrap_or(1) - 1);
}
None
}
}