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

View File

@@ -1,3 +1,3 @@
# companion-module-[replace with module name]
# companion-module-stream.bot-api
See [HELP.md](./companion/HELP.md) and [LICENSE](./LICENSE)

View File

@@ -1,19 +1,87 @@
module.exports = function (self) {
export default function (self) {
self.setActionDefinitions({
sample_action: {
name: 'My First Action',
runAction: {
name: 'Run Action',
options: [
{
id: 'num',
type: 'number',
label: 'Test',
default: 5,
min: 0,
max: 100,
id: 'action',
type: 'dropdown',
label: 'Action',
default: '',
choices: self.actions.actions.map((value) => ({ label: value.name, ...value })),
},
],
callback: async (event) => {
console.log('Hello world!', event.options.num)
console.log('Run Action!', event.options.action)
await self.client.doAction(event.options.action)
},
},
runActionAdvanced: {
name: 'Run Action Advanced',
options: [
{
id: 'action',
type: 'dropdown',
label: 'Action',
default: '',
choices: self.actions.actions.map((value) => ({ label: value.name, ...value })),
},
{
id: 'actionArgs',
type: 'textinput',
label: 'Action Arguments',
default: 0,
},
],
callback: async (event) => {
console.log('Run Action Advanced!', event.options.action)
await self.client.doAction(event.options.action, event.options.actionArgs)
},
},
runActionByName: {
name: 'Run Action by Name',
options: [
{
id: 'actionName',
type: 'textinput',
label: 'Action Name',
default: '',
},
],
callback: async (event) => {
console.log('Run Action by Name: ', event.options.actionName)
await self.client.doAction(event.options.actionName)
},
},
runActionById: {
name: 'Run Action by Id',
options: [
{
id: 'actionId',
type: 'textinput',
label: 'Action Id',
default: 0,
},
],
callback: async (event) => {
console.log('Run Action by Id: ', event.options.actionId)
await self.client.doAction(event.options.actionId)
},
},
clearCredits: {
name: 'Clear Credits',
options: [],
callback: async (event) => {
console.log('Clear Credits!')
await self.client.clearCredits()
},
},
testCredits: {
name: 'Test Credits',
options: [],
callback: async (event) => {
console.log('Test Credits!')
await self.client.testCredits()
},
},
})

View File

@@ -1,16 +1,16 @@
{
"id": "your-module-name",
"name": "your-module-name",
"shortname": "module-shortname",
"description": "A short one line description of your module",
"id": "companion-module-streamer.bot-api",
"name": "companion-module-streamer.bot-api",
"shortname": "Streamer.bot",
"description": "Streamer.bot API",
"version": "0.0.0",
"license": "MIT",
"repository": "git+https://github.com/bitfocus/companion-module-your-module-name.git",
"bugs": "https://github.com/bitfocus/companion-module-your-module-name/issues",
"repository": "git+https://github.com/RavenX8/companion-module-streamer.bot-api.git",
"bugs": "https://github.com/RavenX8/companion-module-streamer.bot-api.git/issues",
"maintainers": [
{
"name": "Your name",
"email": "Your email"
"name": "RavenX8",
"email": "redflames1003@gmail.com"
}
],
"runtime": {
@@ -20,7 +20,7 @@
"entrypoint": "../main.js"
},
"legacyIds": [],
"manufacturer": "Your company",
"products": ["Your product"],
"manufacturer": "Streamer.bot",
"products": ["Streamer.bot"],
"keywords": []
}

View File

@@ -1,6 +1,6 @@
const { combineRgb } = require('@companion-module/base')
import { combineRgb } from '@companion-module/base'
module.exports = async function (self) {
export default async function (self) {
self.setFeedbackDefinitions({
ChannelState: {
name: 'Example Feedback',

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)

View File

@@ -1,17 +1,19 @@
{
"name": "your-module-name",
"name": "stream.bot-api",
"version": "0.1.0",
"main": "main.js",
"type": "module",
"scripts": {
"format": "prettier -w ."
},
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/bitfocus/companion-module-your-module-name.git"
"url": "git+https://github.com/RavenX8/companion-module-streamer.bot-api.git"
},
"dependencies": {
"@companion-module/base": "~1.5.1"
"@companion-module/base": "~1.5.1",
"@streamerbot/client": "^1.3.1"
},
"devDependencies": {
"@companion-module/tools": "^1.4.1"

View File

@@ -1,4 +1,4 @@
module.exports = [
export default [
/*
* Place your upgrade scripts here
* Remember that once it has been added it cannot be removed!

View File

@@ -1,7 +1,24 @@
module.exports = async function (self) {
export default async function (self) {
self.setVariableDefinitions([
{ variableId: 'variable1', name: 'My first variable' },
{ variableId: 'variable2', name: 'My second variable' },
{ variableId: 'variable3', name: 'Another variable' },
{ variableId: 'viewerCount', name: 'Current Viewer Count' },
{ variableId: 'lastTwitchFollower', name: 'Last Twitch Follower' },
{ variableId: 'lastTwitchChatMessage', name: 'Last Twitch Chat Message' },
{ variableId: 'lastDonationSender', name: 'Last Streamlabs Donation Sender' },
{ variableId: 'lastDonationAmount', name: 'Last Streamlabs Donation amount' },
{ variableId: 'lastDonationFormattedAmount', name: 'Last Streamlabs Donation formatted amount' },
{ variableId: 'lastDonationCurrency', name: 'Last Streamlabs donation currency' },
{ variableId: 'lastDonationMessage', name: 'Last Streamlabs Donation Message' },
{ variableId: 'lastMerchandiseBuyerName', name: 'Last Streamlabs Merchandise Buyer Name' },
{ variableId: 'lastMerchandiseBuyerProduct', name: 'Last Streamlabs Bought Merchandise Product' },
{ variableId: 'lastMerchandiseBuyerMessage', name: 'Last Streamlabs Merchendise Buyer message' },
{ variableId: 'lastMerchandiseProductImageUrl', name: 'Last Streamlabs Bought Merchendise Image url' },
{ variableId: 'lastTipUsername', name: 'Last StreamElements Tip Username' },
{ variableId: 'lastTipAmount', name: 'Last StreamElements Tip Amount' },
{ variableId: 'lastTipCurrency', name: 'Last StreamElements Tip Currency' },
{ variableId: 'lastTipMessage', name: 'Last StreamElements Tip Message' },
{ variableId: 'lastYoutubeSubscriber', name: 'Last Youtube Subscriber' },
{ variableId: 'lastYoutubeChatMessage', name: 'Last Youtube Chat Message' },
{ variableId: 'lastYoutubeSuperChat', name: 'Last Youtube Super Chat' },
{ variableId: 'lastYoutubeSponsor', name: 'Last Youtube Sponsor' },
])
}