Skip to content

Commit

Permalink
feat(sources): include source channel in events
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Shatford <[email protected]>
  • Loading branch information
jordanshatford committed Dec 19, 2024
1 parent 9726a43 commit b31f466
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-ears-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cq/sources": minor
---

feat: include source channel in events
6 changes: 5 additions & 1 deletion packages/sources/src/twitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ export class TwitchChatSource extends BaseClipSource {
if (callback) {
this.ctx = callback
}
const ctx = await this.ctx()
if (ctx.username) {
this.channel = ctx.username
}
try {
this.chat = new TwitchChat(await this.ctx())
this.chat = new TwitchChat(ctx)
} catch (e) {
this.handleError(e)
return
Expand Down
11 changes: 10 additions & 1 deletion packages/sources/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum ClipSourceStatus {
export interface ClipSourceEvent<T = undefined> {
timestamp: string
source: ClipSource
channel: string
data: T
}

Expand Down Expand Up @@ -63,6 +64,8 @@ export abstract class BaseClipSource

public status = ClipSourceStatus.UNKNOWN

protected channel = 'unknown'

public abstract connect(callback?: ClipSourceCtxCallback): Promise<void>
public abstract disconnect(): Promise<void>

Expand All @@ -75,6 +78,7 @@ export abstract class BaseClipSource
this.emit('status', {
timestamp: timestamp ?? this.timestamp(),
source: this.name,
channel: this.channel,
data: status
})
}
Expand All @@ -84,14 +88,15 @@ export abstract class BaseClipSource
this.emit('error', {
timestamp,
source: this.name,
channel: this.channel,
data: error
})
this.handleStatusUpdate(ClipSourceStatus.ERROR, timestamp)
}

protected handleConnected() {
const timestamp = this.timestamp()
this.emit('connected', { timestamp, source: this.name, data: undefined })
this.emit('connected', { timestamp, source: this.name, channel: this.channel, data: undefined })
this.handleStatusUpdate(ClipSourceStatus.CONNECTED, timestamp)
}

Expand All @@ -100,6 +105,7 @@ export abstract class BaseClipSource
this.emit('disconnected', {
timestamp,
source: this.name,
channel: this.channel,
data: reason
})
this.handleStatusUpdate(ClipSourceStatus.DISCONNECTED, timestamp)
Expand All @@ -110,6 +116,7 @@ export abstract class BaseClipSource
this.emit('message', {
timestamp,
source: this.name,
channel: this.channel,
data: {
...message,
urls: getAllURLsFromText(message.text)
Expand All @@ -123,6 +130,7 @@ export abstract class BaseClipSource
this.emit('message-deleted', {
timestamp,
source: this.name,
channel: this.channel,
data: {
...message,
urls: getAllURLsFromText(message.text)
Expand All @@ -136,6 +144,7 @@ export abstract class BaseClipSource
this.emit('user-timeout', {
timestamp,
source: this.name,
channel: this.channel,
data: username
})
this.handleStatusUpdate(ClipSourceStatus.CONNECTED, timestamp)
Expand Down

0 comments on commit b31f466

Please sign in to comment.