diff --git a/bot/chat-bot.ts b/bot/chat-bot.ts index 13776f9..981754b 100644 --- a/bot/chat-bot.ts +++ b/bot/chat-bot.ts @@ -3,16 +3,16 @@ // also it should be imported only once // so that a singleton is created. import 'reflect-metadata'; -import { ChatClient, ChatMessage, ChatRaidInfo, UserNotice } from '@twurple/chat'; +import { ChatClient, ChatCommunitySubInfo, ChatMessage, ChatRaidInfo, ChatSubExtendInfo, ChatSubGiftInfo, ChatSubInfo, UserNotice } from '@twurple/chat'; import { EventSubWsListener } from '@twurple/eventsub-ws'; import { inject, injectable } from 'inversify'; import winston from 'winston'; import { IRaidStreamEvent, - ISubscriptionStreamEvent, + ISubscriptionHandler, MessageHandler, RaidHandler, - SubscriptionHandlers, + SubscriptionHandler, } from './handlers'; import InjectionTypes from '../dependency-management/types'; @@ -23,7 +23,7 @@ export default class ChatBot { @inject(EventSubWsListener) private eventSubWsListener: EventSubWsListener, @inject(MessageHandler) private messageHandler: MessageHandler, @inject(RaidHandler) private raidHandler: IRaidStreamEvent, - @inject(SubscriptionHandlers) private subscriptionHandlers: ISubscriptionStreamEvent, + @inject(SubscriptionHandler) private subscriptionHandler: ISubscriptionHandler, @inject(InjectionTypes.Logger) private logger: winston.Logger, ) { this.logger.info(`** Chat Bot initialized **`); @@ -39,11 +39,21 @@ export default class ChatBot { }); // Subscription Event Registration - this.chatClient.onSubExtend(this.subscriptionHandlers.onSubExtend); - this.chatClient.onResub(this.subscriptionHandlers.onResubHandler); - this.chatClient.onSub(this.subscriptionHandlers.onSubscribe); - this.chatClient.onCommunitySub(this.subscriptionHandlers.onCommunitySub); - this.chatClient.onSubGift(this.subscriptionHandlers.onSubGift); + this.chatClient.onSubExtend(async (channel: string, user: string, subInfo: ChatSubExtendInfo, msg: UserNotice) => { + await this.subscriptionHandler.onSubExtend(channel, user, subInfo, msg); + }); + this.chatClient.onResub(async (channel: string, user: string, subInfo: ChatSubInfo, msg: UserNotice) => { + await this.subscriptionHandler.onResubHandler(channel, user, subInfo, msg); + }); + this.chatClient.onSub(async (channel: string, user: string, subInfo: ChatSubInfo, msg: UserNotice) => { + await this.subscriptionHandler.onSubscribe(channel, user, subInfo, msg); + }); + this.chatClient.onCommunitySub(async (channel: string, user: string, subInfo: ChatCommunitySubInfo, msg: UserNotice) => { + await this.subscriptionHandler.onCommunitySub(channel, user, subInfo, msg); + }); + this.chatClient.onSubGift(async (channel: string, user: string, subInfo: ChatSubGiftInfo, msg: UserNotice) => { + await this.subscriptionHandler.onSubGift(channel, user, subInfo, msg); + }); // Authenticaiton Event Registration this.chatClient.onAuthenticationSuccess(() => this.logger.info('Chat Client authenticated successfully')); diff --git a/bot/handlers/index.ts b/bot/handlers/index.ts index 7975ad2..18445f4 100644 --- a/bot/handlers/index.ts +++ b/bot/handlers/index.ts @@ -1,7 +1,7 @@ import { IFollowStreamEvent, FollowHandler } from './follow.handler'; import { MessageHandler } from './message.handler'; import { IRaidStreamEvent, RaidHandler } from './raid.handler'; -import { ISubscriptionStreamEvent, SubscriptionHandlers } from './subscription.handler'; +import { ISubscriptionHandler, SubscriptionHandler } from './subscription.handler'; export { IFollowStreamEvent, @@ -9,6 +9,6 @@ export { MessageHandler, IRaidStreamEvent, RaidHandler, - ISubscriptionStreamEvent, - SubscriptionHandlers, + ISubscriptionHandler, + SubscriptionHandler, }; diff --git a/bot/handlers/subscription.handler.ts b/bot/handlers/subscription.handler.ts index 7c6913a..91ec2f8 100644 --- a/bot/handlers/subscription.handler.ts +++ b/bot/handlers/subscription.handler.ts @@ -16,7 +16,7 @@ import { SubscriptionType, } from '../../database'; -export interface ISubscriptionStreamEvent { +export interface ISubscriptionHandler { onSubscribe(channel: string, user: string, subInfo: ChatSubInfo, message: UserNotice): Promise; onSubExtend(channel: string, user: string, subInfo: ChatSubExtendInfo, message: UserNotice): Promise; onResubHandler(channel: string, user: string, subInfo: ChatSubInfo, message: UserNotice): Promise @@ -25,7 +25,7 @@ export interface ISubscriptionStreamEvent { } @injectable() -export class SubscriptionHandlers implements ISubscriptionStreamEvent { +export class SubscriptionHandler implements ISubscriptionHandler { private giftCounts = new Map(); constructor( diff --git a/dependency-management/inversify.config.ts b/dependency-management/inversify.config.ts index 29b6f7f..5a4ab37 100644 --- a/dependency-management/inversify.config.ts +++ b/dependency-management/inversify.config.ts @@ -44,10 +44,10 @@ import { FollowHandler, IFollowStreamEvent, IRaidStreamEvent, - ISubscriptionStreamEvent, + ISubscriptionHandler, MessageHandler, RaidHandler, - SubscriptionHandlers, + SubscriptionHandler, } from '../bot/handlers'; import Broadcaster from '../bot/utilities/broadcaster'; import InjectionTypes from './types'; @@ -73,7 +73,7 @@ SAContainer.bind(OverlayServer).toSelf().inSingletonScope(); // SAContainer.bind(FollowHandler).toSelf(); SAContainer.bind(MessageHandler).toSelf(); SAContainer.bind(RaidHandler).toSelf(); -SAContainer.bind(SubscriptionHandlers).toSelf(); +SAContainer.bind(SubscriptionHandler).toSelf(); // Bot Command Handler bindings SAContainer.bind(InjectionTypes.CommandHandlers).to(AboutCommand);