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

Shuffle mod log tracking to be more legible #51

Merged
merged 1 commit into from
May 18, 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
15 changes: 1 addition & 14 deletions app/commands/convene.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { format } from "date-fns";
import {
ApplicationCommandType,
ChannelType,
Expand Down Expand Up @@ -56,25 +55,13 @@ export const handler = async (
return;
}

const { message: logMessage } = await reportUser({
const { thread } = await reportUser({
message,
reason: ReportReasons.mod,
staff: interaction.user,
extra: `‼️ <@${interaction.user.id}> requested mods respond`,
});

if (logMessage.hasThread) {
console.log("Ignoring a redundant Convene Mods call");
await interaction.reply({
content: `Already active: <#${logMessage.thread?.id}>`,
ephemeral: true,
});
return;
}

const thread = await logMessage.startThread({
name: `${message.author.username} mod response ${format(new Date(), "P")}`,
});
const staff = interaction.user;
const originalChannel = (await message.channel.fetch()) as TextChannel;
const pollInstance = reacord.send(
Expand Down
4 changes: 2 additions & 2 deletions app/commands/track.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export const handler = async (
// Need to ensure that we've finished reporting before we try to
// respond to a click event.
// Initiating at the top level and waiting here is a big UX win.
const { message: logMessage } = await reportPromise;
const { latestReport } = await reportPromise;
await Promise.allSettled([
message.delete(),
logMessage.reply({
latestReport.reply({
allowedMentions: { users: [] },
content: `Message in <#${message.channelId}> deleted by <@${user.id}>`,
}),
Expand Down
70 changes: 39 additions & 31 deletions app/helpers/modLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ interface Report {
date: Date;
}

const makeLogThread = (message: Message, user: User) => {
return message.startThread({
name: `${user.username} – ${format(message.createdAt, "P")}`,
});
};

const warningMessages = new Map<
string,
{
Expand All @@ -54,30 +60,35 @@ export const reportUser = async ({
message.author.id
}${simplifyString(message.content)}`;
const cached = warningMessages.get(simplifiedContent);
const newReport = { message, reason, staff, date: new Date() };

if (cached) {
// If we already logged for ~ this message, edit the log
// If we already logged for ~ this message, post to the existing thread
const { logMessage: cachedMessage, logs } = cached;

const newLogs = logs.concat([{ message, reason, staff, date: new Date() }]);
const newLogs = logs.concat([newReport]);
const warnings = newLogs.length;

const logBody = await constructLog({
logs: newLogs,
extra,
staff,
});

const finalLog =
logBody.content?.replace(/warned \d times/, `warned ${warnings} times`) ||
"";
let thread = cachedMessage.thread;
if (!thread || !cachedMessage.hasThread) {
thread = await makeLogThread(cachedMessage, message.author);
}

const [latestReport] = await Promise.all([
thread.send({ content: await makeReportString(newReport) }),
cachedMessage.edit(
cachedMessage.content?.replace(
/warned \d times/,
`warned ${warnings} times`,
) || "",
),
]);

await cachedMessage.edit(truncateMessage(finalLog.slice(0, 1999)));
warningMessages.set(simplifiedContent, {
logMessage: cachedMessage,
logs: newLogs,
});
return { warnings, message: cachedMessage };
return { warnings, message: cachedMessage, latestReport, thread };
} else {
// If this is new, send a new message
const { modLog: modLogId } = await fetchSettings(guild, [SETTINGS.modLog]);
Expand All @@ -90,20 +101,26 @@ export const reportUser = async ({
staff,
});

const warningMessage = await modLog.send(logBody);
const thread = await warningMessage.startThread({
name: message.content.slice(0, 50).toLocaleLowerCase().trim(),
});
await thread.send(quoteAndEscape(message.content).trim().slice(0, 2000));
const [warningMessage, reportString] = await Promise.all([
modLog.send(logBody),
makeReportString(newReport),
]);
const thread = await makeLogThread(warningMessage, message.author);
const latestReport = await thread.send(reportString);

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

const makeReportString = ({ message, reason, staff, date }: Report) =>
`- ${constructDiscordLink(message)} ${staff ? ` ${staff.username} ` : ""}${
ReadableReasons[reason]
} on ${format(date, "Pp")}`;

const constructLog = async ({
logs,
extra: origExtra = "",
Expand All @@ -112,29 +129,20 @@ const constructLog = async ({
}): Promise<MessageCreateOptions> => {
const lastReport = logs.at(-1)!;

const reports = logs
.map(
({ message, reason, staff, date }) =>
`- ${constructDiscordLink(message)} ${
staff ? ` ${staff.username} ` : ""
}${ReadableReasons[reason]} on ${format(date, "Pp")}`,
)
.join("\n")
.trim();
const preface = `<@${lastReport.message.author.id}> (${
lastReport.message.author.username
}) warned ${logs.length} times, posted ${formatDistanceToNowStrict(
lastReport.message.createdAt,
)} ago`;
const extra = origExtra ? `\n${origExtra}\n` : "";

const reportedMessage = quoteAndEscape(lastReport.message.content).trim();
const attachments = describeAttachments(lastReport.message.attachments);

return {
content: truncateMessage(`${preface}

${reports}
${extra}`),
${extra}
${reportedMessage}`).trim(),
embeds: attachments ? [{ description: `\n\n${attachments}` }] : undefined,
};
};
3 changes: 2 additions & 1 deletion app/helpers/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const truncateMessage = (
// Discord's limit for message length
maxLength = 2000,
) => {
if (content.length > maxLength) return `${content.slice(0, maxLength - 1)}…`;
if (content.length > maxLength)
return `${content.trim().slice(0, maxLength - 1)}…`;

return content;
};
Loading