forked from its-bede/twitch-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (94 loc) · 2.68 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
/**
* NodeJS Twitch get-request to twitch chat relay via tmi.js
*
* @author Benjamin Deutscher <[email protected]>
* @version 2.0.0
* @copyright 28.05.2021 Benjamin Deutscher
*/
console.log(`Lampe385 Twitch get-request to twitch chat relay (NodeJS ${process.version})`);
let tmi, client;
const fs = require('fs');
let config;
require('log-timestamp');
try {
tmi = require('tmi.js');
config = require("./config");
} catch (error) {
console.log("Requirements missing! Try: npm install");
console.log(error);
process.exit(1);
}
// ---------------------------
// Setup
// ---------------------------
client = new tmi.Client({
options: {
debug: false
},
connection: {
reconnect: true,
secure: true
},
identity: {
username: config.username,
password: config.token
},
channels: config.channels
});
client.connect();
// Twitch CLIENT EVENTS
client.on('connected', (address, port) => {
console.log(`Connected: ${address}:${port}`);
});
client.on('join', (channel, username, self) => {
if (self) console.log(`${username} joined ${channel}`);
});
client.on('notice', (channel, msgid, message) => {
console.log(`notice: [${channel}] ${msgid} => ${message}`);
});
client.on('message', (channel, userstate, message, self) => {
if (self) return;
console.log(`<${userstate['display-name']}> ${message}`);
});
process.on('SIGINT', function () {
client.disconnect();
console.log("SIGINT");
process.exit();
});
// LOCAL WEBSERVER SETUP
const express = require('express');
const app = express();
// on the request to root (localhost:5555/)
app.get('/', function (req, res) {
res.send('<b>My</b> first express http server');
});
// start the server in the port 5555 !
app.listen(5555, function () {
console.log('Twitch get-request to chat relay listening on port 5555.');
});
// Setup dynamic routes like
// http://localhost:5555/CHANNELNAME/COMMANDNAME
try {
const data = fs.readFileSync('./commands.json', 'utf8');
const cmd_conf = JSON.parse(data);
console.log(" Available Commands:")
for (const [channel, commands] of Object.entries(cmd_conf[0])) {
console.log(` Channel: ${channel.padStart(19, ' ')}`);
console.log("========================================");
commands.forEach(cmd => {
console.log(`${cmd.name.padStart(20, ' ')}: ${cmd.response}`);
app.get(`/${channel}/${cmd.name}`, function (req, res) {
client.say(channel, `${cmd.response}`);
res.send('done');
});
})
console.log("========================================");
}
} catch (err) {
console.log(`Error reading file from disk: ${err}`);
}
// Change the 404 message modifing the middleware
app.use(function (req, res, next) {
res.status(404).send("Sorry, that route doesn't exist.");
});
// EOF