summaryrefslogtreecommitdiff
path: root/src/betterttv.rs
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-04-09 17:04:03 +0500
committerilotterytea <iltsu@alright.party>2025-04-09 17:04:03 +0500
commit66b4eeb985e242cc0ee2925d639e976187727409 (patch)
tree6671db6c2c46bd8b15a462018173b120bbe48509 /src/betterttv.rs
parenta4ce687ddd119ff97ac1efd3b23d0c27013845f4 (diff)
feat: methods for parting channels from events
Diffstat (limited to 'src/betterttv.rs')
-rw-r--r--src/betterttv.rs40
1 files changed, 29 insertions, 11 deletions
diff --git a/src/betterttv.rs b/src/betterttv.rs
index 0dbd4a3..09f544c 100644
--- a/src/betterttv.rs
+++ b/src/betterttv.rs
@@ -169,8 +169,15 @@ impl BetterTTVWSClient {
}
pub fn join_channel(&mut self, twitch_id: usize) {
- if self.awaiting_channels.contains(&twitch_id) || self.joined_channels.contains(&twitch_id)
- {
+ if self.joined_channels.contains(&twitch_id) {
+ return;
+ }
+
+ self.awaiting_channels.insert(twitch_id);
+ }
+
+ pub fn part_channel(&mut self, twitch_id: usize) {
+ if !self.joined_channels.contains(&twitch_id) {
return;
}
@@ -254,12 +261,25 @@ impl BetterTTVWSClient {
async fn join_channels(&mut self, stream: &mut WebSocketStream<MaybeTlsStream<TcpStream>>) {
for id in &self.awaiting_channels {
- let json = serde_json::json!({
- "name": "join_channel",
- "data": {
- "name": format!("twitch:{}", id)
- }
- });
+ let json = if self.joined_channels.contains(id) {
+ self.joined_channels.remove(id);
+
+ serde_json::json!({
+ "name": "part_channel",
+ "data": {
+ "name": format!("twitch:{}", id)
+ }
+ })
+ } else {
+ self.joined_channels.insert(*id);
+
+ serde_json::json!({
+ "name": "join_channel",
+ "data": {
+ "name": format!("twitch:{}", id)
+ }
+ })
+ };
stream
.send(Message::Text(
@@ -268,9 +288,7 @@ impl BetterTTVWSClient {
.into(),
))
.await
- .expect("Error sending join request");
-
- self.joined_channels.insert(*id);
+ .expect("Error sending join/part request");
}
self.awaiting_channels.clear();