All files / src/services/announcements AnnouncementService.ts

100% Statements 26/26
100% Branches 9/9
100% Functions 5/5
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 274x 4x 4x 4x 4x 4x 4x 1x 12x 12x 12x 12x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 5x 1x  
import { renderTemplate } from "@workspace/shared/twitchEventSub"
import type { TwitchApiService } from "../TwitchApiService"
import type { TwitchAuthService } from "../TwitchAuthService"
import type { Logger } from "@workspace/logger"
 
type Definition = Parameters<typeof renderTemplate>[0]
export class AnnouncementService {
  constructor(
    private readonly api: Pick<TwitchApiService, "sendChatMessage">,
    private readonly auth: Pick<TwitchAuthService, "botToken">,
    private readonly logger: Logger
  ) {}
  async send(
    broadcasterId: string,
    definition: Definition,
    values: Record<string, string>,
    template?: string,
    beforeSend?: () => Promise<boolean>
  ): Promise<void> {
    const message = renderTemplate(definition, values, template)
    const token = await this.auth.botToken()
    if (beforeSend && !(await beforeSend())) return
    await this.api.sendChatMessage(token, broadcasterId, message)
    this.logger.info("Twitch chat message sent")
  }
}