Skip to content

Commit

Permalink
getting OOP ready for multiple chat platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Geczy committed Nov 13, 2023
1 parent c0e31f0 commit 9886899
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
services:
twitch-chat:
platform: linux/arm64
image: "ghcr.io/dotabod/twitch-chat:v2.0-dev"
volumes:
- $PWD/packages/twitch/chat/src/:/app/packages/twitch/chat/src/

steam:
platform: linux/arm64
image: "ghcr.io/dotabod/steam:v2.0-dev"
volumes:
- $PWD/packages/steam/src:/app/packages/steam/src

twitch-events:
platform: linux/arm64
image: "ghcr.io/dotabod/twitch-events:v2.0-dev"
volumes:
- $PWD/packages/twitch/events/src:/app/packages/twitch/events/src

dota:
image: "ghcr.io/dotabod/dota:v2.0-dev"
platform: linux/arm64
volumes:
- $PWD/packages/dota/src:/app/packages/dota/src
- $PWD/packages/dota/jest.config.ts:/app/packages/dota/jest.config.ts
Expand Down
46 changes: 46 additions & 0 deletions packages/twitch/chat/src/ChatPlatformClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Abstract class to define a standard interface for chat platform clients.
* This will ensure that all chat platform clients have a consistent interface.
*/
export default class ChatPlatformClient {
/**
* Connect to the chat platform.
*/
async connect() {
throw new Error('connect method must be implemented')
}

/**
* Send a message to a specified channel.
* @param {string} channel - The channel to send the message to.
* @param {string} message - The message to send.
*/
async say(channel, message) {
throw new Error('say method must be implemented')
}

/**
* Join a specific channel.
* @param {string} channel - The channel to join.
*/
async join(channel) {
throw new Error('join method must be implemented')
}

/**
* Leave a specific channel.
* @param {string} channel - The channel to leave.
*/
async part(channel) {
throw new Error('part method must be implemented')
}

/**
* Handle incoming messages.
* This method should be overridden to handle messages in a platform-specific way.
* @param {function} callback - The callback function to handle messages.
*/
onMessage(callback) {
throw new Error('onMessage method must be implemented')
}
}
68 changes: 68 additions & 0 deletions packages/twitch/chat/src/KickChatClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// path/filename: src/chat/KickChatClient.js

import { Kient } from 'kient' // Assuming Kient is the correct package for the Kick API

import ChatPlatformClient from './ChatPlatformClient'

/**
* Kick chat client implementation.
*/
export default class KickChatClient extends ChatPlatformClient {
constructor(channelId) {
super()
this.client = null
this.channelId = channelId
}

/**
* Connect to Kick chat.
*/
async connect() {
this.client = await Kient.create()
await this.client.api.authentication.login({
email: 'info@x.com', // Replace with actual credentials or configuration
password: 'x', // Replace with actual credentials or configuration
})

const channel = await this.client.api.channel.getChannel(this.channelId)
await this.client.ws.chatroom.listen(channel.chatroom.id)

console.log('[KICK] Connected to chat client')
}

/**
* Send a message to a Kick channel.
* This might need adjustment based on Kick API capabilities.
* @param {string} channel - The channel to send the message to.
* @param {string} message - The message to send.
*/
async say(channel, message) {
// Implementation depends on Kick API's capabilities
}

/**
* Join a Kick channel.
* This might be redundant or need adjustment based on Kick API capabilities.
* @param {string} channel - The channel to join.
*/
async join(channel) {
// Implementation depends on Kick API's capabilities
}

/**
* Leave a Kick channel.
* This might be redundant or need adjustment based on Kick API capabilities.
* @param {string} channel - The channel to leave.
*/
async part(channel) {
// Implementation depends on Kick API's capabilities
}

/**
* Handle incoming messages from Kick.
* @param {function} callback - The callback function to handle messages.
*/
onMessage(callback) {
this.client.on('onMessage', callback)
}
}
66 changes: 66 additions & 0 deletions packages/twitch/chat/src/TwitchChatClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// path/filename: src/chat/TwitchChatClient.js

import { ChatClient } from '@twurple/chat'

import ChatPlatformClient from './ChatPlatformClient'
import { getBotAuthProvider } from './twitch/lib/getBotAuthProvider'
import { getChannels } from './twitch/lib/getChannels'

/**
* Twitch chat client implementation.
*/
export default class TwitchChatClient extends ChatPlatformClient {
constructor() {
super()
this.client = null
}

/**
* Connect to Twitch chat.
*/
async connect() {
this.client = new ChatClient({
isAlwaysMod: true,
botLevel: process.env.NODE_ENV === 'production' ? 'verified' : undefined,
authProvider: await getBotAuthProvider(),
channels: getChannels,
webSocket: true,
})

await this.client.connect()
console.log('[TWITCH] Connected to chat client')
}

/**
* Send a message to a Twitch channel.
* @param {string} channel - The channel to send the message to.
* @param {string} message - The message to send.
*/
async say(channel, message) {
await this.client.say(channel, message)
}

/**
* Join a Twitch channel.
* @param {string} channel - The channel to join.
*/
async join(channel) {
await this.client.join(channel)
}

/**
* Leave a Twitch channel.
* @param {string} channel - The channel to leave.
*/
async part(channel) {
await this.client.part(channel)
}

/**
* Handle incoming messages from Twitch.
* @param {function} callback - The callback function to handle messages.
*/
onMessage(callback) {
this.client.on('message', callback)
}
}

0 comments on commit 9886899

Please sign in to comment.