First impl of the streamer.bot api changes

This commit is contained in:
2024-01-26 12:46:46 -05:00
parent 79dccc9709
commit 9fafe2b318
8 changed files with 235 additions and 45 deletions

131
main.js
View File

@@ -1,26 +1,95 @@
const { InstanceBase, Regex, runEntrypoint, InstanceStatus } = require('@companion-module/base')
const UpgradeScripts = require('./upgrades')
const UpdateActions = require('./actions')
const UpdateFeedbacks = require('./feedbacks')
const UpdateVariableDefinitions = require('./variables')
import { InstanceBase, Regex, runEntrypoint, InstanceStatus } from '@companion-module/base'
import UpgradeScripts from './upgrades.js'
import UpdateActions from './actions.js'
import UpdateFeedbacks from './feedbacks.js'
import UpdateVariableDefinitions from './variables.js'
import { StreamerbotClient } from '@streamerbot/client'
class ModuleInstance extends InstanceBase {
constructor(internal) {
super(internal)
this.loaded = false
this.intervalId = undefined
this.config = {}
this.credits = {}
this.actions = []
this.activeViewers = 0
this.dataArray = []
}
async subscribe() {
this.dataArray['CrowdControl'] = []
this.dataArray['Patreon'] = []
this.dataArray['StreamElements'] = []
this.dataArray['Streamlabs'] = []
this.dataArray['TipeeeStream'] = []
this.dataArray['Twitch'] = []
this.dataArray['YouTube'] = []
await this.client.on(
['CrowdControl.*', 'Patreon.*', 'StreamElements.*', 'Streamlabs.*', 'TipeeeStream.*', 'Twitch.*', 'YouTube.*'],
(data) => {
console.log('Data Received!', data)
if (this.dataArray.hasOwnProperty(data.event.source) === false) this.dataArray[data.event.source] = []
if (this.dataArray[data.event.source].hasOwnProperty(data.event.type) === false)
this.dataArray[data.event.source][data.event.type] = {}
this.dataArray[data.event.source][data.event.type] = data
}
)
}
async init(config) {
this.config = config
this.client = new StreamerbotClient({
host: this.config.host,
port: this.config.port,
endpoint: this.config.endpoint,
onConnect: async (data) => {
await this.subscribe()
this.updateStatus(InstanceStatus.Ok)
this.loaded = true
this.updateStatus(InstanceStatus.Ok)
this.updateActions() // export actions
this.updateFeedbacks() // export feedbacks
this.updateVariableDefinitions() // export variable definitions
await this.client.getActions().then((value) => {
this.actions = value
})
await this.client.getActiveViewers().then((value) => {
this.activeViewers = value.count
})
await this.client.getCredits().then((value) => {
this.credits = value
})
this.updateActions() // export actions
this.updateFeedbacks() // export feedbacks
this.updateVariableDefinitions() // export variable definitions
// this.updateVariableValues()
this.intervalId = setInterval(() => {
this.updateVariableValues()
}, 1000)
},
onDisconnect: () => {
this.updateStatus(InstanceStatus.Disconnected)
},
onError: (error) => {
this.loaded = false
this.log('debug', error.message)
this.updateStatus(InstanceStatus.UnknownError)
},
})
this.updateStatus(InstanceStatus.Connecting)
}
// When module gets deleted
async destroy() {
this.log('debug', 'destroy')
clearInterval(this.intervalId)
await this.client.unsubscribe('*')
await this.client.disconnect()
}
async configUpdated(config) {
@@ -31,18 +100,27 @@ class ModuleInstance extends InstanceBase {
getConfigFields() {
return [
{
type: 'textinput',
id: 'host',
type: 'textinput',
label: 'Target IP',
width: 8,
regex: Regex.IP,
tooltip: 'Address of the Streamer.bot WebSocket Server. Default localhost',
default: 'localhost',
},
{
type: 'textinput',
id: 'port',
type: 'textinput',
label: 'Target Port',
width: 4,
width: 5,
regex: Regex.PORT,
tooltip: 'Port of the Streamer.bot WebSocket Server. Default 8080',
default: '8080',
},
{
id: 'endpoint',
type: 'textinput',
label: 'Endpoint',
tooltip: 'Endpoint of the Streamer.bot WebSocket Server. Default /',
default: '/',
},
]
}
@@ -58,6 +136,31 @@ class ModuleInstance extends InstanceBase {
updateVariableDefinitions() {
UpdateVariableDefinitions(this)
}
updateVariableValues() {
this.setVariableValues({
viewerCount: this.activeViewers,
lastTwitchFollower: this.dataArray['Twitch']['Follow']?.data.displayName,
lastTwitchChatMessage: this.dataArray['Twitch']['ChatMessage']?.data.message.message,
lastDonationSender: this.dataArray['Streamlabs']['Donation']?.data.from,
lastDonationAmount: this.dataArray['Streamlabs']['Donation']?.data.amount,
lastDonationFormattedAmount: this.dataArray['Streamlabs']['Donation']?.data.formattedAmount,
lastDonationCurrency: this.dataArray['Streamlabs']['Donation']?.data.currency,
lastDonationMessage: this.dataArray['Streamlabs']['Donation']?.data.message,
lastMerchandiseBuyerName: this.dataArray['Streamlabs']['Merchandise']?.data.from,
lastMerchandiseBuyerProduct: this.dataArray['Streamlabs']['Merchandise']?.data.product,
lastMerchandiseBuyerMessage: this.dataArray['Streamlabs']['Merchandise']?.data.message,
lastMerchandiseProductImageUrl: this.dataArray['Streamlabs']['Merchandise']?.data.image,
lastTipUsername: this.dataArray['StreamElements']['Tip']?.data.username,
lastTipAmount: this.dataArray['StreamElements']['Tip']?.data.amount,
lastTipCurrency: this.dataArray['StreamElements']['Tip']?.data.currency,
lastTipMessage: this.dataArray['StreamElements']['Tip']?.data.message,
// lastYoutubeSubscriber: this.dataArray['YouTube']['NewSubscriber']?.data.message,
lastYoutubeChatMessage: this.dataArray['YouTube']['Message']?.data.message,
lastYoutubeSuperChat: this.dataArray['YouTube']['SuperChat']?.data.message,
// lastYoutubeSponsor: this.dataArray['YouTube']['NewSponsor']?.data.message,
})
}
}
runEntrypoint(ModuleInstance, UpgradeScripts)