-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
141 lines (126 loc) · 5.04 KB
/
server.js
File metadata and controls
141 lines (126 loc) · 5.04 KB
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
'use strict';
const SERVERVARS = require('./config/server-vars');
/** BEGIN Twitch Server **/
const USERNAME = SERVERVARS.username();
//https://twitchapps.com/tmi/ < go here to make a token.
const PASSWORD = SERVERVARS.password();
const CHANNEL = SERVERVARS.channel();
const IRC = require('slate-irc');
const NET = require('net');
const HTTP = require('http');
const FS = require('fs');
function pong(){
return function(IRC){
IRC.on('data', function(msg){
if ('PING' != msg.command) return;
IRC.write('PONG :' + msg.trailing);
});
}
}
let commandPermissions = {
profile: true,
uptime: true,
futureBuilds: true,
onCooldown: false,
cooldownSeconds: 5
}
const stream = NET.connect({
port: 6667,
host: 'IRC.twitch.tv'
});
let client = IRC(stream);
client.use(pong());
const moment = require('moment');
let startTime = moment();
console.log('*****');
console.log("Logging into Twitch. . .");
client.pass(PASSWORD);
client.nick(USERNAME);
client.user(USERNAME, USERNAME);
client.join(CHANNEL);
client.send(CHANNEL, 'Andybot reporting for duty!');
console.log("Successfully logged in at " + startTime.format('HH:mm a') + '.');
console.log('*****');
//add your commands here
client.on('data', function(msg){
let words = msg.trailing.split(" ");
if (words.length > 0 && !commandPermissions.onCooldown){
var currentTime = moment();
if (words[0] === "!profile" && commandPermissions.profile){
commandPermissions.onCooldown = true;
client.send(CHANNEL, "https://www.pathofexile.com/account/view-profile/sketchspace/characters");
console.log("<" + currentTime.format("HH:mm") + ">POE Profile requested.");
setTimeout(function(){
console.log("<" + currentTime.format("HH:mm") + ">Commands are off cooldown.");
commandPermissions.onCooldown = false;
}, commandPermissions.cooldownSeconds * 1000);
} else if (words[0] === "!uptime" && commandPermissions.uptime){
commandPermissions.onCooldown = true;
client.send(CHANNEL, "Stream started at: " + startTime.format('HH:mm a') + ".");
console.log("<" + currentTime.format("HH:mm") + ">Uptime requested.");
setTimeout(function(){
console.log("<" + currentTime.format("HH:mm") + ">Commands are off cooldown.");
commandPermissions.onCooldown = false;
}, commandPermissions.cooldownSeconds * 1000);
}else if (words[0] === "!futurebuilds" && commandPermissions.futureBuilds){
commandPermissions.onCooldown = true;
client.send(CHANNEL, "Future Builds for Legacy > https://pastebin.com/LEAFq6pu");
console.log("<" + currentTime.format("HH:mm") + ">Future Builds requested.");
setTimeout(function(){
console.log("<" + currentTime.format("HH:mm") + ">Commands are off cooldown.");
commandPermissions.onCooldown = false;
}, commandPermissions.cooldownSeconds * 1000);
}
}
});
/** END Twitch Server **/
/** BEGIN Server Setup **/
let server = HTTP.createServer( function(req, res) {
res.setHeader('Access-Control-Allow-Origin', "http://localhost");
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'POST') {
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = JSON.parse(body);
console.log("[" + moment().format("HH:mm") + "] POST received.");
if (body['profileState'] === "Off"){
console.log("!profile command turned Off.");
commandPermissions.profile = false;
} else if (body['profileState'] === "On"){
console.log("!profile command turned On.");
commandPermissions.profile = true;
} else if (body['uptimeState'] === "Off"){
console.log("!uptime command turned Off.");
commandPermissions.uptime = false;
} else if (body['uptimeState'] === "On"){
console.log("!uptime command turned On.");
commandPermissions.uptime = true;
} else if (body['futureBuildsState'] === "Off"){
console.log("!futureBuilds command turned Off.");
commandPermissions.futureBuilds = false;
} else if (body['futureBuildsState'] === "On"){
console.log("!futureBuilds command turned On.");
commandPermissions.futureBuilds = true;
}
});
req.on('error', (err) => {
console.log('[' + moment().format("HH:mm") + '] Error during received POST: ' + err.stack);
});
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('post received');
}
});
let port = 3000;
let host = '127.0.0.1';
server.listen(port, host);
console.log('*****');
console.log('Starting up control-command server. . .');
console.log('Control-command server listening at HTTP://' + host + ':' + port + " at " + moment().format('HH:mm a') + '.');
console.log('*****');
/** END Server Setup **/
console.log('*****');
console.log('[] denote server messages, <> denote chat messages.');
console.log('*****');