-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
120 lines (106 loc) · 2.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const fetch = require('node-fetch');
const { log } = require('deckboard-kit');
class StreamerBot {
constructor() {
this.name = 'Streamer.bot';
this.platforms = ['WINDOWS', 'LINUX'];
this.configs = {
address: {
type: 'text',
name: 'Address',
descriptions: 'Example: 127.0.0.1',
value: ''
},
port: {
type: 'text',
name: 'Port',
descriptions: 'Example: 7474',
value: ''
}
};
this.inputs = [
{
label: 'Execute Action',
value: 'streamerbot-action',
icon: 'robot',
color: '#5b00a0',
input: [
{
label: 'Action',
ref: 'actionId',
type: 'input:autocomplete'
},
{
label: 'Arguments',
ref: 'args',
hint: 'In JSON format. ex: {"fiz": "buzz"}',
type: 'input:text'
}
]
}
];
this.initExtension();
}
get selections() {
return [
{
header: this.name
},
...this.inputs
];
}
// Executes when the extensions loaded every time the app start.
initExtension() {}
getAutocompleteOptions(ref) {
switch (ref) {
case 'actionId':
return this.fetchStreamerBotActions();
default:
return [];
}
}
async fetchStreamerBotActions() {
try {
const { address, port } = this.configs;
const url = `http://${address.value}:${port.value}/GetActions`;
const response = await fetch(url, { method: 'GET' });
const data = await response.json();
return data.actions.map(({ id, name }) => ({
value: id,
label: name,
}));
} catch (err) {
log.error('fetchStreamerBotActions', err);
throw err;
}
}
async doStreamerBotAction(id, args) {
try {
const { address, port } = this.configs;
const url = `http://${address.value}:${port.value}/DoAction`;
if (!!args && typeof args === 'string') args = JSON.parse(args);
else args = {};
const body = JSON.stringify({
action: { id },
args,
});
const response = await fetch(url, { method: 'POST', body });
const data = await response.json();
log.info('doStreamerBotAction', data);
} catch (err) {
log.error('doStreamerBotAction', err);
throw err;
}
}
execute(action, data) {
switch (action) {
case 'streamerbot-action':
const { actionId, args } = data;
this.doStreamerBotAction(actionId, args);
break;
default:
break;
}
}
}
module.exports = (sendData) => new StreamerBot(sendData);