-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
141 lines (131 loc) · 4.5 KB
/
index.js
File metadata and controls
141 lines (131 loc) · 4.5 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
const https = require('https')
/**
* @typedef AnalyticData
* @property {Number} servers
* @property {Number} users
* @property {Number} channels
* @property {Number} sent_messages
* @property {Number} received_messages
* @property {Number} ram_used
*/
class Analytics{
/**
* @param {String} apiToken Chewey Bot api Token
* @param {import("eris").Client | import("discord.js").Client} discordBot (recommended) optional if want auto reporter (stats recorded for you)
*/
constructor(apiToken,discordBot){
this.options={
apiToken,
discordBot:discordBot,
sent_messages:0,
received_messages:0
}
if(discordBot!=null){
discordBot.on("messageCreate",(msg)=>{
this.options.received_messages++;
if(msg.author.id==discordBot.user.id){
this.options.sent_messages++;
}
})
discordBot.on("message", (msg) => {
this.options.received_messages++;
if (msg.author.id == discordBot.user.id) {
this.options.sent_messages++;
}
})
// allow bot to warmup
discordBot.on("ready",()=>{
setTimeout(() => {
this.startAutoReport()
// send initial on load
this.sendReport(this.buildBody(this.options.discordBot)).then(() => {}, () => {})
}, 10000);
})
}
}
/**
* Start auto reporting - only used if discordBot Client provided
*/
startAutoReport(){
if (this.options.discordBot && !this.options.interval) {
this.options.interval = setInterval(() => {
this.sendReport(this.buildBody(this.options.discordBot)).then(() => {}, () => {}) //ignore auto errors
}, 10.1 * 60 * 1000); //10minute + extra to ensure clean limits
}
}
/**
* Stop auto reporting - to start again use startAutoReport()
*/
stopAutoReport(){
if (this.options.interval){
clearInterval(this.options.interval)
delete this.options.interval
}
}
/**
* Used to auto construct the body to send
* @param {@param {import("eris").Client | import("discord.js").Client} discordBot} discordBot
* @returns {AnalyticData}
*/
buildBody(discordBot){
// Eris : Discord.js
let channelCount = discordBot.channelGuildMap ? Object.keys(discordBot.channelGuildMap).length : discordBot.channels.cache.size
let reply= {
servers:discordBot.guilds.size?discordBot.guilds.size:discordBot.guilds.cache.size,
users:discordBot.users.size?discordBot.users.size:discordBot.users.cache.size,
channels:channelCount,
sent_messages:this.options.sent_messages,
received_messages: this.options.received_messages,
ram_used:process.memoryUsage().rss //not true usage but is true ram consumption by pc spec
}
this.options.sent_messages=0;
this.options.received_messages=0;
return reply
}
/**
* @param {AnalyticData} body
* @returns {Promise<String|null>}
*/
sendReport(body){
// console.log("Sending rep",body);
return httpsPost(body,this.options.apiToken)
}
}
function httpsPost(body,auth){
return new Promise((resolve,reject)=>{
const data = JSON.stringify(body)
const options = {
hostname: 'api.chewey-bot.top',
port: 443,
path: '/analytics/post',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
'Authorization': auth
}
}
const req = https.request(options, (res) => {
let body = '';
res.on('data', function (d) {
body += d;
});
res.on("end",()=>{
try{
body=JSON.parse(body)
}catch(e){}
if(res.statusCode!=200){
console.log("Report stats Chewey Bot API error ",body);
}
resolve(body)
})
})
req.on('error', (error) => {
console.error("Report stats Chewey Bot API Error",error)
reject(error)
})
req.write(data)
req.end()
})
}
module.exports=Analytics