Skip to content

Commit

Permalink
wip twitch bot client wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
mzrimsek committed Jun 22, 2021
1 parent d09a332 commit 7fdb61a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/clients/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { TwitchBotChatClient } from './twitch';

export * from './twitch';
export * from './obs';
export * from './firebase';
export * from './discord';
export * from './mqtt';

export const Clients = {
TwitchBotChat: new TwitchBotChatClient()
};
19 changes: 18 additions & 1 deletion src/clients/twitch/botChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getRefreshableAuthProvider } from './helpers';
export class TwitchBotChatClient {
private client: ChatClient | null = null;

async getClient(): Promise<ChatClient> {
private async getClient(): Promise<ChatClient> {
if (!this.client) {
const { channel, botClientId, botClientSecret, botTokensLocation } = twitchConfig;

Expand All @@ -23,4 +23,21 @@ export class TwitchBotChatClient {
}
return this.client;
}

async onMessage(
messageHandler: (channel: string, user: string, message: string) => void
): Promise<void> {
const client = await this.getClient();
client.onMessage(messageHandler);
}

async say(channel: string, message: string): Promise<void> {
const client = await this.getClient();
client.say(channel, message);
}

async getMods(channel: string): Promise<string[]> {
const client = await this.getClient();
return client.getMods(channel);
}
}
32 changes: 6 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,7 @@
require('dotenv').config();

import { ADMIN_USER, BOT_NAME, COMMAND_PREFACE, LIGHT_COMMANDS, OBS_COMMANDS } from './constants';
import { Clients, CommandData, RedemptionData, TwitchPubSub } from './models';
import {
apiClient,
collections,
discordClient,
firestore,
getTwitchPubSubClient,
mqttClient,
obsClient,
obsConnected,
twitchChatClient
} from './clients';
import { CommandData, RedemptionData, TwitchPubSub } from './models';
import { discordConfig, logger } from './config';
import {
handleAdminCommand,
Expand All @@ -24,24 +13,14 @@ import {
} from './commands';
import { isChannelLive, randomlyPadContent } from './utils';

import { Clients } from './clients';
import { Message } from 'discord.js';
import { PubSubRedemptionMessage } from 'twitch-pubsub-client';
import { handleOBSRedemption } from './redemptions';

const clients: Clients = {
twitchChatClient,
obsClient,
firebase: {
firestore,
collections
},
discordClient,
mqttClient
};

let commandsActive = true;

twitchChatClient.onMessage(async (channel: string, user: string, message: string) => {
Clients.TwitchBotChat.onMessage(async (channel: string, user: string, message: string) => {
if (user === BOT_NAME) return; // ignore messages from the bot

if (message[0] !== COMMAND_PREFACE) return; // ignore non command messages
Expand All @@ -54,11 +33,12 @@ twitchChatClient.onMessage(async (channel: string, user: string, message: string
return;
}

const mods = await twitchChatClient.getMods(channel);
const mods = await Clients.TwitchBotChat.getMods(channel);

const messageParts = message.split(' ');
const username = `@${user}`;
const printFunc = (content: string) => twitchChatClient.say(channel, randomlyPadContent(content));
const printFunc = (content: string) =>
Clients.TwitchBotChat.say(channel, randomlyPadContent(content));
const commandsActiveUpdateFunc = (newState: boolean) => {
commandsActive = newState;
};
Expand Down

0 comments on commit 7fdb61a

Please sign in to comment.