-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (74 loc) · 3.61 KB
/
index.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
module.exports = function({ bot, knex, config, commands, threads }) {
const responseMsg = (!config.mr["reportResponseMessage"] ? "Thank you! Our moderators will take a look and take any action if needed." : config.mr["reportResponseMessage"])
// Misc functions
function log(log) {
console.info(`[messageReport:log] ${log}`);
}
function err(err) {
console.error(`[messageReport:error] ${err}`);
}
function escape(str) {
let strs = str.replace(/`/g, "")
return strs;
}
async function isBlocked(userId) {
const row = await knex("blocked_users").where("user_id", userId).first();
return !!row;
}
// Publish the app interaction to the main server
bot.guilds.get(config.mainServerId[0]).createCommand({
name: "Report Message",
type: 3
})
.then(cmd => {
log("Ready! Usage: Right Click > App > Report Message");
})
.catch(error => {
// The only time this would error I think is if the bot lacked the application.commands scope, so we'll just assume that's why
err("Failed to publish the Report Message interaction in the main server.");
err(error);
return;
})
bot.on("interactionCreate", async i => {
// Making sure this is the interaction we want
if (!i.data.type == 3) {return;}
if (!i.name == "Report Message") {return;}
await i.acknowledge(64);
// Obvious reasons
if (await isBlocked(i.member.id)) {
const row = await knex("blocked_users").where("user_id", i.member.id).first();
await i.createFollowup( { content: `**You are currently blocked from creating threads.** This block ${(row.expires_at == null ? "has not been set to expire automatically." : `will expire <t:${Math.round(Date.parse(row.expires_at + " UTC") / 1000)}:R>.`)}` } );
return;
}
// Format the reported message. Probably shouldn't be using random() but I was tired and this worked so ¯\_(ツ)_/¯
const reportMsg = i.data.resolved.messages.random()
const msgModel = `:pencil: **${i.member.username}** reported a message:\n**${reportMsg.author.username}#${reportMsg.author.discriminator} (<@${reportMsg.author.id}>) => <#${reportMsg.channel.id}>:** ${(reportMsg.content.substring(0, 1800).length == 0 ? "[no content]" : `\`\`\`${escape(reportMsg.content.substring(0, 300))}\`\`\``)}${(reportMsg.attachments.length !== 0 ? ` [${reportMsg.attachments.length} attachments]` : "")}\n\n(https://discord.com/channels/${reportMsg.guildID}/${reportMsg.channel.id}/${reportMsg.id})`
// Only allows regular messages to be reported for compatibility reasons
if (reportMsg.type !== 0 && reportMsg.type !== 19) {
await i.createFollowup( { content: "**This type of message cannot be reported.**" } );
return;
}
// Disallow self reporting
if (i.member.id == reportMsg.author.id) {
await i.createFollowup( { content: "**You may not report your own messages.**" } );
return;
}
// If the user already has an active thread then add it to that and stop there
if (await threads.findOpenThreadByUserId(i.member.id)){
const t = await threads.findOpenThreadByUserId(i.member.id);
await t.postSystemMessage(msgModel);
await i.createFollowup( { content: responseMsg } );
return;
}
// Actually do the thing
await threads.createNewThreadForUser(i.member, {
source: "messagereport",
categoryId: (!config.mr["categoryId"] ? config.categoryAutomation.newThread : config.mr["categoryId"])
})
.then(nt => {
nt.postSystemMessage(msgModel);
i.createFollowup( { content: responseMsg } );
})
// Okay we're done bye
})
}