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

feat(locks): member locks #1172

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
11 changes: 9 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
"source.fixAll": true,
"source.organizeImports": false
},
"eslint.workingDirectories": [{ "pattern": "./packages/*" }],
"eslint.workingDirectories": [
{
"pattern": "./packages/*"
},
{
"pattern": "./apps/*"
}
],
"unocss.root": "./packages/website",
"i18n-ally.localesPaths": "./packages/yuudachi/locales",
"i18n-ally.localesPaths": "./apps/yuudachi/locales",
"i18n-ally.enabledFrameworks": ["i18next"],
"i18n-ally.sourceLanguage": "en-US",
"i18n-ally.displayLanguage": "en-US",
Expand Down
6 changes: 4 additions & 2 deletions apps/yuudachi/locales/en-US/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"no_message_resolvable": "Provided value `{{-val}}` for argument `{{arg}}` is not a valid message link or id.",
"no_guild": "Could not find guild `{{guild_id}}`",
"no_channel": "Could not find channel `{{channel_id}}` in guild `{{- guild}}`",
"ignored_channel": "Resolving a message from an ignored channel is not allowed."
"ignored_channel": "Resolving a message from an ignored channel is not allowed.",
"member_lock_acquired": "This member is already being processed by another command."
},
"buttons": {
"cancel": "Cancel",
Expand Down Expand Up @@ -358,7 +359,8 @@
"invalid_attachment": "Invalid attachment, only images are allowed.",
"timed_out": "The report has timed out, please try again.",
"bot": "You cannot report bots.",
"no_attachment_forward": "This user has already been recently reported, you must specify an attachment to forward if it helps the context of the report."
"no_attachment_forward": "This user has already been recently reported, you must specify an attachment to forward if it helps the context of the report.",
"member_lock_acquired": "This user is already being processed by a moderator."
},
"warnings": "**Attention:** We are not Discord and we **cannot** moderate {{- trust_and_safety}} issues.\n**Creating false reports may lead to moderation actions.**",
"trust_and_safety_sub": "Trust & Safety",
Expand Down
2 changes: 2 additions & 0 deletions apps/yuudachi/src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const enum ThreatLevelColor {

export const OP_DELIMITER = "-";

export const MEMBER_LOCK_EXPIRE_SECONDS = 20;

export const CASE_REASON_MAX_LENGTH = 500;
export const CASE_REASON_MIN_LENGTH = 3;

Expand Down
13 changes: 13 additions & 0 deletions apps/yuudachi/src/commands/moderation/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { nanoid } from "nanoid";
import { inject, injectable } from "tsyringe";
import { CASE_REASON_MAX_LENGTH } from "../../Constants.js";
import { CaseAction, createCase } from "../../functions/cases/createCase.js";
import { acquireMemberLock, releaseMemberLock, extendMemberLock } from "../../functions/locks/locks.js";
import { generateCasePayload } from "../../functions/logging/generateCasePayload.js";
import { upsertCaseLog } from "../../functions/logging/upsertCaseLog.js";
import { checkLogChannel } from "../../functions/settings/checkLogChannel.js";
Expand Down Expand Up @@ -70,6 +71,10 @@ export default class extends Command<typeof BanCommand> {
);
}

if (args.user.member) {
await acquireMemberLock(args.user.member, locale);
}

const banKey = nanoid();
const cancelKey = nanoid();

Expand Down Expand Up @@ -126,6 +131,10 @@ export default class extends Command<typeof BanCommand> {
} else if (collectedInteraction?.customId === banKey) {
await collectedInteraction.deferUpdate();

if (args.user.member) {
await extendMemberLock(args.user.member);
}

await this.redis.setex(`guild:${collectedInteraction.guildId}:user:${args.user.user.id}:ban`, 15, "");
const case_ = await createCase(
collectedInteraction.guild,
Expand All @@ -149,5 +158,9 @@ export default class extends Command<typeof BanCommand> {
components: [],
});
}

if (args.user.member) {
await releaseMemberLock(args.user.member);
}
}
}
6 changes: 6 additions & 0 deletions apps/yuudachi/src/commands/moderation/kick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { nanoid } from "nanoid";
import { inject, injectable } from "tsyringe";
import { CASE_REASON_MAX_LENGTH } from "../../Constants.js";
import { createCase, CaseAction } from "../../functions/cases/createCase.js";
import { acquireMemberLock, extendMemberLock, releaseMemberLock } from "../../functions/locks/locks.js";
import { generateCasePayload } from "../../functions/logging/generateCasePayload.js";
import { upsertCaseLog } from "../../functions/logging/upsertCaseLog.js";
import { checkLogChannel } from "../../functions/settings/checkLogChannel.js";
Expand Down Expand Up @@ -63,6 +64,8 @@ export default class extends Command<typeof KickCommand> {
);
}

await acquireMemberLock(args.user.member, locale);

const kickKey = nanoid();
const cancelKey = nanoid();

Expand Down Expand Up @@ -118,6 +121,7 @@ export default class extends Command<typeof KickCommand> {
});
} else if (collectedInteraction?.customId === kickKey) {
await collectedInteraction.deferUpdate();
await extendMemberLock(args.user.member);

await this.redis.setex(`guild:${collectedInteraction.guildId}:user:${args.user.user.id}:kick`, 15, "");
const case_ = await createCase(
Expand All @@ -139,5 +143,7 @@ export default class extends Command<typeof KickCommand> {
components: [],
});
}

await releaseMemberLock(args.user.member);
}
}
5 changes: 5 additions & 0 deletions apps/yuudachi/src/commands/moderation/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Redis } from "ioredis";
import { nanoid } from "nanoid";
import { inject, injectable } from "tsyringe";
import { REPORT_REASON_MAX_LENGTH, REPORT_REASON_MIN_LENGTH } from "../../Constants.js";
import { checkMemberLock } from "../../functions/locks/utils.js";
import type { Report } from "../../functions/reports/createReport.js";
import { getPendingReportByTarget } from "../../functions/reports/getReport.js";
import { checkLogChannel } from "../../functions/settings/checkLogChannel.js";
Expand Down Expand Up @@ -248,6 +249,10 @@ export default class extends Command<
throw new Error(i18next.t("command.mod.report.common.errors.no_self", { lng: locale }));
}

if (await checkMemberLock(author.guild.id, target.id)) {
throw new Error(i18next.t("command.mod.report.common.errors.member_lock_acquired", { lng: locale }));
}

const userKey = `guild:${author.guild.id}:report:user:${target.id}`;
const latestReport = await getPendingReportByTarget(author.guild.id, target.id);
if (latestReport || (await this.redis.exists(userKey))) {
Expand Down
12 changes: 12 additions & 0 deletions apps/yuudachi/src/commands/moderation/softban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { nanoid } from "nanoid";
import { inject, injectable } from "tsyringe";
import { CASE_REASON_MAX_LENGTH } from "../../Constants.js";
import { CaseAction, createCase } from "../../functions/cases/createCase.js";
import { acquireMemberLock, extendMemberLock, releaseMemberLock } from "../../functions/locks/locks.js";
import { generateCasePayload } from "../../functions/logging/generateCasePayload.js";
import { upsertCaseLog } from "../../functions/logging/upsertCaseLog.js";
import { checkLogChannel } from "../../functions/settings/checkLogChannel.js";
Expand Down Expand Up @@ -57,6 +58,10 @@ export default class extends Command<typeof SoftbanCommand> {

const isStillMember = interaction.guild.members.resolve(args.user.user.id);

if (isStillMember) {
await acquireMemberLock(args.user.member!, locale);
}

const softbanKey = nanoid();
const cancelKey = nanoid();

Expand Down Expand Up @@ -112,6 +117,9 @@ export default class extends Command<typeof SoftbanCommand> {
});
} else if (collectedInteraction?.customId === softbanKey) {
await collectedInteraction.deferUpdate();
if (isStillMember) {
await extendMemberLock(args.user.member!);
}

await this.redis.setex(`guild:${collectedInteraction.guildId}:user:${args.user.user.id}:ban`, 15, "");
await this.redis.setex(`guild:${collectedInteraction.guildId}:user:${args.user.user.id}:unban`, 15, "");
Expand Down Expand Up @@ -154,5 +162,9 @@ export default class extends Command<typeof SoftbanCommand> {
components: [],
});
}

if (isStillMember) {
await releaseMemberLock(args.user.member!);
}
}
}
6 changes: 6 additions & 0 deletions apps/yuudachi/src/commands/moderation/sub/restrict/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { nanoid } from "nanoid";
import type { Sql } from "postgres";
import { CASE_REASON_MAX_LENGTH } from "../../../../Constants.js";
import { CaseAction, createCase } from "../../../../functions/cases/createCase.js";
import { acquireMemberLock, extendMemberLock, releaseMemberLock } from "../../../../functions/locks/locks.js";
import { generateCasePayload } from "../../../../functions/logging/generateCasePayload.js";
import { upsertCaseLog } from "../../../../functions/logging/upsertCaseLog.js";
import type { RestrictCommand } from "../../../../interactions/index.js";
Expand Down Expand Up @@ -35,6 +36,8 @@ export async function embed(
);
}

await acquireMemberLock(args.user.member, locale);

const sql = container.resolve<Sql<any>>(kSQL);

const [roles] = await sql<[{ embed_role_id: Snowflake | null }?]>`
Expand Down Expand Up @@ -124,6 +127,7 @@ export async function embed(
});
} else if (collectedInteraction?.customId === roleKey) {
await collectedInteraction.deferUpdate();
await extendMemberLock(args.user.member);

const case_ = await createCase(
collectedInteraction.guild,
Expand All @@ -146,4 +150,6 @@ export async function embed(
components: [],
});
}

await releaseMemberLock(args.user.member);
}
6 changes: 6 additions & 0 deletions apps/yuudachi/src/commands/moderation/sub/restrict/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { nanoid } from "nanoid";
import type { Sql } from "postgres";
import { CASE_REASON_MAX_LENGTH } from "../../../../Constants.js";
import { CaseAction, createCase } from "../../../../functions/cases/createCase.js";
import { acquireMemberLock, extendMemberLock, releaseMemberLock } from "../../../../functions/locks/locks.js";
import { generateCasePayload } from "../../../../functions/logging/generateCasePayload.js";
import { upsertCaseLog } from "../../../../functions/logging/upsertCaseLog.js";
import type { RestrictCommand } from "../../../../interactions/index.js";
Expand Down Expand Up @@ -35,6 +36,8 @@ export async function emoji(
);
}

await acquireMemberLock(args.user.member, locale);

const sql = container.resolve<Sql<any>>(kSQL);

const [roles] = await sql<[{ emoji_role_id: Snowflake | null }?]>`
Expand Down Expand Up @@ -124,6 +127,7 @@ export async function emoji(
});
} else if (collectedInteraction?.customId === roleKey) {
await collectedInteraction.deferUpdate();
await extendMemberLock(args.user.member);

const case_ = await createCase(
collectedInteraction.guild,
Expand All @@ -146,4 +150,6 @@ export async function emoji(
components: [],
});
}

await releaseMemberLock(args.user.member);
}
6 changes: 6 additions & 0 deletions apps/yuudachi/src/commands/moderation/sub/restrict/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { nanoid } from "nanoid";
import type { Sql } from "postgres";
import { CASE_REASON_MAX_LENGTH } from "../../../../Constants.js";
import { CaseAction, createCase } from "../../../../functions/cases/createCase.js";
import { acquireMemberLock, extendMemberLock, releaseMemberLock } from "../../../../functions/locks/locks.js";
import { generateCasePayload } from "../../../../functions/logging/generateCasePayload.js";
import { upsertCaseLog } from "../../../../functions/logging/upsertCaseLog.js";
import type { RestrictCommand } from "../../../../interactions/index.js";
Expand Down Expand Up @@ -35,6 +36,8 @@ export async function react(
);
}

await acquireMemberLock(args.user.member, locale);

const sql = container.resolve<Sql<any>>(kSQL);

const [roles] = await sql<[{ reaction_role_id: Snowflake | null }?]>`
Expand Down Expand Up @@ -124,6 +127,7 @@ export async function react(
});
} else if (collectedInteraction?.customId === roleKey) {
await collectedInteraction.deferUpdate();
await extendMemberLock(args.user.member);

const case_ = await createCase(
collectedInteraction.guild,
Expand All @@ -146,4 +150,6 @@ export async function react(
components: [],
});
}

await releaseMemberLock(args.user.member);
}
6 changes: 6 additions & 0 deletions apps/yuudachi/src/commands/moderation/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { nanoid } from "nanoid";
import { inject, injectable } from "tsyringe";
import { CASE_REASON_MAX_LENGTH } from "../../Constants.js";
import { CaseAction, createCase } from "../../functions/cases/createCase.js";
import { acquireMemberLock, extendMemberLock, releaseMemberLock } from "../../functions/locks/locks.js";
import { generateCasePayload } from "../../functions/logging/generateCasePayload.js";
import { upsertCaseLog } from "../../functions/logging/upsertCaseLog.js";
import { checkLogChannel } from "../../functions/settings/checkLogChannel.js";
Expand Down Expand Up @@ -77,6 +78,8 @@ export default class extends Command<typeof TimeoutCommand> {
);
}

await acquireMemberLock(args.user.member, locale);

const timeoutKey = nanoid();
const cancelKey = nanoid();

Expand Down Expand Up @@ -132,6 +135,7 @@ export default class extends Command<typeof TimeoutCommand> {
});
} else if (collectedInteraction?.customId === timeoutKey) {
await collectedInteraction.deferUpdate();
await extendMemberLock(args.user.member);

await this.redis.setex(`guild:${collectedInteraction.guildId}:user:${args.user.user.id}:timeout`, 15, "");
const case_ = await createCase(
Expand All @@ -154,5 +158,7 @@ export default class extends Command<typeof TimeoutCommand> {
components: [],
});
}

await releaseMemberLock(args.user.member);
}
}
6 changes: 6 additions & 0 deletions apps/yuudachi/src/commands/moderation/warn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import i18next from "i18next";
import { nanoid } from "nanoid";
import { CASE_REASON_MAX_LENGTH } from "../../Constants.js";
import { CaseAction, createCase } from "../../functions/cases/createCase.js";
import { acquireMemberLock, extendMemberLock, releaseMemberLock } from "../../functions/locks/locks.js";
import { generateCasePayload } from "../../functions/logging/generateCasePayload.js";
import { upsertCaseLog } from "../../functions/logging/upsertCaseLog.js";
import { checkLogChannel } from "../../functions/settings/checkLogChannel.js";
Expand Down Expand Up @@ -46,6 +47,8 @@ export default class extends Command<typeof WarnCommand> {
);
}

await acquireMemberLock(args.user.member, locale);

const warnKey = nanoid();
const cancelKey = nanoid();

Expand Down Expand Up @@ -101,6 +104,7 @@ export default class extends Command<typeof WarnCommand> {
});
} else if (collectedInteraction?.customId === warnKey) {
await collectedInteraction.deferUpdate();
await extendMemberLock(args.user.member);

const case_ = await createCase(
collectedInteraction.guild,
Expand All @@ -121,5 +125,7 @@ export default class extends Command<typeof WarnCommand> {
components: [],
});
}

await releaseMemberLock(args.user.member);
}
}
7 changes: 6 additions & 1 deletion apps/yuudachi/src/functions/anti-raid/blastOff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import i18next from "i18next";
import type { Redis } from "ioredis";
import type { TargetRejection } from "../../commands/moderation/sub/anti-raid-nuke/utils.js";
import { type Case, CaseAction, createCase } from "../cases/createCase.js";
import { acquireMemberLock, releaseMemberLock } from "../locks/locks.js";
import { generateCasePayload } from "../logging/generateCasePayload.js";

export async function blastOff(
Expand All @@ -21,6 +22,7 @@ export async function blastOff(

const confirmedHits: GuildMember[] = [];
for (const member of confirmations) {
await acquireMemberLock(member, locale);
JPBM135 marked this conversation as resolved.
Show resolved Hide resolved
promises.push(
createCase(
interaction.guild,
Expand Down Expand Up @@ -51,7 +53,10 @@ export async function blastOff(
// eslint-disable-next-line promise/prefer-await-to-then, promise/prefer-await-to-callbacks
.catch((error: Error) => void rejections.push({ member, reason: error.message }))
// eslint-disable-next-line promise/prefer-await-to-then
.finally(() => void redis.expire(`guild:${interaction.guildId}:anti_raid_nuke`, 15)),
.finally(() => {
void redis.expire(`guild:${interaction.guildId}:anti_raid_nuke`, 15);
void releaseMemberLock(member);
}),
);
}

Expand Down
Loading