This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat-collection.ts
256 lines (213 loc) · 6.58 KB
/
stat-collection.ts
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import * as Discord from "discord.js";
import admin from "firebase-admin";
import moment from 'moment';
import { Subject } from "rxjs";
import { auditTime } from "rxjs/operators";
import { Logger } from "./logger";
import { getVisitor } from "./ua";
import { getCustomEmojis } from "./utils";
export class StatCollection {
private static guilds: { [guildId: string]: StatCollection } = {};
public static fromGuild(guild: Discord.Guild) {
if (!this.guilds[guild.id]) {
this.guilds[guild.id] = new StatCollection(guild);
}
return this.guilds[guild.id];
}
private meta: StatGuild;
private temp: StatData;
private update$ = new Subject<void>();
constructor(private guild: Discord.Guild) {
this.temp = {
members: guild.memberCount,
};
this.meta = {
userNames: {},
channelNames: {},
};
this.update$.pipe(
auditTime(60 * 1000)
).subscribe(() => {
this.save();
});
this.update$.next();
}
async save() {
const newmeta = this.meta;
const temp = this.temp;
this.newTemp();
Logger.debug(`save stat ${this.guild.id}: ${JSON.stringify(temp)}`);
const db = admin.firestore();
const metaRef = db.collection("stat").doc(this.guild.id);
const metaDoc = await metaRef.get();
let meta = metaDoc.data() as StatGuild;
if (!metaDoc.exists || !meta) {
meta = newmeta;
}
else {
Object.keys(newmeta.channelNames).forEach(key => {
meta.channelNames[key] = newmeta.channelNames[key];
});
Object.keys(newmeta.userNames).forEach(key => {
meta.userNames[key] = newmeta.userNames[key];
});
}
metaRef.set(meta, { merge: true });
const dateStr = moment().format('YYYY-MM-DD');
const dailyRef = metaRef.collection("daily").doc(dateStr);
const dailyDoc = await dailyRef.get();
let daily = dailyDoc.data() as StatData;
if (!dailyDoc.exists || !daily) {
daily = temp;
}
else {
mergeData(daily, temp);
}
dailyRef.set(daily, { merge: true });
}
newTemp() {
const prev = this.temp;
this.temp = {
members: prev.members,
};
this.meta = {
userNames: {},
channelNames: {},
};
}
memberChange() {
if (this.temp.members != this.guild.memberCount) {
this.temp.members = this.guild.memberCount;
this.update$.next();
}
}
addMessage(message: Discord.Message) {
if (!this.temp.messagesByMember) this.temp.messagesByMember = {};
if (!this.temp.messagesByChannel) this.temp.messagesByChannel = {};
if (!this.temp.messagesByMemberByChannel) this.temp.messagesByMemberByChannel = {};
if (!this.temp.messagesByMemberByChannel[message.author.id]) this.temp.messagesByMemberByChannel[message.author.id] = {};
add(this.temp, 'messages');
add(this.temp.messagesByMember, message.author.id);
add(this.temp.messagesByMemberByChannel[message.author.id], message.channel.id);
add(this.temp.messagesByChannel, message.channel.id);
const emojis = getCustomEmojis(message);
emojis.forEach(emoji => {
if (!this.temp.emojis) this.temp.emojis = {};
if (!this.temp.emojisByMember) this.temp.emojisByMember = {};
if (!this.temp.emojisByMember[message.author.id]) this.temp.emojisByMember[message.author.id] = {};
add(this.temp.emojis, emoji.id);
add(this.temp.emojisByMember[message.author.id], emoji.id);
});
this.meta.userNames[message.author.id] = message.author.tag;
this.meta.channelNames[message.channel.id] = (message.channel as Discord.TextChannel).name;
this.update$.next();
getVisitor(message.author).event('message', {
user: message.author.id,
tag: message.author.tag,
channel: message.channel.id,
channelName: (message.channel as Discord.TextChannel).name,
guild: message.guild?.id,
}).send();
}
addReaction(messageReaction: Discord.MessageReaction, user: Discord.User | Discord.PartialUser) {
if (messageReaction.emoji.id) {
if (!this.temp.reactions) this.temp.reactions = {};
if (!this.temp.reactionsByMember) this.temp.reactionsByMember = {};
if (!this.temp.reactionsByMember[user.id]) this.temp.reactionsByMember[user.id] = {};
add(this.temp.reactions, messageReaction.emoji.id);
add(this.temp.reactionsByMember[user.id], messageReaction.emoji.id);
this.update$.next();
getVisitor(user).event('reaction', {
user: user.id,
tag: user.tag,
emoji: messageReaction.emoji.id,
}).send();
}
}
addMeme(message: Discord.Message, meme: string) {
if (!this.temp.memes) this.temp.memes = {};
add(this.temp.memes, meme);
this.update$.next();
getVisitor(message.author).event('meme', {
user: message.author.id,
tag: message.author.tag,
channel: message.channel.id,
channelName: (message.channel as Discord.TextChannel).name,
guild: message.guild?.id,
meme,
}).send();
}
}
export function mergeData(base: StatData, temp: StatData) {
base.members = temp.members;
if (temp.messages) base.messages = (base.messages || 0) + temp.messages;
mergeRecordData(base, temp, 'messagesByMember');
mergeDoubleRecordData(base, temp, 'messagesByMemberByChannel');
mergeRecordData(base, temp, 'messagesByChannel');
mergeRecordData(base, temp, 'emojis');
mergeDoubleRecordData(base, temp, 'emojisByMember');
mergeRecordData(base, temp, 'reactions');
mergeDoubleRecordData(base, temp, 'reactionsByMember');
mergeRecordData(base, temp, 'memes');
}
export function mergeRecordData(base: StatData, temp: StatData, type: string) {
if (temp[type]) {
if (!base[type]) base[type] = {};
Object.keys(temp[type]).forEach(key => {
add(base[type], key, temp[type][key]);
});
}
}
export function mergeDoubleRecordData(base: StatData, temp: StatData, type: string) {
if (temp[type]) {
if (!base[type]) base[type] = {};
Object.keys(temp[type]).forEach(key => {
mergeRecordData(base[type], temp[type], key);
});
}
}
function add(obj: object, key: string, value = 1) {
obj[key] = (obj[key] || 0) + value;
}
interface StatGuild {
userNames: {
[userId: string]: string,
};
channelNames: {
[channelId: string]: string,
};
}
interface StatData {
members: number;
messages?: number;
messagesByMember?: Record<string, number>;
messagesByMemberByChannel?: Record<string, Record<string, number>>;
messagesByChannel?: Record<string, number>;
emojis?: Record<string, number>;
emojisByMember?: Record<string, Record<string, number>>;
reactions?: Record<string, number>;
reactionsByMember?: Record<string, Record<string, number>>;
memes?: Record<string, number>;
}
/*
/stat/{guildId}
{
userNames: {
[userId: string]: string,
},
channelNames: {
[channelId: string]: string,
},
}
/stat/{guildId}/daily/{date}
{
members: number,
messages: number,
messagesByMember: {
[userId: string]: number,
},
messagesByChannel: {
[channelId: string]: number,
},
}
*/