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) 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({ self.setActionDefinitions({
sample_action: { runAction: {
name: 'My First Action', name: 'Run Action',
options: [ options: [
{ {
id: 'num', id: 'action',
type: 'number', type: 'dropdown',
label: 'Test', label: 'Action',
default: 5, default: '',
min: 0, choices: self.actions.actions.map((value) => ({ label: value.name, ...value })),
max: 100,
}, },
], ],
callback: async (event) => { 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", "id": "companion-module-streamer.bot-api",
"name": "your-module-name", "name": "companion-module-streamer.bot-api",
"shortname": "module-shortname", "shortname": "Streamer.bot",
"description": "A short one line description of your module", "description": "Streamer.bot API",
"version": "0.0.0", "version": "0.0.0",
"license": "MIT", "license": "MIT",
"repository": "git+https://github.com/bitfocus/companion-module-your-module-name.git", "repository": "git+https://github.com/RavenX8/companion-module-streamer.bot-api.git",
"bugs": "https://github.com/bitfocus/companion-module-your-module-name/issues", "bugs": "https://github.com/RavenX8/companion-module-streamer.bot-api.git/issues",
"maintainers": [ "maintainers": [
{ {
"name": "Your name", "name": "RavenX8",
"email": "Your email" "email": "redflames1003@gmail.com"
} }
], ],
"runtime": { "runtime": {
@@ -20,7 +20,7 @@
"entrypoint": "../main.js" "entrypoint": "../main.js"
}, },
"legacyIds": [], "legacyIds": [],
"manufacturer": "Your company", "manufacturer": "Streamer.bot",
"products": ["Your product"], "products": ["Streamer.bot"],
"keywords": [] "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({ self.setFeedbackDefinitions({
ChannelState: { ChannelState: {
name: 'Example Feedback', name: 'Example Feedback',

131
main.js
View File

@@ -1,26 +1,95 @@
const { InstanceBase, Regex, runEntrypoint, InstanceStatus } = require('@companion-module/base') import { InstanceBase, Regex, runEntrypoint, InstanceStatus } from '@companion-module/base'
const UpgradeScripts = require('./upgrades') import UpgradeScripts from './upgrades.js'
const UpdateActions = require('./actions') import UpdateActions from './actions.js'
const UpdateFeedbacks = require('./feedbacks') import UpdateFeedbacks from './feedbacks.js'
const UpdateVariableDefinitions = require('./variables') import UpdateVariableDefinitions from './variables.js'
import { StreamerbotClient } from '@streamerbot/client'
class ModuleInstance extends InstanceBase { class ModuleInstance extends InstanceBase {
constructor(internal) { constructor(internal) {
super(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) { async init(config) {
this.config = 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 await this.client.getActions().then((value) => {
this.updateFeedbacks() // export feedbacks this.actions = value
this.updateVariableDefinitions() // export variable definitions })
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 // When module gets deleted
async destroy() { async destroy() {
this.log('debug', 'destroy') this.log('debug', 'destroy')
clearInterval(this.intervalId)
await this.client.unsubscribe('*')
await this.client.disconnect()
} }
async configUpdated(config) { async configUpdated(config) {
@@ -31,18 +100,27 @@ class ModuleInstance extends InstanceBase {
getConfigFields() { getConfigFields() {
return [ return [
{ {
type: 'textinput',
id: 'host', id: 'host',
type: 'textinput',
label: 'Target IP', label: 'Target IP',
width: 8, tooltip: 'Address of the Streamer.bot WebSocket Server. Default localhost',
regex: Regex.IP, default: 'localhost',
}, },
{ {
type: 'textinput',
id: 'port', id: 'port',
type: 'textinput',
label: 'Target Port', label: 'Target Port',
width: 4, width: 5,
regex: Regex.PORT, 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() {
UpdateVariableDefinitions(this) 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) runEntrypoint(ModuleInstance, UpgradeScripts)

View File

@@ -1,17 +1,19 @@
{ {
"name": "your-module-name", "name": "stream.bot-api",
"version": "0.1.0", "version": "0.1.0",
"main": "main.js", "main": "main.js",
"type": "module",
"scripts": { "scripts": {
"format": "prettier -w ." "format": "prettier -w ."
}, },
"license": "MIT", "license": "MIT",
"repository": { "repository": {
"type": "git", "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": { "dependencies": {
"@companion-module/base": "~1.5.1" "@companion-module/base": "~1.5.1",
"@streamerbot/client": "^1.3.1"
}, },
"devDependencies": { "devDependencies": {
"@companion-module/tools": "^1.4.1" "@companion-module/tools": "^1.4.1"

View File

@@ -1,4 +1,4 @@
module.exports = [ export default [
/* /*
* Place your upgrade scripts here * Place your upgrade scripts here
* Remember that once it has been added it cannot be removed! * 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([ self.setVariableDefinitions([
{ variableId: 'variable1', name: 'My first variable' }, { variableId: 'viewerCount', name: 'Current Viewer Count' },
{ variableId: 'variable2', name: 'My second variable' }, { variableId: 'lastTwitchFollower', name: 'Last Twitch Follower' },
{ variableId: 'variable3', name: 'Another variable' }, { 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' },
]) ])
} }