This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
/
reposter.js
502 lines (474 loc) · 19.6 KB
/
reposter.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
"use strict";
console.log("LOADING LIBRARIES...");
const fs = require("fs");
const Discord = require("discord.js");
const client = new Discord.Client();
client.login("<SECRET_BOT_TOKEN>").catch(console.error);
client.on("ready", function() {
const serverCount = client.guilds.cache.size;
client.user.setActivity(`${serverCount} server${serverCount === 1 ? "" : "s"}`, { type: "WATCHING" }).catch(console.error);
console.log("READY FOR ACTION!");
});
let config = {
replacements: {},
nicknames: {},
prefixes: {},
active: {},
tags: {},
pins: {},
live: {}
};
function updateJson() {
fs.writeFileSync("config.json", JSON.stringify(config, undefined, "\t"));
}
if (fs.existsSync("config.json")) {
config = JSON.parse(fs.readFileSync("config.json", "utf8"));
} else {
updateJson();
}
function updateStatus() {
const repostCount = Object.keys(config.active).length;
client.user.setActivity(`${repostCount} repost${repostCount === 1 ? "" : "s"}`, { type: "WATCHING" }).catch(console.error);
}
function capitalizeFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function inactive(to, from) {
return from ? !config.active[from] : !config.active[to];
}
function replaceAll(channel, str) {
const replace = config.replacements[(channel.guild || channel).id];
if (replace) {
let replaced = str;
for (let find in replace) {
const regex = new RegExp(find, "g");
replaced = replaced.replace(regex, replace[find]);
}
return replaced;
} else {
return str;
}
}
async function send(channel, content, reactions) {
const channelID = channel.channelID || channel.id;
if (inactive(channelID)) return;
const sent = await channel.send(content).catch(console.error);
if (reactions.size) {
for (const reaction of reactions.values()) {
if (inactive(channelID)) break;
const emoji = reaction.emoji;
if (client.emojis.cache.has(emoji.id) || emoji.id === null) {
await sent.react(emoji).catch(console.error);
}
}
}
}
function replaceEmbedText(embed, channel) {
if (embed.author) {
embed.setAuthor(replaceAll(channel, embed.author.name), embed.author.iconURL, embed.author.url);
}
if (embed.description) {
embed.setDescription(replaceAll(channel, embed.description));
}
/*for (let i = 0; i < embed.fields.length; i++) {
const field = embed.fields[i];
embed.addField(replaceAll(channel, field.name), replaceAll(channel, field.value), field.inline);
}*/
if (embed.footer) {
embed.setFooter(replaceAll(channel, embed.footer.text), embed.footer.iconURL);
}
if (embed.title) {
embed.setTitle(replaceAll(channel, embed.title));
}
return embed;
}
function setBoolean(channel, key, value) {
const guild = (channel.guild || channel).id;
const enabled = config[key][guild];
const property = capitalizeFirst(key);
if (value && value.match(/1|true|yes|confirm|agree|enable|on|positive|accept|ye|yep|ya|yah|yeah|sure|ok|okay/)) {
config[key][guild] = true;
channel.send(`✅ **${property} on!**`).catch(console.error);
} else if (value && value.match(/0|false|no|deny|denied|disagree|disable|off|negative|-1|nah|na|nope|stop|end|cease/)) {
config[key][guild] = false;
channel.send(`❌ **${property} off!**`).catch(console.error);
} else {
config[key][guild] = !enabled;
channel.send(`${enabled ? "❌" : "✅"} **${property} toggled ${enabled ? "off" : "on"}!**`).catch(console.error);
}
updateJson();
}
function niceName(to, from, user) {
const guild = (to.guild || to).id;
if (config.nicknames[guild] && from.guild) {
const member = from.guild.member(user);
if (member) {
return member.displayName;
} else if (config.tags[guild]) {
return user.tag;
} else {
user.username;
}
} else if (config.tags[guild]) {
return user.tag;
} else {
return user.username;
}
}
function setPrefix(channel, prefix) {
const guild = (channel.guild || channel).id;
const previous = config.prefixes[guild] || "/";
if (prefix) {
config.prefixes[guild] = prefix;
channel.send(`**Changed prefix from \`${previous}\` to \`${prefix}\`!**`).catch(console.error);
updateJson();
} else {
channel.send(`**Missing \`prefix\` argument! \`${previous}repost prefix <PREFIX>\`**`).catch(console.error);
}
}
function setReplacement(channel, find, replace) {
const guild = (channel.guild || channel).id;
const prefix = config.prefixes[guild] || "/";
config.replacements[guild] = config.replacements[guild] || {};
if (find && replace) {
config.replacements[guild][find] = replace;
channel.send(`**Replacing \`${find}\` with \`${replace}\`!**`).catch(console.error);
updateJson();
} else if (find) {
const replacement = config.replacements[guild][find];
if (replacement) {
channel.send(`**\`${find}\` is replaced with \`${replacement}\`**`).catch(console.error);
} else {
channel.send(`**Missing \`replace\` argument! \`${prefix}repost replace ${find} <REPLACE>\`**`).catch(console.error);
}
} else {
channel.send(`**Missing \`find\` and \`replace\` arguments! \`${prefix}repost replace <FIND> <REPLACE>\`**`).catch(console.error);
}
}
async function sendReplacements(channel, id) {
const replace = config.replacements[(channel.guild || channel).id];
if (replace) {
const size = Object.keys(replace).length;
const count = await channel.send(`**This channel has ${size} replacement${size === 1 ? "" : "s"}!**`).catch(console.error);
for (let find in replace) {
const message = await channel.send(`\`${find}\` is replaced with \`${replace[find]}\``).catch(console.error);
await message.react("❌").catch(console.error);
message.awaitReactions((reaction, user) => user.id === id && reaction.emoji.name === "❌", { max: 1 }).then(function() {
delete replace[find];
message.delete().catch(console.error);
updateJson();
const newSize = Object.keys(replace).length;
count.edit(`**This channel has ${newSize} replacement${newSize === 1 ? "" : "s"}!**`).catch(console.error);
})
}
} else {
channel.send("**This channel has no replacements!**").catch(console.error);
}
}
const systemMessages = {
RECIPIENT_ADD: " added someone to the group.",
RECIPIENT_REMOVE: " removed someone from the group.",
CALL: " started a call.",
CHANNEL_NAME_CHANGE: " changed the name of this channel.",
CHANNEL_ICON_CHANGE: " changed the icon of this channel.",
PINS_ADD: " pinned a message to this channel.",
GUILD_MEMBER_JOIN: " just joined."
};
async function sendMessage(message, channel, webhook, author) {
if (inactive(channel.id, message.channel.id)) return;
if (message.type !== "DEFAULT") {
await channel.send(`**${replaceAll(channel, niceName(channel, message.channel, message.author))}${systemMessages[message.type]}**`).catch(console.error);
} else if (message.author.id !== author) {
if (webhook) {
await webhook.edit(replaceAll(channel, niceName(channel, message.channel, message.author)), message.author.displayAvatarURL()).catch(console.error);
} else {
await channel.send(`**${replaceAll(channel, niceName(channel, message.channel, message.author))}**`).catch(console.error);
}
}
if (message.content) {
await send(webhook ? webhook : channel, replaceAll(channel, message.content), message.reactions);
}
if (message.attachments.size) {
for (const attachment of message.attachments.values()) {
await send(webhook ? webhook : channel, attachment.filesize > 8000000 ? attachment.url : { files: [attachment.url] }, message.reactions);
}
}
if (message.embeds.length) {
for (let i = 0; i < message.embeds.length; i++) {
const embed = message.embeds[i];
if (embed.type === "rich") {
await send(webhook ? webhook : channel, replaceEmbedText(embed, channel), message.reactions);
}
}
}
}
async function sendMessages(messages, channel, webhook, author) {
if (inactive(channel.id)) return;
let last;
if (messages && messages.size) {
const backward = messages.array().reverse();
for (let i = 0; i < backward.length; i++) {
if (inactive(channel.id, backward[i].channel.id)) break;
await sendMessage(backward[i], channel, webhook, last ? last.author.id : author);
last = backward[i];
}
}
}
async function fetchMessages(message, channel, webhook, author) {
if (inactive(channel.id, message.channel.id)) return;
const messages = await message.channel.messages.fetch({ limit: 100, after: message.id }).catch(async function() {
await channel.send("**Couldn't fetch messages!**").catch(console.error);
});
if (inactive(channel.id, message.channel.id)) return;
if (messages && messages.size) {
await sendMessages(messages, channel, webhook, author);
const last = messages.last();
await fetchMessages(last, channel, webhook, last.author.id);
} else {
await channel.send("**Repost Complete!**").catch(console.error);
}
}
async function fetchWebhook(channel) {
const webhooks = await channel.fetchWebhooks().catch(async function() {
await channel.send("**Can't read webhooks!**").catch(console.error);
});
if (webhooks) {
for (const webhook of webhooks.values()) {
if (webhook.owner.id === client.user.id) {
return webhook;
}
}
return channel.createWebhook("Reposter", { avatar: client.user.displayAvatarURL(), reason: "Reposting" }).catch(console.error);
}
}
async function sendInfo(to, from) {
const rich = new Discord.MessageEmbed();
rich.setTitle(from.name || from.id);
rich.setDescription(from.topic || "No topic");
rich.setFooter(`Reposting from ${from.id}`, client.user.displayAvatarURL());
if (from.guild) {
rich.setAuthor(from.guild.name, from.guild.iconURL());
rich.setThumbnail(from.guild.iconURL());
} else if (from.recipient) {
rich.setAuthor(niceName(to, from, from.recipient), from.recipient.displayAvatarURL());
rich.setThumbnail(from.recipient.displayAvatarURL());
} else {
rich.setAuthor(niceName(to, from, from.owner), from.iconURL());
rich.setThumbnail(from.iconURL());
}
rich.setTimestamp();
if (from.parent) {
rich.addField("Channel Category", from.parent.name, true);
}
rich.addField("NSFW Channel", from.nsfw || "false", true);
rich.addField("Channel ID", from.id, true);
rich.addField("Channel Type", from.type, true);
rich.addField("Channel Creation Date", from.createdAt, true);
rich.addField("Channel Creation Time", from.createdTimestamp, true);
if (from.guild) {
rich.addField("Server ID", from.guild.id, true);
rich.addField("Server Owner", niceName(to, from, await client.users.fetch(from.guild.ownerID)), true);
rich.addField("Server Region", from.guild.region, true);
const channels = new Map();
for (const channel of from.guild.channels.cache.values()) {
channels.set(channel.type, (channels.get(channel.type) || 0) + 1);
}
for (const channel of channels.entries()) {
rich.addField(`${capitalizeFirst(channel[0])} Channels`, channel[1], true);
}
rich.addField("Server Members", from.guild.memberCount, true);
rich.addField("Server Roles", from.guild.roles.cache.size, true);
rich.addField("Server Emojis", from.guild.emojis.cache.size, true);
rich.addField("Server Verification", from.guild.verificationLevel, true);
if (from.guild.systemChannel) {
rich.addField("Default Channel", from.guild.systemChannel.name, true);
rich.addField("Default Channel ID", from.guild.systemChannelID, true);
}
rich.addField("Server Creation Date", from.guild.createdAt, true);
rich.addField("Server Creation Time", from.guild.createdTimestamp, true);
}
await to.send(rich).catch(console.error);
}
async function repost(id, message, webhook, direction, live) {
const channel = (id && id.id) ? id : await client.channels.fetch(id).catch(() => null);
const dir = direction ? "from" : "to";
if (!channel) {
const guild = await client.guilds.fetch(id).catch(() => null);
if (guild) {
config.active[message.channel.id] = true;
updateStatus();
await message.channel.send(`**Reposting${live ? " live " : " "}${dir} \`${guild.name || id}\`!**`).catch(console.error);
for (const match of guild.channels.cache.values()) {
if (inactive(message.channel.id)) break;
config.active[match.id] = true;
updateStatus();
updateJson();
await repost(match, message, webhook, direction, live);
}
} else if (message.mentions.channels.size) {
await repost(message.mentions.channels.first(), message, webhook, direction, live);
} else {
const matches = [];
for (const match of client.channels.cache.values()) {
if (id === match.name) {
matches.push(match);
}
}
if (matches.length) {
if (matches.length === 1) {
await repost(matches[0], message, webhook, direction, live);
} else {
await message.channel.send(`**Found ${matches.length} channels!**`).catch(console.error);
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
const rich = new Discord.MessageEmbed();
rich.setFooter(`${capitalizeFirst(match.type)} Channel`, client.user.displayAvatarURL());
if (match.guild) {
rich.setAuthor(match.name, match.guild.iconURL());
} else if (match.recipient) {
rich.setAuthor(niceName(message.channel, match, match.recipient), match.recipient.displayAvatarURL());
} else {
rich.setAuthor(match.name, match.iconURL());
}
rich.setTimestamp(match.createdAt);
rich.addField("Channel ID", `\`${match.id}\``, false);
const embed = await message.channel.send(rich).catch(console.error);
await embed.react("✅").catch(console.error);
embed.awaitReactions((reaction, user) => user.id === message.author.id && reaction.emoji.name === "✅", { max: 1 }).then(async function() {
await repost(match, message, webhook, direction, live);
});
}
}
} else {
await message.channel.send(`**Couldn't repost ${dir} \`${id}\`!**`).catch(console.error);
}
}
} else if (channel.id === message.channel.id) {
await message.channel.send(`**Can't repost ${dir} the same channel!**`).catch(console.error);
} else if (!channel.type.match(/text|group|dm/)) {
await message.channel.send(`**Can't repost ${dir} ${channel.type} channels!**`).catch(console.error);
} else if (webhook && (direction ? message.channel.type : channel.type) === "dm") {
await message.channel.send("**Can't create webhooks on DM channels!**").catch(console.error);
} else if (channel.type === "text" && !direction && !channel.permissionsFor(client.user).has("SEND_MESSAGES")) {
await message.channel.send(`**Can't repost to \`${channel.name || id}\` without permission!**`).catch(console.error);
} else {
const to = direction ? message.channel : channel;
const from = direction ? channel : message.channel;
config.active[to.id] = true;
config.active[from.id] = true;
updateStatus();
updateJson();
await message.channel.send(`**Reposting${live ? " live " : " "}${dir} \`${channel.name || id}\`!**`).catch(console.error);
if (live) {
config.live[from.id] = { channel: to.id, hook: webhook };
updateJson();
} else {
await sendInfo(to, from);
if (inactive(to.id, from.id)) return;
const hook = webhook && await fetchWebhook(to);
if (config.pins[(to.guild || to).id]) {
await to.send("__**Pins**__").catch(console.error);
const pins = await from.messages.fetchPinned().catch(async function() {
await to.send("**Can't read pins!**").catch(console.error);
});
await sendMessages(pins, to, hook);
}
if (inactive(to.id, from.id)) return;
await to.send("__**Messages**__").catch(console.error);
const messages = await from.messages.fetch({ limit: 1, after: "0" }).catch(async function() {
await to.send("**Can't read messages!**").catch(console.error);
});
const first = messages && messages.first();
if (first) {
await sendMessage(first, to, hook);
await fetchMessages(first, to, hook, first.author.id);
} else {
await to.send("**Repost Complete!**").catch(console.error);
}
}
}
}
async function repostLive(message) {
const live = config.live[message.channel.id] || config.live[message.guild.id];
if (live) {
const channel = await client.channels.fetch(live.channel).catch(() => null);
const hook = live.hook && await fetchWebhook(channel);
await sendMessage(message, channel, hook);
}
}
function sendCommands(channel) {
const prefix = config.prefixes[(channel.guild || channel).id] || "/";
const rich = new Discord.MessageEmbed();
rich.setTitle("Reposter Commands");
rich.setDescription("By MysteryPancake");
rich.setFooter(client.user.id, client.user.displayAvatarURL());
rich.setAuthor(niceName(channel, channel, client.user), client.user.displayAvatarURL(), "https://github.com/MysteryPancake/Discord-Reposter");
rich.setThumbnail(client.user.displayAvatarURL());
rich.setTimestamp();
rich.setURL("https://github.com/MysteryPancake/Discord-Reposter#commands");
rich.addField("Repost To", `*Reposts to a channel.*\`\`\`${prefix}repost <CHANNEL>\n${prefix}repost to <CHANNEL>\`\`\``, false);
rich.addField("Repost From", `*Reposts from a channel.*\`\`\`${prefix}repost from <CHANNEL>\`\`\``, false);
rich.addField("Repost Webhook", `*Reposts through a webhook.*\`\`\`${prefix}reposthook\n${prefix}repostwebhook\`\`\`Instead of:\`\`\`${prefix}repost\`\`\``, false);
rich.addField("Repost Live", `*Reposts messages as they come.*\`\`\`${prefix}repostlive\n${prefix}repostlivehook\`\`\`Instead of:\`\`\`${prefix}repost\`\`\``, false);
rich.addField("Repost Stop", `*Stops reposting.*\`\`\`${prefix}repost stop\n${prefix}repost halt\n${prefix}repost cease\n${prefix}repost terminate\n${prefix}repost suspend\n${prefix}repost cancel\n${prefix}repost die\n${prefix}repost end\`\`\``, false);
rich.addField("Repost Commands", `*Posts the command list.*\`\`\`${prefix}repost help\n${prefix}repost commands\`\`\``, false);
rich.addField("Repost Replace", `*Replaces text when reposting.*\`\`\`${prefix}repost replace <FIND> <REPLACE>\`\`\``, false);
rich.addField("Repost Replacements", `*Posts the replacement list.*\`\`\`${prefix}repost replacements\`\`\``, false);
rich.addField("Repost Prefix", `*Changes the bot prefix.*\`\`\`${prefix}repost prefix <PREFIX>\`\`\``, false);
rich.addField("Repost Tags", `*Toggles user tags when reposting.*\`\`\`${prefix}repost tags\n${prefix}repost tags <STATE>\`\`\``, false);
rich.addField("Repost Nicknames", `*Toggles nicknames when reposting.*\`\`\`${prefix}repost nicknames\n${prefix}repost nicknames <STATE>\`\`\``, false);
rich.addField("Repost Pins", `*Toggles pins when reposting.*\`\`\`${prefix}repost pins\n${prefix}repost pins <STATE>\`\`\``, false);
rich.addField("Channel ID", `\`\`\`${channel.id}\`\`\``, false);
channel.send(rich).catch(console.error);
}
client.on("message", function(message) {
repostLive(message);
if (message.author.bot) return;
const args = message.content.toLowerCase().split(" ");
const prefix = config.prefixes[(message.guild || message.channel).id] || "/";
if (args[0].startsWith(`${prefix}repost`)) {
switch (args[1]) {
case undefined:
case "help":
case "commands":
sendCommands(message.channel);
break;
case "replacements":
sendReplacements(message.channel, message.author.id);
break;
case "replace":
setReplacement(message.channel, args[2], args[3]);
break;
case "prefix":
setPrefix(message.channel, args[2]);
break;
case "tags":
case "nicknames":
case "pins":
setBoolean(message.channel, args[1], args[2]);
break;
case "stop":
case "halt":
case "cease":
case "terminate":
case "suspend":
case "cancel":
case "die":
case "end":
delete config.active[message.channel.id];
delete config.live[message.channel.id];
updateStatus();
updateJson();
message.channel.send("**Reposting Terminated!**").catch(console.error);
break;
default:
const last = args[2];
if (last) {
repost(last, message, args[0].indexOf("hook") !== -1, args[1] === "from", args[0].indexOf("live") !== -1);
} else {
repost(args[1], message, args[0].indexOf("hook") !== -1, false, args[0].indexOf("live") !== -1);
}
break;
}
}
});