-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarPostStore.js
304 lines (269 loc) · 8.21 KB
/
StarPostStore.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const {Collection} = require("discord.js");
const fetch = require("node-fetch");
class StarPostStore extends Collection {
constructor(bot, db) {
super();
this.db = db;
this.bot = bot;
};
async init() {
this.bot.on("messageReactionAdd", (...args) => {
try {
this.handleReactions(...args)
} catch(e) {
console.log(e);
}
})
this.bot.on("messageReactionRemove", async (...args) => {
try {
this.handleReactionRemove(...args);
} catch(e) {
console.log(e);
}
})
this.bot.on("messageReactionRemoveAll", async (msg) => {
try {
this.handleReactionRemove(msg, "bulk");
} catch(e) {
console.log(e);
}
})
this.bot.on("messageDelete", async (msg) => {
try {
this.delete(msg.channel.guild.id, msg.id);
} catch(e) {
console.log(e);
}
})
this.bot.on("messageBulkDelete", async (msgs) => {
try {
for(var msg of msgs) this.delete(msg.channel.guild.id, msg.id);
} catch(e) {
console.log(e);
}
})
}
async create(server, channel, msg, data = {}) {
return new Promise(async (res, rej) => {
var attachments = [];
if(msg.attachments && msg.attachments[0]) {
for(var i = 0; i < msg.attachments.length; i++) {
var att = await fetch(msg.attachments[i].url);
att = Buffer.from(await att.buffer());
if(att.length > 8000000) continue;
attachments.push({file: att, name: msg.attachments[i].filename});
}
}
var embed = {
author: {
name: `${msg.author.username}#${msg.author.discriminator}`,
icon_url: msg.author.avatarURL
},
footer: {
text: msg.channel.name
},
description: (msg.content || "*(image only)*") + `\n\n[Go to message](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id})`,
timestamp: new Date(msg.timestamp).toISOString()
};
try {
var message = await this.bot.createMessage(channel, {
content: `${data.emoji.includes(":") ? `<${data.emoji}>` : data.emoji} ${data.count}`,
embed
}, attachments[0] ? attachments : null);
await this.db.query(`INSERT INTO starred_messages (
server_id,
channel_id,
message_id,
original_id,
emoji
) VALUES ($1,$2,$3,$4,$5)`,
[server, channel, message.id, msg.id, data.emoji])
} catch(e) {
console.log(e);
return rej(e.message);
}
res(await this.get(server, message));
})
}
//for migrations/indexing existing messages
async index(server, channel, message, data = {}) {
return new Promise(async (res, rej) => {
try {
await this.db.query(`INSERT INTO starred_messages (
server_id,
channel_id,
message_id,
original_id,
emoji
) VALUES ($1,$2,$3,$4,$5)`,
[server, channel, message, data.original_id, data.emoji])
} catch(e) {
console.log(e);
return rej(e.message);
}
res(await this.get(server, message));
})
}
async get(server, message, forceUpdate = false) {
return new Promise(async (res, rej) => {
if(!forceUpdate) {
var post = super.get(`${server}-${message}`);
if(post) return res(post);
}
try {
//second line grabs the correct starboard and returns it
//as a prop specifically called "starboard"
var data = await this.db.query(`
SELECT starred_messages.*, (
SELECT row_to_json((SELECT a FROM (SELECT starboards.*) a))
FROM starboards WHERE starboards.channel_id = starred_messages.channel_id
) AS starboard FROM starred_messages WHERE server_id = $1 AND message_id = $2`,
[server, message]);
} catch(e) {
console.log(e);
return rej(e.message);
}
if(data.rows && data.rows[0]) {
this.set(`${server}-${message}`, data.rows[0])
res(data.rows[0])
} else res(undefined);
})
}
async getByOriginal(server, message) {
return new Promise(async (res, rej) => {
try {
//second line grabs the correct starboard and returns it
//as a prop specifically called "starboard"
var data = await this.db.query(`
SELECT starred_messages.*, (
SELECT row_to_json((SELECT a FROM (SELECT starboards.*) a))
FROM starboards WHERE starboards.channel_id = starred_messages.channel_id
) AS starboard FROM starred_messages WHERE server_id = $1 AND original_id = $2`,
[server, message]);
} catch(e) {
console.log(e);
return rej(e.message);
}
if(data.rows && data.rows[0]) {
res(data.rows[0])
} else res(undefined);
})
}
async getAll(server) {
return new Promise(async (res, rej) => {
try {
var data = await this.db.query(`
SELECT starred_messages.*, (
SELECT row_to_json((SELECT a FROM (SELECT starboards.*) a))
FROM starboards WHERE starboards.channel_id = starred_messages.channel_id
) AS starboard FROM starred_messages WHERE server_id = $1`,
[server]);
} catch(e) {
console.log(e);
return rej(e.message);
}
if(data.rows && data.rows[0]) {
res(data.rows)
} else res(undefined);
})
}
async update(server, message, data = {}) {
return new Promise(async (res, rej) => {
//no database updates needed! yay!
try {
var post = await this.get(server, message);
} catch(e) {
console.log(e);
return rej(e.message);
}
try {
if(data.count > 0)
this.bot.editMessage(
post.channel_id,
post.message_id,
`${post.emoji.includes(":") ?
`<${post.emoji}>` :
post.emoji} ${data.count}`
);
else await this.delete(server, message);
} catch(e) {
console.log(e);
return rej(e.message || e);
}
res();
})
}
async delete(server, message) {
return new Promise(async (res, rej) => {
try {
await this.db.query(`DELETE FROM starred_messages WHERE server_id = $1 AND message_id = $2`, [server, message]);
super.delete(`${server}-${message}`);
} catch(e) {
console.log(e);
return rej(e.message || e);
}
res();
})
}
async handleReactions(msg, emoji, user) {
return new Promise(async (res, rej) => {
if(!msg.channel.guild) return res();
try {
msg = await this.bot.getMessage(msg.channel.id, msg.id);
} catch(e) {
if(!e.message.toLowerCase().includes("unknown message")) console.log(e);
return rej(e.message);
}
emoji = emoji.id ? `:${emoji.name}:${emoji.id}` : emoji.name;
var reaction = msg.reactions[emoji.replace(/^:/,"")];
var board = await this.bot.stores.starboards.getByEmoji(msg.guild.id, emoji);
if(!board) return res();
var cfg = await this.bot.stores.configs.get(msg.guild.id);
var tolerance;
if(cfg) tolerance = cfg.starboard || 2;
if(board.tolerance) tolerance = board.tolerance;
var member = msg.guild.members.find(m => m.id == user.id);
if(!member) return rej("Member not found");
var post = await this.getByOriginal(msg.guild.id, msg.id);
var scc;
if(post) {
scc = await this.update(msg.guild.id, post.message_id, {emoji: emoji, count: reaction ? reaction.count : 0})
} else if(reaction.count >= tolerance || (board.override && member.permission.has("manageMessages"))) {
scc = await this.create(msg.guild.id, board.channel_id, msg, {emoji: emoji, count: reaction ? reaction.count : 0})
}
res(scc);
})
}
async handleReactionRemove(msg, emoji, user) {
return new Promise(async (res, rej) => {
var post = await this.getByOriginal(msg.channel.guild.id, msg.id);
if(!post) return;
try {
msg = await this.bot.getMessage(msg.channel.id, msg.id);
} catch(e) {
if(!e.message.toLowerCase().includes("unknown message")) console.log(e);
return rej(e.message);
}
if(emoji != "bulk") {
//not removeAll reaction event, so we need some extra logic
var config = await this.bot.stores.configs.get(msg.channel.guild.id);
if(config && config.blacklist && config.blacklist.includes(user.id)) return;
var reaction = emoji.id ? `:${emoji.name}:${emoji.id}` : emoji.name;
var count = msg.reactions[reaction.replace(/^:/,"")] ? msg.reactions[reaction.replace(/^:/,"")].count : 0;
console.log(count);
if(count > 0) {
await this.update(post.server_id, post.message_id, {emoji: reaction, count});
return;
}
}
try {
await this.bot.deleteMessage(post.channel_id, post.message_id);
} catch(e) {
console.log(e);
return rej(e.message);
}
res();
})
}
}
module.exports = (bot, db) => new StarPostStore(bot, db);