-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitch_key_control.js
112 lines (89 loc) · 2.96 KB
/
twitch_key_control.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
const prompt = require('prompt-sync')();
const tmi = require('tmi.js');
const robot = require("robotjs");
const configManager = require("./config");
const config = configManager.getConfig();
const minAmount = config.settings.minimum_amount;
const time = config.settings.time;
const delay = config.settings.delay;
const channelName = config.settings.channel ?? prompt(config.language.CHANNEL_NAME_PROMPT);
const client = new tmi.Client({
channels: [channelName]
});
const action = {
key: [],
mouse: [],
time: Number,
repeat: Number,
delay: Number,
cancel: Boolean
};
const actionMap = new Map();
robot.setKeyboardDelay(delay);
client.connect().then(() => console.log(config.language.CHAT_STATUS_CONNECTED.replace("{channel}", channelName)));
client?.on('message', (channel, tags, message) => {
const action = getByCommand(message);
if (!action) {
return;
}
actionMap.set(action, (actionMap.get(action) ?? 0) + 1);
setTimeout(() => {
actionMap.delete(action);
}, time);
if (actionMap.get(action) >= minAmount) {
const amount = actionMap.get(action);
console.log((amount > 1 ? config.language.ACTION_PERFORM_PLURAL : config.language.ACTION_PERFORM_SINGULAR)
.replace("{command}", action.command).replace("{amount}", amount));
repeatAction(() => {
if (action.key) {
if (action.time) {
action.key.forEach(action => {
robot.keyToggle(action, "down");
})
setTimeout(() => {
action.key.forEach(action => {
robot.keyToggle(action, "up");
})
}, action.time);
return;
}
action.key.forEach(action => {
robot.keyTap(action);
})
}
if (action.mouse) {
if (action.time) {
action.mouse.forEach(action => {
robot.mouseToggle("up", action);
})
setTimeout(() => {
action.mouse.forEach(action => {
robot.mouseToggle("down", action);
})
}, action.time);
return;
}
action.mouse.forEach(action => {
robot.mouseClick(action);
})
}
}, action.delay ?? 0, action.repeat ?? 1);
}
});
function getByCommand(command) {
for (const index in config.keys) {
if (config.keys[index].command === command) {
return config.keys[index];
}
}
return null;
}
function repeatAction(action, delay, repetitions) {
if (repetitions <= 0) {
return;
}
action();
setTimeout(function() {
repeatAction(action, delay, repetitions - 1);
}, delay);
}