-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
227 lines (189 loc) · 7.47 KB
/
app.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
// discord init
const Discord = require('discord.js');
const client = new Discord.Client({
partials: Object.values(Discord.Constants.PartialTypes),
});
// emoji that goes in the post title
const tt = '⭐';
let settings;
let guildID = '';
let smugboardID = '';
let token = '';
let messagePosted = {};
try {
settings = require('./settings.json');
} catch (e) {
console.log(`a settings.json file has not been generated. ${e.stack}`);
process.exit();
}
function login() {
if (settings.token) {
console.log(`Logging in with token...`);
client.login(settings.token);
} else {
console.log('Error logging in: There may be an issue with you settings.json file');
}
}
client.on('ready', () => {
console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
guildID = settings.serverID;
smugboardID = settings.channelID;
loadIntoMemory(client, guildID, smugboardID, settings.fetchLimit);
//client.user.setActivity(`you...`, { type: 'WATCHING' })
})
// BEGIN STARBOARD CODE
async function loadIntoMemory(client, guildID, channelID, limit) {
const channel = client.guilds.get(guildID).channels.get(channelID);
console.log(`Loading ${limit} messages...`)
let messagesLeft = 0
if (limit > 100) {
messagesLeft = limit - 100
limit = 100
}
console.log(`${messagesLeft} messages left to load`)
var messages = await channel.messages.fetch({ limit: limit })
while (messagesLeft) {
if (messagesLeft > 100) {
messagesLeft = messagesLeft - 100
console.log(`${messagesLeft} messages left to load`)
let moreMessages = await channel.messages.fetch({ limit: 100, before: messages.lastKey() })
messages = await messages.concat(moreMessages)
} else {
console.log(`${messagesLeft} messages left to load`)
let moreMessages = await channel.messages.fetch({ limit: messagesLeft, before: messages.lastKey() })
messages = await messages.concat(moreMessages)
messagesLeft = 0
}
}
const posts = messages.filter(m => m.content.match(/\((\d{18})\)/));
const newPosts = messages.filter(m => {
if(m.embeds.length > 0){
if(m.embeds[0].footer)
return String(m.embeds[0].footer.text).match(/\((\d{18})\)/)
else
return false
}else{
return false
}
});
const postsMap = posts.reduce((mapAccumulator, obj) => {
// either one of the following syntax works
// mapAccumulator[obj.key] = obj.val;
mapAccumulator[String(obj.content.match(/\((\d{18})\)/)[1])] = {
p: true,
lc: settings.threshold + 1,
legacy: true,
psm: obj.id,
};
return mapAccumulator;
}, {});
const newPostsMap = newPosts.reduce((mapAccumulator, obj) => {
// either one of the following syntax works
// mapAccumulator[obj.key] = obj.val;
mapAccumulator[String(obj.embeds[0].footer.text).match(/\((\d{18})\)/)[1]] = {
p: true,
lc: settings.threshold + 1,
legacy: false,
psm: obj.id,
};
return mapAccumulator;
}, {});
messagePosted = {...postsMap, ...newPostsMap};
console.log(`Loaded ${Object.keys(postsMap).length} legacy posts, and ${Object.keys(newPostsMap).length} new posts in ${settings.reactionEmoji} channel`);
console.log(`Loading complete`)
}
client.on('messageReactionAdd', (reaction_orig, user) => {
// if channel is posting channel
if (reaction_orig.message.channel.id == smugboardID) return;
// if reaction is not desired emoji
if (reaction_orig.emoji.name !== settings.reactionEmoji) return;
const msg = reaction_orig.message;
const msgID = msg.id;
const msgChannelID = msg.channel.id;
const msgChannel = client.guilds.get(guildID).channels.get(msgChannelID);
const msgLink = `https://discordapp.com/channels/${guildID}/${msgChannelID}/${msgID}`;
const channel = client.guilds.get(guildID).channels.get(smugboardID);
// if message doesnt exist yet in memory, create it
if (!messagePosted.hasOwnProperty(msgID)) {
// p: boolean: has been posted to channel,
// lc: int: number of stars
messagePosted[msgID] = {
p: false,
lc: 0
};
}else{
if(messagePosted[msgID].legacy){
console.log(`Legacy message ${settings.reactionEmoji}'d, ignoring`)
return;
}
}
msgChannel.messages.fetch(msg.id).then((msg) => {
// if message is older than set amount
let dateDiff = (new Date()) - reaction_orig.message.createdAt;
let dateCutoff = 1000 * 60 * 60 * 24;
if (Math.floor(dateDiff / dateCutoff) >= settings.dateCutoff) {
console.log(`a message older than ${settings.dateCutoff} days was reacted to, ignoring`);
return;
}
//We need to do this because the reaction count seems to be 1 if an old cached
//message is starred. This is to get the 'actual' count
msg.reactions.forEach((reaction) => {
if (reaction.emoji.name == settings.reactionEmoji) {
console.log(`message ${settings.reactionEmoji}'d! (${msgID}) in #${msgChannel.name} total: ${reaction.count}`);
// did message reach threshold
if (reaction.count >= settings.threshold) {
messagePosted[msgID].lc = reaction.count;
// if message is already posted
if (messagePosted[msgID].hasOwnProperty('psm')) {
const editableMessageID = messagePosted[msgID].psm;
console.log(`updating count of message with ID ${editableMessageID}. reaction count: ${reaction.count}`);
const messageFooter = `${reaction.count} ${tt} (${msgID})`
channel.messages.fetch(editableMessageID).then((message) => {
message.embeds[0].setFooter(messageFooter)
message.edit(message.embeds[0])
});
} else {
// if message has already been created
if (messagePosted[msgID].p) return;
console.log(`posting message with content ID ${msgID}. reaction count: ${reaction.count}`);
// add message to ongoing object in memory
messagePosted[msgID].p = true;
// create content message
const contentMsg = `${msg.content}\n\n→ [original message](${msgLink}) in <#${msgChannelID}>`;
const avatarURL = `https://cdn.discordapp.com/avatars/${msg.author.id}/${msg.author.avatar}.jpg`;
const embeds = msg.embeds;
const attachments = msg.attachments;
const messageFooter = `${reaction.count} ${tt} (${msgID})`
let eURL = ''
if (embeds.length > 0) {
// attempt to resolve image url; if none exist, ignore it
if(embeds[0].thumbnail && embeds[0].thumbnail.url)
eURL = embeds[0].thumbnail.url;
else if(embeds[0].image && embeds[0].image.url)
eURL = embeds[0].image.url;
else
eURL = embeds[0].url;
} else if (attachments.array().length > 0) {
const attARR = attachments.array();
eURL = attARR[0].url;
// no attachments or embeds
}
const embed = new Discord.MessageEmbed()
.setAuthor(msg.author.username, avatarURL)
.setColor(settings.hexcolor)
.setDescription(contentMsg)
.setImage(eURL)
.setTimestamp(new Date())
.setFooter(messageFooter);
channel.send({
embed
}).then((starMessage) => {
messagePosted[msgID].psm = starMessage.id;
});
}
}
}
});
});
});
login();