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

Add metadata to mod long showing number of reported posts #54

Merged
merged 3 commits into from
May 27, 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
2 changes: 1 addition & 1 deletion app/commands/track.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const handler = async (
message.delete(),
latestReport.reply({
allowedMentions: { users: [] },
content: `Message in <#${message.channelId}> deleted by <@${user.id}>`,
content: `deleted by ${user.username}`,
}),
]);
instance.render("Tracked");
Expand Down
73 changes: 49 additions & 24 deletions app/helpers/modLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
User,
ClientUser,
} from "discord.js";
import TTLCache from "@isaacs/ttlcache";

import { fetchSettings, SETTINGS } from "~/models/guilds.server";
import {
Expand Down Expand Up @@ -34,19 +35,30 @@ interface Report {
staff: User | ClientUser | false;
}

const HOUR = 60 * 60 * 1000;
type UserID = string;
type GuildID = string;
const cache = new TTLCache<
`${UserID}${GuildID}`,
Map<
string,
{
logMessage: Message;
logs: Report[];
}
>
>({
ttl: 20 * HOUR,
max: 1000,
});

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

const warningMessages = new Map<
string,
{
logMessage: Message;
logs: Report[];
}
>();
// const warningMessages = new ();
export const reportUser = async ({
reason,
message,
Expand All @@ -55,17 +67,33 @@ export const reportUser = async ({
}: Omit<Report, "date">) => {
const { guild } = message;
if (!guild) throw new Error("Tried to report a message without a guild");
const simplifiedContent = `${message.guildId}${
message.author.id
}${simplifyString(message.content)}`;
const cached = warningMessages.get(simplifiedContent);
const cacheKey = `${message.guildId}${message.author.id}`;
const simplifiedContent = simplifyString(message.content);

let cachedWarnings = cache.get(cacheKey);
if (!cachedWarnings) {
cachedWarnings = new Map<
string,
{
logMessage: Message;
logs: Report[];
}
>();
cache.set(cacheKey, cachedWarnings);
}
const cached = cachedWarnings.get(simplifiedContent);
const newReport: Report = { message, reason, staff };

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

const newLogs = logs.concat([newReport]);
cachedWarnings.set(simplifiedContent, {
logMessage: cachedMessage,
logs: newLogs,
});

const warnings = newLogs.length;

let thread = cachedMessage.thread;
Expand All @@ -76,17 +104,11 @@ export const reportUser = async ({
const [latestReport] = await Promise.all([
thread.send({ content: makeReportString(newReport) }),
cachedMessage.edit(
cachedMessage.content?.replace(
/warned \d times/,
`warned ${warnings} times`,
) || "",
cachedMessage.content
?.replace(/warned \d times/, `warned ${cachedWarnings.size} times`)
.replace(/in \d channels/, `in ${warnings} channels`) || "",
),
]);

warningMessages.set(simplifiedContent, {
logMessage: cachedMessage,
logs: newLogs,
});
return { warnings, message: cachedMessage, latestReport, thread };
} else {
// If this is new, send a new message
Expand All @@ -97,14 +119,15 @@ export const reportUser = async ({
const logBody = await constructLog({
extra,
logs: newLogs,
uniquePastWarnings: cachedWarnings.size + 1,
staff,
});

const warningMessage = await modLog.send(logBody);
const thread = await makeLogThread(warningMessage, message.author);
const latestReport = await thread.send(makeReportString(newReport));

warningMessages.set(simplifiedContent, {
cachedWarnings.set(simplifiedContent, {
logMessage: warningMessage,
logs: newLogs,
});
Expand All @@ -119,17 +142,19 @@ const makeReportString = ({ message, reason, staff }: Report) =>

const constructLog = async ({
logs,
uniquePastWarnings,
extra: origExtra = "",
}: Pick<Report, "extra" | "staff"> & {
logs: Report[];
uniquePastWarnings: number;
}): Promise<MessageCreateOptions> => {
const lastReport = logs.at(-1)!;

const preface = `<@${lastReport.message.author.id}> (${
lastReport.message.author.username
}) warned ${logs.length} times, posted ${formatDistanceToNowStrict(
lastReport.message.createdAt,
)} ago`;
}) warned ${uniquePastWarnings} times recently, posted in ${
logs.length
} channels ${formatDistanceToNowStrict(lastReport.message.createdAt)} ago`;
const extra = origExtra ? `${origExtra}\n` : "";

const reportedMessage = quoteAndEscape(lastReport.message.content).trim();
Expand Down
Loading
Loading