Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to Track #83

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/helpers/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export const describeReactions = (
title: "Reactions",
fields: reactions.map((r) => ({
name: "",
value: `${r.count} ${r.emoji.name}`,
value: `${r.count} ${
r.emoji.id ? `<:${r.emoji.name}:${r.emoji.id}>` : r.emoji.name
}`,
inline: true,
})),
};
Expand Down
103 changes: 61 additions & 42 deletions app/helpers/modLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ export const reportUser = async ({
// If we already logged for ~ this message, post to the existing thread
const { logMessage: cachedMessage, logs } = cached;

let thread = cachedMessage.thread;
if (!thread || !cachedMessage.hasThread) {
thread = await makeLogThread(cachedMessage, message.author);
}

if (cached.logs.some((l) => l.message.id === message.id)) {
// If we've already logged exactly this message, don't log it again as a
// separate report.
const latestReport = await thread.send(makeReportMessage(newReport));
return {
warnings: logs.length,
message: cachedMessage,
latestReport,
thread,
};
}

const newLogs = logs.concat([newReport]);
cachedWarnings.set(simplifiedContent, {
logMessage: cachedMessage,
Expand All @@ -99,57 +116,60 @@ export const reportUser = async ({

const warnings = newLogs.length;

let thread = cachedMessage.thread;
if (!thread || !cachedMessage.hasThread) {
thread = await makeLogThread(cachedMessage, message.author);
}

const [latestReport] = await Promise.all([
thread.send({ content: makeReportString(newReport) }),
thread.send(makeReportMessage(newReport)),
cachedMessage.edit(
cachedMessage.content
?.replace(/warned \d times/, `warned ${cachedWarnings.size} times`)
.replace(/in \d channels/, `in ${warnings} channels`) || "",
),
]);
return { warnings, message: cachedMessage, latestReport, thread };
} else {
// If this is new, send a new message
const { modLog: modLogId } = await fetchSettings(guild, [SETTINGS.modLog]);
const modLog = await guild.channels.fetch(modLogId);
if (!modLog) {
throw new Error("Channel configured for use as mod log not found");
}
if (modLog.type !== ChannelType.GuildText) {
throw new Error(
"Invalid channel configured for use as mod log, must be guild text",
);
}
const newLogs: Report[] = [{ message, reason, staff }];
}

const logBody = await constructLog({
extra,
logs: newLogs,
previousWarnings: cachedWarnings,
staff,
});
// If this is new, send a new message
const { modLog: modLogId } = await fetchSettings(guild, [SETTINGS.modLog]);
const modLog = await guild.channels.fetch(modLogId);
if (!modLog) {
throw new Error("Channel configured for use as mod log not found");
}
if (modLog.type !== ChannelType.GuildText) {
throw new Error(
"Invalid channel configured for use as mod log, must be guild text",
);
}
const newLogs: Report[] = [{ message, reason, staff }];

const warningMessage = await modLog.send(logBody);
const thread = await makeLogThread(warningMessage, message.author);
const latestReport = await thread.send(makeReportString(newReport));
const logBody = await constructLog({
extra,
logs: newLogs,
previousWarnings: cachedWarnings,
staff,
});

cachedWarnings.set(simplifiedContent, {
logMessage: warningMessage,
logs: newLogs,
});
return { warnings: 1, message: warningMessage, latestReport, thread };
}
const warningMessage = await modLog.send(logBody);
const thread = await makeLogThread(warningMessage, message.author);
const latestReport = await thread.send(makeReportMessage(newReport));

cachedWarnings.set(simplifiedContent, {
logMessage: warningMessage,
logs: newLogs,
});
return { warnings: 1, message: warningMessage, latestReport, thread };
};

const makeReportString = ({ message, reason, staff }: Report) =>
`- ${constructDiscordLink(message)} ${staff ? ` ${staff.username} ` : ""}${
ReadableReasons[reason]
}`;
const makeReportMessage = ({ message, reason, staff }: Report) => {
const embeds = [describeReactions(message.reactions.cache)].filter(
(e): e is APIEmbed => Boolean(e),
);

return {
content: `- ${constructDiscordLink(message)} ${
staff ? ` ${staff.username} ` : ""
}${ReadableReasons[reason]}`,
embeds: embeds.length === 0 ? undefined : embeds,
};
};

const constructLog = async ({
logs,
Expand Down Expand Up @@ -201,10 +221,9 @@ const constructLog = async ({
);
}

const embeds = [
describeAttachments(message.attachments),
describeReactions(message.reactions.cache),
].filter((e): e is APIEmbed => Boolean(e));
const embeds = [describeAttachments(message.attachments)].filter(
(e): e is APIEmbed => Boolean(e),
);

return {
content: truncateMessage(`${preface}
Expand Down
Loading