-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
194 lines (159 loc) · 4.3 KB
/
app.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const fs = require("fs");
const touch = require("touch");
const path = require("path");
const tmi = require("tmi.js");
let { PythonShell } = require("python-shell");
const configFolder = "config";
const handlerScript = path.join(__dirname, "scripts/directkeys2.py");
var scriptString = fs.readFileSync(handlerScript, "utf-8");
var keymapContent = require("./scripts/keycodes.json"); // Read with require so it can be packaged
var configPath = path.join(configFolder, "user.json");
const defaultConfig = {
config: {
botname: "TwitchPlays",
channel: "icrazyblazelive",
key: "oauth:yourkeyhere",
prefix: "!"
},
names: {
w: ["DIK_W:0.5"],
a: ["DIK_A:0.5"],
s: ["DIK_S:0.5"],
d: ["DIK_D:0.5"],
shoot: ["DIK_LCONTROL:0"],
use: ["DIK_SPACE:0"],
map: ["DIK_TAB:0"],
run: ["DIK_LSHIFT:2", "DIK_W:2"],
1: ["DIK_1:0"],
2: ["DIK_2:0"],
3: ["DIK_3:0"],
4: ["DIK_4:0"],
5: ["DIK_5:0"],
6: ["DIK_6:0"],
7: ["DIK_7:0"],
8: ["DIK_8:0"],
9: ["DIK_9:0"]
}
};
var isAcceptingCommands = true;
// Attempt to load JSON files
try {
var configFile = fs.readFileSync(configPath);
console.log("Configuration loaded!");
} catch (error) {
console.log(
"Could not find configuration file. The file has now been created. Please edit it and re-run the program!"
);
if (!fs.existsSync(configFolder)) {
fs.mkdirSync(configFolder);
}
touch(configPath);
let configData = JSON.stringify(defaultConfig, null, 2);
fs.writeFileSync(configPath, configData);
return;
}
// Get JSON content
var configContent = JSON.parse(configFile);
// Set variables from JSON
var botname = configContent.botname;
var key = configContent.key;
var channel = configContent.channel;
var prefix = configContent.prefix;
const client = new tmi.Client({
options: { debug: false },
connection: {
reconnect: true,
secure: true
},
identity: {
username: botname,
password: key
},
channels: [channel]
});
client.connect();
client.on("message", (channel, tags, message, self) => {
if (self) return;
if (message.startsWith(prefix)) {
if (tags.badges.broadcaster == 1 || tags.badges.moderator == 1) {
var returnvalue = parseTwitchCommand(message.substring(prefix.length));
if (returnvalue != null) {
client.say(channel, returnvalue);
}
}
}
if (!isAcceptingCommands) return;
for (var id in configContent.names) {
var id_value = configContent.names[id];
if (message.toUpperCase() == id.toUpperCase()) {
for (idv in id_value) {
value = id_value[idv];
// id value is an array
// Get hold time
var holdtime = value.split(/:(.+)/)[1]; // split by :
if (holdtime == null) {
holdtime = 0;
}
var id_stripped = value.substring(
0,
value.length - holdtime.toString().length - 1
);
var sentKey = sendKey(id_stripped, holdtime);
if (sentKey) {
// Display command
console.log(tags["display-name"] + ": " + message);
}
}
}
}
});
function parseTwitchCommand(cmd) {
// Twitch chat commands will be added later
console.log("Got moderator command: " + cmd);
if (cmd == "togglelock") {
isAcceptingCommands = !isAcceptingCommands;
if (isAcceptingCommands) {
return "Now accepting commands.";
} else {
return "Stopped accepting commands.";
}
}
if (cmd == "quitgame") {
// Quit function (Chocolate DOOM)
if (isAcceptingCommands) {
parseTwitchCommand("togglelock");
}
setTimeout(function() {
sendKey("DIK_ESCAPE", 0.1);
sendKey("DIK_UP", 0.1);
sendKey("DIK_RETURN", 0.1);
sendKey("DIK_Y", 0.1);
return "Quit the game.";
}, 1000);
}
}
function sendKey(keycode, holdtime) {
// Convert DIK code to hex Scancode using keymap
for (var sendCode in keymapContent.codes) {
var code_value = keymapContent.codes[sendCode];
if (keycode == sendCode) {
return runPython(code_value, holdtime);
}
}
return false;
}
function runPython(opt, sec) {
var options = {
mode: "text",
pythonPath: "python",
args: [opt, sec]
};
PythonShell.runString(scriptString, options, function(err) {
if (err) {
console.log(err);
return false;
} else {
return true;
}
});
}