-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
169 lines (143 loc) · 4.68 KB
/
bot.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
const fs = require('fs');
// from ./tools/
const format_timestamp = require('./src/tools/format_time')
const log_link = require('./src/tools/log_link.js')
// from ./commands/
const clear_log = require('./src/commands/clear.js')
const opting = require('./src/commands/opting.js')
const help = require('./src/commands/help.js')
// Config
const config = JSON.parse(fs.readFileSync('./src/config.json'))
const JWT_TOKEN = config.jwt_token
// WebSocket
const WebSocket = require('ws');
const ReconnectingWebSocket = require('reconnecting-websocket')
//
// File Management
//
// 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) }
// Users Directory Object
var logDirectory = {
Array: [],
eval: function () {
var usersDirectory = fs.readdirSync('./logs/')
for (i in usersDirectory) {
// this skips an iteration of the loop if it is the optedin.json file
if (usersDirectory[i] === 'optedin.json') { continue }
// adds
this.Array.push(usersDirectory[i].split('.')[0])
}
}
}
logDirectory.eval()
//
// JSON
//
var optedIn = {
JSON: JSON.parse(fs.readFileSync('./logs/optedin.json').toString()),
eval: () => {
this.JSON = JSON.parse(fs.readFileSync('./logs/optedin.json').toString())
}
}
//
// WebSocket
//
const RWSoptions = {
WebSocket: WebSocket,
connectionTimeout: 20000
}
const rws = new ReconnectingWebSocket('wss://chat.strims.gg/ws', { headers: { Cookie: `;jwt=${JWT_TOKEN}` } }, RWSoptions);
// Chat tools object
var chatTools = {
sendChatMessage: (message) => {
rws.send(`MSG {"data":"${message}"}`)
},
sendPrivateMessage: (username, message) => {
rws.send(`PRIVMSG {"nick":"${username}", "data":"${message}"}`)
}
}
// On WebSocket Connect
rws.addEventListener('open', () => {
let time = new Date().toLocaleString()
console.log(`[${time}] BOT CONNECTED`)
})
// On WebSocket Messaged Received
rws.addEventListener('message', (e) => {
try {
// 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' && message.data.startsWith('!mentions')) {
args = message.data.split(' ')
if (args.length === 1) {
if (logDirectory.Array.includes(message.nick)) {
log_link.grabLog(message.nick, chatTools)
} else if (!(optedIn.JSON["users"].includes(message.nick))) {
chatTools.sendPrivateMessage(message.nick, 'You do not have a file. If you would like to be logged type `/w mentions help` to learn more about it.')
}
}
switch (args[1]) {
case "enable":
opting.enable(message.nick, optedIn.JSON, chatTools)
optedIn.eval()
logDirectory.eval()
break;
case "disable":
opting.disable(message.nick, optedIn.JSON, chatTools)
optedIn.eval()
logDirectory.eval()
break;
case "help":
help.send(message, chatTools)
break;
default:
log_link.grabLog(message.nick, chatTools)
}
}
if (message.type === 'PRIVMSG') {
args = message.data.split(' ')
switch (args[0]) {
case "enable":
opting.enable(message.nick, optedIn.JSON, chatTools)
optedIn.eval()
logDirectory.eval()
break;
case "disable":
opting.disable(message.nick, optedIn.JSON, chatTools)
optedIn.eval()
logDirectory.eval()
break;
case "clear":
clear_log.clear(message.nick, chatTools)
break;
case "list":
log_link.grabLog(message.nick, chatTools)
break;
case "help":
help.send(message, chatTools)
break;
default:
chatTools.sendPrivateMessage(message.nick, "Unknown command. Type `/w mentions help` to learn more.")
}
}
} catch (e) { console.log(e) }
})
// On Websocket Disconnect
rws.addEventListener('close', () => {
rws.reconnect()
let time = new Date().toLocaleString()
console.log(`[${time}] BOT DISCONNECTED`)
})