-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
173 lines (147 loc) · 4.76 KB
/
server.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require("dotenv").config();
const axios = require('axios');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
const fs = require("fs");
const tmi = require("tmi.js");
// Define configuration options
const opts = {
channels: process.env.CHANNELS.split(",").map(s => s.trim()),
admins: process.env.ADMINS.split(",").map(s => s.trim().toLowerCase()),
speakers: process.env.SPEAKERS.split(",").map(s => s.trim().toLowerCase()),
webhook: process.env.WEBHOOK,
webhook_content: process.env.WEBHOOK_CONTENT
};
let board = {
commands: [],
messages: [],
notifications: [],
sound_strings: [],
message_sound: true,
tts: true
}
const express = require("express");
const app = express();
const port = 3003;
console.log(`
# ### ### ###
## # # #
# ## ## # ### ### ### ###
# # # # # # # # ##
### ### ### # ### ### # ###
Starting Twitch Soundboard!
* Change channel name in the example.env file and rename it to .env
* Insert browser window with URL: http://localhost:${port} to OBS
* Upload your *.mp4 and *.mp3s to /pulic/files/*
* Files will be shown with "!filename" in twitch chat
`);
if (opts.webhook) {
readline.question(`Do you want to trigger your configured webhook? (y/yes)\n`, result => {
if (result === "yes" || result === "y") {
// trigger webhook
axios.post(opts.webhook, {
content: opts.webhook_content
}).then(() => {
console.log(`Webhook triggered!`);
}).catch(err => console.warn(err));
}
readline.close();
});
}
app.use(express.static("public"));
// Build a soundstrings to pass to your frontend
fs.readdir("./public/files/", (err, files) => {
if (err) {
throw err;
}
files.forEach(file => {
if (file.indexOf(".mp4") >= 0 || file.indexOf(".mp3") >= 0) {
board.sound_strings.push({
command: "!" + file.toLocaleLowerCase().split(".")[0],
media: file.toLocaleLowerCase()
});
}
});
});
// Endpoint for the frontend that gets all the twitch messages and commands
app.get('/board', (req, res) => {
res.send(JSON.stringify(board));
board.commands = [];
board.messages = [];
board.notifications = [];
});
// Create a twitch client with our options
const client = new tmi.client(opts);
// Register our event handlers (defined below)
client.on("message", onMessageHandler);
client.on("connected", onConnectedHandler);
// Connect to Twitch:
client.connect();
// Called every time a message comes in
function onMessageHandler(target, context, msg, self) {
if (self) { return; } // Ignore messages from the bot
// Remove whitespace from chat message
const twitch_message = msg.trim();
// if message is mentioning someone else, dont read it
for (let channel of opts.channels) {
if (twitch_message.indexOf("@") == 0
&& twitch_message.indexOf("@" + channel) == -1) {
return;
}
}
// admin commands
if (opts.admins.includes(context.username)) {
switch (twitch_message.toLowerCase()) {
case "!soundoff":
board.message_sound = false
break;
case "!soundon":
board.message_sound = true
case "!ttsoff":
board.tts = false
break;
case "!ttson":
board.tts = true
break;
case "!mute":
board.mute = true
break;
case "!unmute":
board.mute = false
break;
default:
break;
}
}
// mute all sound
if (board.mute) {
return;
}
// read commands with !
if (twitch_message.indexOf("!") === 0) {
board.commands.push(twitch_message.toLowerCase());
} else {
// read messages without !
if (board.message_sound) {
board.notifications.push(twitch_message.toLowerCase());
}
// read message with TTS if in speakers or subscriber
if (board.tts) {
if (opts.speakers.indexOf(context.username.toLowerCase()) >= 0) {
board.messages.push(twitch_message.toLowerCase());
} else if (context.subscriber === true) {
board.messages.push(twitch_message.toLowerCase());
}
}
}
}
// Called every time the bot connects to Twitch chat
function onConnectedHandler(addr, port) {
console.log(`* Connected to twitch chat on ${addr}:${port}`);
}
// Start the browser window server
app.listen(port, () => {
console.log(`Soundboard visuals are listening at http://localhost:${port}`)
})