-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
108 lines (89 loc) · 2.86 KB
/
logger.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
const WebSocket = require('ws')
const ReconnectingWebSocket = require('reconnecting-websocket')
const fs = require('fs')
const config = require('./src/config.json')
// check if logs folder exists.
// if not, create logs folder and optedin.json file
// if it does, create array of files inside logs folder
try {
if (!(fs.existsSync('./logs/'))) {
fs.mkdirSync('./logs/')
fs.appendFileSync('./logs/optedin.json', '{"users":[]}')
}
} catch (e) {
console.error(e)
}
//
// Optedin.json event watching
//
var optedIN = {
users: JSON.parse(fs.readFileSync('./logs/optedin.json')).users,
eval: function() { try {
fs.readFile('./logs/optedin.json', (err, data) => {
this.users = JSON.parse(data).users
})
} catch (e) { console.error(e) }
}
}
optedIN.eval()
fs.watch('./logs/optedin.json', (event, filename) => {
if (event === 'change') {
optedIN.eval()
}
})
//
// Log Writing
//
const writeLog = (name, message) => {
var fileLocation = `./logs/${name}.txt`
fs.readFile(fileLocation, 'utf8', (err, data) => {
var lines = data.split('\r\n').filter(Boolean)
var newMessage = `${message.timestamp} ${message.nick}: ${message.data}`
if (lines.length === config.loglimit) {
lines.shift()
lines.push(newMessage)
} else if (lines.length < config.loglimit) {
lines.push(newMessage)
}
fs.writeFile(fileLocation, lines.join('\r\n'), (err) => {
(err) ? console.error(err) : null
})
})
}
//
// WebSocket
//
const rws = new ReconnectingWebSocket('wss://chat.strims.gg/ws', [], { WebSocket: WebSocket, connectionTimeout: 20000 });
rws.addEventListener('open', () => {
let time = new Date().toLocaleString()
console.log(`[${time}] LOGGER CONNECTED`)
})
rws.addEventListener('message', (e) => {
// Example of e.data
// JOIN {"nick":"Fatal","features":[],"timestamp":1577117797198}
const WebSocketMessagePrefix = e.data.split(' ', 1).toString()
const WebSocketMessage = e.data
// Creating JSON object with the websocket message minus the 'NAMES', 'JOIN', 'LEAVE', 'MSG', 'PRIVMSG' before the array
const message = JSON.parse(WebSocketMessage.substr(WebSocketMessagePrefix.length + 1))
// Adding new JSON key and value for type of message sent, example: 'NAMES', 'JOIN', 'LEAVE', 'MSG', 'PRIVMSG'
message.type = WebSocketMessagePrefix
if (message.type === 'MSG') {
optedIN.users.some(name => {
if (String(message.data).toLowerCase().includes(name.toLowerCase())) {
fs.exists(`./logs/${name}.txt`, (bool) => {
if (bool) {
writeLog(name, message)
} else if (!bool) {
fs.appendFileSync(`./logs/${name}.txt`, '')
writeLog(name, message)
}
})
}
})
}
})
rws.addEventListener('close', () => {
rws.reconnect()
let time = new Date().toLocaleString()
console.log(`[${time}] LOGGER DISCONNECTED`)
})