Skip to content

Commit 66a04b1

Browse files
committed
feat: log message on start/stop of a stream
1 parent 4bb875c commit 66a04b1

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/twitch/eventsub.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use tokio_tungstenite::{
1111
tungstenite::{self, error::ProtocolError, http::Uri, protocol::WebSocketConfig},
1212
MaybeTlsStream,
1313
};
14-
use tracing::{debug, error, trace, warn};
14+
use tracing::{error, info, warn};
1515
use twitch_api::{
1616
eventsub::{
1717
channel::{ChannelChatMessageV1, ChannelChatMessageV1Payload},
1818
stream::{StreamOfflineV1, StreamOnlineV1},
19-
Event, EventsubWebsocketData, Payload, ReconnectPayload, SessionData, Transport,
19+
Event, EventsubWebsocketData, Message, Payload, ReconnectPayload, SessionData, Transport,
2020
WelcomePayload,
2121
},
2222
helix::chat::{SendChatMessageBody, SendChatMessageRequest},
@@ -25,6 +25,8 @@ use twitch_api::{
2525
HelixClient,
2626
};
2727

28+
use crate::twitch::StreamInfo;
29+
2830
type WebSocketStream = tokio_tungstenite::WebSocketStream<MaybeTlsStream<TcpStream>>;
2931

3032
pub struct EventSubClient {
@@ -173,14 +175,46 @@ impl EventSubClient {
173175
tx: mpsc::Sender<ChannelChatMessageV1Payload>,
174176
) -> Result<()> {
175177
match event {
176-
Event::StreamOnlineV1(Payload { message, .. }) => {
177-
debug!(?message, "got stream.online event");
178+
Event::StreamOnlineV1(Payload {
179+
message: Message::Notification(message),
180+
..
181+
}) => {
182+
let get_info = || async {
183+
let token = self.token.get(&self.client).await.ok()?;
184+
let stream = self
185+
.client
186+
.get_streams_from_ids(&[&message.broadcaster_user_id][..].into(), &*token)
187+
.next()
188+
.await?
189+
.ok()?;
190+
191+
StreamInfo::try_from(stream).ok()
192+
};
193+
194+
if let Some(info) = get_info().await {
195+
info!(
196+
info.id,
197+
%info.started_at,
198+
info.title,
199+
info.category,
200+
"streamer started streaming",
201+
);
202+
} else {
203+
info!(
204+
info.id = message.id,
205+
info.started_at = %message.started_at,
206+
"streamer started streaming",
207+
);
208+
}
178209
}
179-
Event::StreamOfflineV1(Payload { message, .. }) => {
180-
debug!(?message, "got stream.offline event");
210+
Event::StreamOfflineV1(Payload {
211+
message: Message::Notification(_),
212+
..
213+
}) => {
214+
info!("streamer stopped streaming");
181215
}
182216
Event::ChannelChatMessageV1(Payload {
183-
message: twitch_api::eventsub::Message::Notification(message),
217+
message: Message::Notification(message),
184218
..
185219
}) => {
186220
if message.chatter_user_id != self.user_id {

src/twitch/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use crate::{
3030

3131
mod eventsub;
3232

33-
#[expect(dead_code)]
3433
#[derive(Debug)]
3534
struct StreamInfo {
3635
id: String,

0 commit comments

Comments
 (0)