-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadListener.js
More file actions
190 lines (158 loc) · 5.11 KB
/
ThreadListener.js
File metadata and controls
190 lines (158 loc) · 5.11 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
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
const fs = require("fs");
const EventEmitter = require('events');
let monitoredThreadInfo = {};
let outputFileName = '';
let fileOutput = false;
let currentThread = '';
class Emitter extends EventEmitter {}
let myEmitter = new Emitter();
function start(options, api) {
let threadsToProcess = [];
api.setOptions({
listenEvents: true,
selfListen: true, //also lets you listen to your own messages or events
logLevel: "silent",
updatePresence: true
});
options.forEach(function (val, index, array) {
switch (val) {
case "-t":
case "--thread":
while (++index < array.length && !array[index].includes("-")) {
threadsToProcess.push(array[index]);
}
break;
case "-dt":
case "--default-thread":
threadsToProcess.push(process.env.defaultListenThreadName);
break;
case "--set-default":
fs.readFile(".env", "utf-8", function (err, data) {
let splitArray = data.split("\n");
splitArray.splice(splitArray.indexOf("defaultListenThreadName"), 1);
splitArray[splitArray.length] = `defaultListenThreadName=${array[++index]}`
fs.writeFile(".env", splitArray.join("\n"), (err) => { });
});
break;
case "-f":
case "--file":
outputFileName = array[++index];
if (outputFileName === undefined) {
console.log('No output file specified');
process.exit(0);
}
fileOutput = true;
fs.writeFile(outputFileName, '[\n', function (err) {
if (err) {
console.log('Unable to prepare file');
process.exit(0);
}
});
console.log(`Writing received events to ${outputFileName}`);
break;
case "-h":
console.log("Usage: node lol.js listen");
console.log("\t-f --file\t\tName of the file that all events will be written to");
console.log("\t-dt --default-thread\tMonitor your designated default thread");
console.log("\t--set-default\t\tName of the thread that will be monitor by default if no thread name is provided");
console.log("\t-t --thread\t\tName of threads that you would like to monitor");
console.log("\t-h --help\t\tDisplay this help dialog");
console.log();
process.exit(0);
}
});
if (threadsToProcess == 0) {
threadsToProcess[0] = process.env.defaultThumbThreadName;
}
console.log("Listening to threads:");
threadsToProcess.forEach(function (val) {
console.log(`\t${val}`);
});
api.getThreadList(10, null, [], (err, list) => {
if (err) { return console.error(err); }
list.forEach(async function (thread) {
if (threadsToProcess.includes(thread.name)) {
currentThread = thread.threadID;
monitoredThreadInfo[thread.threadID] =
{
threadId: thread.threadID,
threadName: thread.name,
members: {}
};
thread.participants.forEach(function (threadParticipantData) {
monitoredThreadInfo[thread.threadID]["members"][threadParticipantData.userID] = JSON.parse(`{"name": "${threadParticipantData.name}"}`);
});
}
});
myEmitter.emit("ready", currentThread, monitoredThreadInfo);
let stopListening = api.listen((err, event) => {
if (err) { return console.error(err); }
if (monitoredThreadInfo[event.threadID] !== undefined) {
switch (event.type) {
case "message":
handleMessage(event);
break;
case "typ":
handleThreadMemberType(event);
break;
case "read":
handleThreadReadEvent(event);
break;
case "read_receipt":
handleReadReceipt(event);
break;
case "message_reaction":
handleMessageReaction(event);
break;
case "presence":
handlePresence(event);
break;
}
if (fileOutput) {
writeEventToFile(event);
}
}
});
});
}
function getCurrentThread() {
return currentThread;
}
function threadInfo() {
return monitoredThreadInfo;
}
function handleMessage(event) {
if (monitoredThreadInfo[event.threadID]["members"][event.senderID].name === monitoredThreadInfo[event.threadID].threadName) {
console.log(`${monitoredThreadInfo[event.threadID]["members"][event.senderID].name}: ${event.body !== "" ? event.body : "Unsupported Message Type"}`)
} else {
console.log(`${monitoredThreadInfo[event.threadID]["members"][event.senderID].name}->${monitoredThreadInfo[event.threadID].threadName}: ${event.body !== "" ? event.body : "Unsupported Message Type"}`)
}
}
function handleThreadMemberType(event) { }
function handleThreadReadEvent(event) { }
function handleReadReceipt(event) { }
function handleMessageReaction(event) {
console.log(`${monitoredThreadInfo[event.threadID]["members"][event.senderID].name} reacted to your message with ${event.reaction}`);
}
function handlePresence(event) {
api.getUserInfo(event.userID, (err, user) => {
console.log(`${user.name} is active`);
});
}
function writeEventToFile(event) {
fs.appendFile(outputFileName, JSON.stringify(event, '', '\t') + ',', function (err) {
if (err) { console.err('Unable to write event to file'); }
});
}
process.on('SIGINT', function () {
console.log('\nClosing...');
process.exit();
});
module.exports = {
start: start,
getCurrentThread: getCurrentThread,
threadInfo: threadInfo,
on: function(threadId, threadInfo) {
myEmitter.on.apply(myEmitter, arguments);
}
}