-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogEngine.js
156 lines (126 loc) · 3.43 KB
/
logEngine.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
const ds = require("./main.js")
const fs = require("fs");
//create buffer for log to shoot to
let logBuffer = {
meta: {
users: {},
userindex: [],
servers: [],
channels: {}
},
data: {}
}
let date = new Date();
let filename = "log" + date.getUTCMonth() + "-" + date.getUTCDate() + "-" + date.getUTCHours() + ".txt"
ds.bot.on("message", async function(message){
let time = new Date().getTime() ;
let object = { meta: {}, data: {}
}
let authorid = message.author.id
let authorName = message.author.username
let guildName = message.guild.name
let channelid = message.channel.id
let channelName = message.channel.name
let messageContent = message.content
let messageid = message.id
//if the user is found set a flag to not push the user again
let userFound = false;
logBuffer.meta.userindex.forEach( id => {
if(authorid == id){
userFound = true;
}
})
if(!userFound){
logBuffer.meta.userindex.push(authorid)
}
logBuffer.meta.users[authorid] = {
name: authorName
}
//if the server is found set a flag to not push the user again
let serverFound = false;
logBuffer.meta.servers.forEach( item => {
if(item.name == guildName){
serverFound = true;
}
})
if(!serverFound){
logBuffer.meta.servers.push({
name: guildName,
type: "SERVER"
})
}
let serverIndex = 0;
logBuffer.meta.servers.forEach( (server, index) =>{
if(server.name == guildName){
serverIndex = index;
}
})
logBuffer.meta.channels[channelid] = {
server: serverIndex,
name: channelName
}
//find user messaging
let userindex = 0
logBuffer.meta.userindex.forEach( (id, index) => {
if(id == authorid){
userindex = index;
}
})
let newMessageObject = [{}]
newMessageObject[0][messageid] = {
u: userindex,
t: time,
m: messageContent
}
if(logBuffer.data[channelid] == undefined){
logBuffer.data[channelid] = newMessageObject[0]
console.log("new Section made!")
return;
}
let channelBuffer = {};
channelBuffer[channelid] = logBuffer.data[channelid];
let currentString = JSON.stringify(newMessageObject[0])
currentString = currentString.slice(1,currentString.length-1);
let savedString = JSON.stringify(channelBuffer[channelid])
savedString = savedString.slice(1,savedString.length-1);
savedString += `,`
savedString += currentString;
savedString += "}"
channelBuffer[channelid] = JSON.parse("{" + savedString);
logBuffer.data[channelid] = channelBuffer[channelid]
//console.log(roughSizeOfObject(JSON.stringify(logBuffer)))
fs.writeFile("logs/" + filename, JSON.stringify(logBuffer), function(err) {
if(err) {
return console.log(err);
}
});
})
function roughSizeOfObject( object ) {
var objectList = [];
var stack = [ object ];
var bytes = 0;
while ( stack.length ) {
var value = stack.pop();
if ( typeof value === 'boolean' ) {
bytes += 4;
}
else if ( typeof value === 'string' ) {
bytes += value.length * 2;
}
else if ( typeof value === 'number' ) {
bytes += 8;
}
else if
(
typeof value === 'object'
&& objectList.indexOf( value ) === -1
)
{
objectList.push( value );
for( var i in value ) {
stack.push( value[ i ] );
}
}
}
return bytes;
}