Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add EventSub channel.subscription.message
- Loading branch information
Showing
5 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
packages/twitch-eventsub/src/Events/EventSubChannelSubscriptionMessageEvent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { Enumerable } from '@d-fischer/shared-utils'; | ||
import type { ApiClient, HelixUser } from 'twitch'; | ||
import { rtfm } from 'twitch-common'; | ||
|
||
/** | ||
* The tier of a subscription. 1000 means tier 1, and so on. | ||
*/ | ||
export type EventSubChannelSubscriptionMessageEventTier = '1000' | '2000' | '3000'; | ||
|
||
/** @private */ | ||
export interface EventSubChannelSubscriptionMessageEmoteData { | ||
begin: number; | ||
end: number; | ||
id: string; | ||
} | ||
|
||
/** @private */ | ||
export interface EventSubChannelSubscriptionMessageData { | ||
text: string; | ||
emotes: EventSubChannelSubscriptionMessageEmoteData[]; | ||
} | ||
|
||
/** @private */ | ||
export interface EventSubChannelSubscriptionMessageEventData { | ||
user_id: string; | ||
user_login: string; | ||
user_name: string; | ||
broadcaster_user_id: string; | ||
broadcaster_user_login: string; | ||
broadcaster_user_name: string; | ||
tier: EventSubChannelSubscriptionMessageEventTier; | ||
message: EventSubChannelSubscriptionMessageData; | ||
cumulative_months: number; | ||
streak_months: number | null; | ||
duration_months: number; | ||
} | ||
|
||
/** | ||
* An EventSub event representing the public announcement of a channel subscription by the subscriber. | ||
*/ | ||
@rtfm<EventSubChannelSubscriptionMessageEvent>('twitch-eventsub', 'EventSubChannelSubscriptionMessageEvent', 'userId') | ||
export class EventSubChannelSubscriptionMessageEvent { | ||
/** @private */ | ||
@Enumerable(false) protected readonly _client: ApiClient; | ||
|
||
/** @private */ | ||
constructor(private readonly _data: EventSubChannelSubscriptionMessageEventData, client: ApiClient) { | ||
this._client = client; | ||
} | ||
|
||
/** | ||
* The ID of the user whose subscription is being announced. | ||
*/ | ||
get userId(): string { | ||
return this._data.user_id; | ||
} | ||
|
||
/** | ||
* The name of the user whose subscription is being announced. | ||
*/ | ||
get userName(): string { | ||
return this._data.user_login; | ||
} | ||
|
||
/** | ||
* The display name of the user whose subscription is being announced. | ||
*/ | ||
get userDisplayName(): string { | ||
return this._data.user_name; | ||
} | ||
|
||
/** | ||
* Retrieves more information about the user whose subscription is being announced. | ||
*/ | ||
async getUser(): Promise<HelixUser> { | ||
return (await this._client.helix.users.getUserById(this._data.user_id))!; | ||
} | ||
|
||
/** | ||
* The ID of the broadcaster. | ||
*/ | ||
get broadcasterId(): string { | ||
return this._data.broadcaster_user_id; | ||
} | ||
|
||
/** | ||
* The name of the broadcaster. | ||
*/ | ||
get broadcasterName(): string { | ||
return this._data.broadcaster_user_login; | ||
} | ||
|
||
/** | ||
* The display name of the broadcaster. | ||
*/ | ||
get broadcasterDisplayName(): string { | ||
return this._data.broadcaster_user_name; | ||
} | ||
|
||
/** | ||
* Retrieves more information about the broadcaster. | ||
*/ | ||
async getBroadcaster(): Promise<HelixUser> { | ||
return (await this._client.helix.users.getUserById(this._data.broadcaster_user_id))!; | ||
} | ||
|
||
/** | ||
* The tier of the subscription, either 1000, 2000 or 3000. | ||
*/ | ||
get tier(): EventSubChannelSubscriptionMessageEventTier { | ||
return this._data.tier; | ||
} | ||
|
||
/** | ||
* The total number of months the user has been subscribed. | ||
*/ | ||
get cumulativeMonths(): number { | ||
return this._data.cumulative_months; | ||
} | ||
|
||
/** | ||
* The number of months the user has been subscribed in a row, or null if they don't want to share it. | ||
*/ | ||
get streakMonths(): number | null { | ||
return this._data.streak_months; | ||
} | ||
|
||
/** | ||
* The number of months the user has now subscribed. | ||
*/ | ||
get durationMonths(): number { | ||
return this._data.duration_months; | ||
} | ||
|
||
/** | ||
* The text of the message. | ||
*/ | ||
get messageText(): string { | ||
return this._data.message.text; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/twitch-eventsub/src/Subscriptions/EventSubChannelSubscriptionMessageSubscription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { HelixEventSubSubscription } from 'twitch'; | ||
import { rtfm } from 'twitch-common'; | ||
import type { EventSubChannelSubscriptionMessageEventData } from '../Events/EventSubChannelSubscriptionMessageEvent'; | ||
import { EventSubChannelSubscriptionMessageEvent } from '../Events/EventSubChannelSubscriptionMessageEvent'; | ||
import type { EventSubBase } from '../EventSubBase'; | ||
import { EventSubSubscription } from './EventSubSubscription'; | ||
|
||
/** | ||
* @private | ||
*/ | ||
@rtfm('twitch-eventsub', 'EventSubSubscription') | ||
export class EventSubChannelSubscriptionMessageSubscription extends EventSubSubscription<EventSubChannelSubscriptionMessageEvent> { | ||
constructor( | ||
handler: (data: EventSubChannelSubscriptionMessageEvent) => void, | ||
client: EventSubBase, | ||
private readonly _userId: string | ||
) { | ||
super(handler, client); | ||
} | ||
|
||
get id(): string { | ||
return `channel.subscription.end.${this._userId}`; | ||
} | ||
|
||
protected transformData( | ||
data: EventSubChannelSubscriptionMessageEventData | ||
): EventSubChannelSubscriptionMessageEvent { | ||
return new EventSubChannelSubscriptionMessageEvent(data, this._client._apiClient); | ||
} | ||
|
||
protected async _subscribe(): Promise<HelixEventSubSubscription> { | ||
return this._client._apiClient.helix.eventSub.subscribeToChannelSubscriptionMessageEvents( | ||
this._userId, | ||
await this._getTransportOptions() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters