-
Notifications
You must be signed in to change notification settings - Fork 13
/
update.js
132 lines (126 loc) · 3.93 KB
/
update.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
import { EmbedBuilder, ThreadAutoArchiveDuration, ButtonBuilder, ButtonStyle } from "discord.js";
import {
MODMAIL_COLORS,
getUserFromModmail,
sendClosedMessage,
sendOpenedMessage,
MODMAIL_UNSUPPORTED,
} from "../../common/modmail.js";
import warn from "../../common/moderation/warns.js";
import { badWordsAllowed, censor } from "../../common/moderation/automod.js";
import log, { LOG_GROUPS } from "../../common/moderation/logging.js";
import { DATABASE_THREAD } from "../../common/database.js";
import CONSTANTS from "../../common/CONSTANTS.js";
import { MessageActionRowBuilder } from "../../common/types/ActionRowBuilder.js";
/** @type {import("../../common/types/event").default<"threadUpdate">} */
export default async function event(oldThread, newThread) {
if (newThread.guild.id !== process.env.GUILD_ID) return;
const logs = [];
if (oldThread.archived !== newThread.archived) {
logs.push(` ${newThread.archived ? "closed" : "opened"}`);
}
if (oldThread.locked !== newThread.locked) {
logs.push(` ${newThread.locked ? "locked" : "unlocked"}`);
}
if (oldThread.autoArchiveDuration !== newThread.autoArchiveDuration) {
logs.push(
`’s hide after inactivity set to ${
{
[ThreadAutoArchiveDuration.OneHour]: "1 Hour",
[ThreadAutoArchiveDuration.OneDay]: "24 Hours",
[ThreadAutoArchiveDuration.ThreeDays]: "3 Days",
[ThreadAutoArchiveDuration.OneWeek]: "1 Week",
}[newThread.autoArchiveDuration || ThreadAutoArchiveDuration.OneDay] ||
newThread.autoArchiveDuration
}`,
);
}
if (oldThread.rateLimitPerUser !== newThread.rateLimitPerUser) {
logs.push(
"’s slowmode was set to " +
newThread.rateLimitPerUser +
` second${newThread.rateLimitPerUser === 1 ? "" : "s"}`,
);
}
newThread.appliedTags; // TODO
if (oldThread.flags.has("Pinned") !== newThread.flags.has("Pinned")) {
await log(
`📌 Post ${
newThread.flags.has("Pinned") ? "" : "un"
}pinned in ${newThread.parent?.toString()}!`,
"messages",
{
components: [
new MessageActionRowBuilder().addComponents(
new ButtonBuilder()
.setLabel("View Post")
.setStyle(ButtonStyle.Link)
.setURL(newThread.url),
),
],
},
);
}
if (
newThread.archived &&
(newThread.name === DATABASE_THREAD ||
/** @type {readonly string[]} */ (LOG_GROUPS).includes(newThread.name)) &&
newThread.parent?.id === CONSTANTS.channels.modlogs?.id
) {
await newThread.setArchived(false);
}
await Promise.all(
logs.map((edit) =>
log(`📃 Thread ${oldThread.toString()} (${newThread.url})` + edit + `!`, "channels"),
),
);
const censored = censor(newThread.name);
if (censored && !badWordsAllowed(newThread)) {
await newThread.setName(oldThread.name);
const owner = await newThread.fetchOwner();
if (owner?.guildMember)
await warn(
owner.guildMember,
`Watch your language!`,
censored.strikes,
"Renamed thread to:\n" + newThread.name,
);
}
const latestMessage = (await oldThread.messages.fetch({ limit: 1 })).first();
if (
newThread.parent?.id !== CONSTANTS.channels.modmail?.id ||
oldThread.archived === newThread.archived ||
(newThread.archived &&
latestMessage?.interaction?.commandName === "modmail close" &&
Date.now() - +latestMessage.createdAt < 60_000)
)
return;
if (newThread.archived) {
await sendClosedMessage(newThread);
return;
}
const member = await getUserFromModmail(newThread);
await Promise.all([
newThread.fetchStarterMessage().then((starter) => {
starter
?.edit({
embeds: [
(starter.embeds[0]
? EmbedBuilder.from(starter.embeds[0])
: new EmbedBuilder()
)
.setTitle("Modmail ticket opened!")
.setFooter({
text:
MODMAIL_UNSUPPORTED +
CONSTANTS.footerSeperator +
"Messages starting with an equals sign (=) are ignored.",
})
.setColor(MODMAIL_COLORS.opened),
],
})
.catch(console.error);
}),
member && sendOpenedMessage(member),
]);
}