From b31f46621bd34ea859bc9e057e36b95716530aa0 Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Thu, 19 Dec 2024 10:40:31 -0400 Subject: [PATCH] feat(sources): include source channel in events Signed-off-by: Jordan Shatford --- .changeset/quiet-ears-brake.md | 5 +++++ packages/sources/src/twitch.ts | 6 +++++- packages/sources/src/types.ts | 11 ++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 .changeset/quiet-ears-brake.md diff --git a/.changeset/quiet-ears-brake.md b/.changeset/quiet-ears-brake.md new file mode 100644 index 00000000..2d68a65e --- /dev/null +++ b/.changeset/quiet-ears-brake.md @@ -0,0 +1,5 @@ +--- +"@cq/sources": minor +--- + +feat: include source channel in events diff --git a/packages/sources/src/twitch.ts b/packages/sources/src/twitch.ts index 2762f729..f7b019eb 100644 --- a/packages/sources/src/twitch.ts +++ b/packages/sources/src/twitch.ts @@ -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 diff --git a/packages/sources/src/types.ts b/packages/sources/src/types.ts index 68504381..45da6b7d 100644 --- a/packages/sources/src/types.ts +++ b/packages/sources/src/types.ts @@ -15,6 +15,7 @@ export enum ClipSourceStatus { export interface ClipSourceEvent { timestamp: string source: ClipSource + channel: string data: T } @@ -63,6 +64,8 @@ export abstract class BaseClipSource public status = ClipSourceStatus.UNKNOWN + protected channel = 'unknown' + public abstract connect(callback?: ClipSourceCtxCallback): Promise public abstract disconnect(): Promise @@ -75,6 +78,7 @@ export abstract class BaseClipSource this.emit('status', { timestamp: timestamp ?? this.timestamp(), source: this.name, + channel: this.channel, data: status }) } @@ -84,6 +88,7 @@ export abstract class BaseClipSource this.emit('error', { timestamp, source: this.name, + channel: this.channel, data: error }) this.handleStatusUpdate(ClipSourceStatus.ERROR, timestamp) @@ -91,7 +96,7 @@ export abstract class BaseClipSource 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) } @@ -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) @@ -110,6 +116,7 @@ export abstract class BaseClipSource this.emit('message', { timestamp, source: this.name, + channel: this.channel, data: { ...message, urls: getAllURLsFromText(message.text) @@ -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) @@ -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)