diff --git a/README.md b/README.md index 6746243..2856175 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# companion-module-[replace with module name] +# companion-module-stream.bot-api See [HELP.md](./companion/HELP.md) and [LICENSE](./LICENSE) diff --git a/actions.js b/actions.js index b9da0f7..09bc6d4 100644 --- a/actions.js +++ b/actions.js @@ -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() }, }, }) diff --git a/companion/manifest.json b/companion/manifest.json index ad77bef..71ffdd3 100644 --- a/companion/manifest.json +++ b/companion/manifest.json @@ -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": [] } diff --git a/feedbacks.js b/feedbacks.js index 1f331c7..4977982 100644 --- a/feedbacks.js +++ b/feedbacks.js @@ -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', diff --git a/main.js b/main.js index 42485cc..4575505 100644 --- a/main.js +++ b/main.js @@ -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) diff --git a/package.json b/package.json index 656f6cb..5231c7d 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/upgrades.js b/upgrades.js index d9d6472..006a02f 100644 --- a/upgrades.js +++ b/upgrades.js @@ -1,4 +1,4 @@ -module.exports = [ +export default [ /* * Place your upgrade scripts here * Remember that once it has been added it cannot be removed! diff --git a/variables.js b/variables.js index a4c9f8c..2c6d540 100644 --- a/variables.js +++ b/variables.js @@ -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' }, ]) }