|
| 1 | +import { ServiceClient } from "nodecg-io-core/extension/types"; |
| 2 | +import { getTokenInfo, StaticAuthProvider, TokenInfo } from "twitch-auth"; |
| 3 | +import { PubSubServiceConfig } from "./index"; |
| 4 | +import { |
| 5 | + PubSubBitsBadgeUnlockMessage, |
| 6 | + PubSubBitsMessage, |
| 7 | + PubSubChatModActionMessage, |
| 8 | + PubSubClient, |
| 9 | + PubSubListener, |
| 10 | + PubSubRedemptionMessage, |
| 11 | + PubSubSubscriptionMessage, |
| 12 | + PubSubWhisperMessage, |
| 13 | +} from "twitch-pubsub-client"; |
| 14 | +import { ApiClient } from "twitch"; |
| 15 | + |
| 16 | +export class PubSubServiceClient implements ServiceClient<PubSubClient> { |
| 17 | + constructor(private client: PubSubClient, private userid: string) {} |
| 18 | + |
| 19 | + /** |
| 20 | + * Creates a instance of TwitchServiceClient using the credentials from the passed config. |
| 21 | + */ |
| 22 | + static async createClient(cfg: PubSubServiceConfig): Promise<PubSubServiceClient> { |
| 23 | + // Create a twitch authentication provider |
| 24 | + const tokenInfo = await PubSubServiceClient.getTokenInfo(cfg); |
| 25 | + const authProvider = new StaticAuthProvider(tokenInfo.clientId, this.normalizeToken(cfg), tokenInfo.scopes); |
| 26 | + |
| 27 | + // Create the actual chat client and connect |
| 28 | + const apiClient = new ApiClient({ authProvider }); |
| 29 | + const pubSubClient = new PubSubClient(); |
| 30 | + const userID = await pubSubClient.registerUserListener(apiClient); |
| 31 | + |
| 32 | + return new PubSubServiceClient(pubSubClient, userID); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets the token info for the passed config. |
| 37 | + */ |
| 38 | + static async getTokenInfo(cfg: PubSubServiceConfig): Promise<TokenInfo> { |
| 39 | + return await getTokenInfo(this.normalizeToken(cfg)); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Strips any "oauth:" before the token away, because the client needs the token without it. |
| 44 | + */ |
| 45 | + static normalizeToken(cfg: PubSubServiceConfig): string { |
| 46 | + return cfg.oauthKey.replace("oauth:", ""); |
| 47 | + } |
| 48 | + |
| 49 | + getNativeClient(): PubSubClient { |
| 50 | + return this.client; |
| 51 | + } |
| 52 | + |
| 53 | + getUserID(): string { |
| 54 | + return this.userid; |
| 55 | + } |
| 56 | + |
| 57 | + async onRedemption( |
| 58 | + callback: (message: PubSubRedemptionMessage) => void, |
| 59 | + ): Promise<PubSubListener<PubSubRedemptionMessage>> { |
| 60 | + return await this.client.onRedemption(this.userid, callback); |
| 61 | + } |
| 62 | + |
| 63 | + async onSubscription( |
| 64 | + callback: (message: PubSubSubscriptionMessage) => void, |
| 65 | + ): Promise<PubSubListener<PubSubSubscriptionMessage>> { |
| 66 | + return await this.client.onSubscription(this.userid, callback); |
| 67 | + } |
| 68 | + |
| 69 | + async onBits(callback: (message: PubSubBitsMessage) => void): Promise<PubSubListener<PubSubBitsMessage>> { |
| 70 | + return await this.client.onBits(this.userid, callback); |
| 71 | + } |
| 72 | + |
| 73 | + async onBitsBadgeUnlock( |
| 74 | + callback: (message: PubSubBitsBadgeUnlockMessage) => void, |
| 75 | + ): Promise<PubSubListener<PubSubBitsBadgeUnlockMessage>> { |
| 76 | + return await this.client.onBitsBadgeUnlock(this.userid, callback); |
| 77 | + } |
| 78 | + |
| 79 | + async onModAction( |
| 80 | + channelID: string, |
| 81 | + callback: (message: PubSubChatModActionMessage) => void, |
| 82 | + ): Promise<PubSubListener<PubSubChatModActionMessage>> { |
| 83 | + return await this.client.onModAction(this.userid, channelID, callback); |
| 84 | + } |
| 85 | + |
| 86 | + async onWhisper(callback: (message: PubSubWhisperMessage) => void): Promise<PubSubListener<PubSubWhisperMessage>> { |
| 87 | + return await this.client.onWhisper(this.userid, callback); |
| 88 | + } |
| 89 | +} |
0 commit comments