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 all 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
5 changes: 5 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 { 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 @@ -126,6 +127,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 Down
2 changes: 2 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 { 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 @@ -118,6 +119,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 Down
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
4 changes: 4 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 { 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 @@ -112,6 +113,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
2 changes: 2 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 { extendMemberLock } 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 @@ -124,6 +125,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 Down
2 changes: 2 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 { extendMemberLock } 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 @@ -124,6 +125,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 Down
2 changes: 2 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 { extendMemberLock } 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 @@ -124,6 +125,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 Down
2 changes: 2 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 { 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 @@ -132,6 +133,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 Down
2 changes: 2 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 { 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 @@ -101,6 +102,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 Down
2 changes: 2 additions & 0 deletions apps/yuudachi/src/commands/utility/ping.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from "@yuudachi/framework";
import type { ArgsParam, InteractionParam, LocaleParam } from "@yuudachi/framework/types";
import i18next from "i18next";
import { acquireMemberLock } from "../../functions/locks/locks.js";
import type { PingCommand } from "../../interactions/index.js";

export default class extends Command<typeof PingCommand> {
Expand All @@ -9,6 +10,7 @@ export default class extends Command<typeof PingCommand> {
args: ArgsParam<typeof PingCommand>,
locale: LocaleParam,
): Promise<void> {
await acquireMemberLock(interaction.member!, locale);
await interaction.deferReply({ ephemeral: args.hide ?? true });

await interaction.editReply({
Expand Down
17 changes: 12 additions & 5 deletions apps/yuudachi/src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { handleCaseAutocomplete } from "../functions/autocomplete/cases.js";
import { handleReasonAutocomplete } from "../functions/autocomplete/reasons.js";
import { handleReportAutocomplete } from "../functions/autocomplete/reports.js";
import { AutocompleteType, findAutocompleteType } from "../functions/autocomplete/validate.js";
import { acquireMemberLock, releaseMemberLock } from "../functions/locks/locks.js";
import { getGuildSetting, SettingsKeys } from "../functions/settings/getGuildSetting.js";
import { findMemberInArgs } from "../util/findMember.js";

const commandCounter = new Counter({
name: "yuudachi_bot_v3_gateway_events_interaction_create_command_total",
Expand Down Expand Up @@ -108,11 +110,16 @@ export default class implements Event {
);
break;
} else {
await command.chatInput(
interaction,
transformApplicationInteraction(interaction.options.data),
effectiveLocale,
);
const args = transformApplicationInteraction(interaction.options.data);
const member = findMemberInArgs(args);

if (member) {
await acquireMemberLock(member, effectiveLocale);
}

await command
.chatInput(interaction, args, effectiveLocale)
.finally(() => member && void releaseMemberLock(member));
break;
}
}
Expand Down
55 changes: 55 additions & 0 deletions apps/yuudachi/src/functions/locks/locks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { clearTimeout } from "node:timers";
import { container, kRedis } from "@yuudachi/framework";
import type { LocaleParam } from "@yuudachi/framework/types";
import type { GuildMember } from "discord.js";
import i18next from "i18next";
import type { Redis } from "ioredis";
import { MEMBER_LOCK_EXPIRE_SECONDS } from "../../Constants.js";
import { createLockTimeout } from "./utils.js";

const LocksMap = new Map<string, NodeJS.Timeout>();

export async function acquireMemberLock(member: GuildMember, locale: LocaleParam): Promise<void> {
const redis = container.resolve<Redis>(kRedis);

const lockKey = `guild:${member.guild.id}:member-lock:${member.id}`;

const lock = await redis.set(lockKey, member.user.createdTimestamp, "EX", MEMBER_LOCK_EXPIRE_SECONDS, "NX");

if (!lock) {
throw new Error(
i18next.t("command.common.errors.member_lock_acquired", {
lng: locale,
}),
);
}

LocksMap.set(lockKey, createLockTimeout(member, LocksMap));
}

export async function extendMemberLock(member: GuildMember): Promise<void> {
const redis = container.resolve<Redis>(kRedis);

const lockKey = `guild:${member.guild.id}:member-lock:${member.id}`;

await redis.expire(lockKey, MEMBER_LOCK_EXPIRE_SECONDS, "GT");

const lock = LocksMap.get(lockKey);

if (lock) {
clearTimeout(lock);
LocksMap.set(lockKey, createLockTimeout(member, LocksMap));
}
}

export async function releaseMemberLock(member: GuildMember): Promise<void> {
const redis = container.resolve<Redis>(kRedis);

const lockKey = `guild:${member.guild.id}:member-lock:${member.id}`;

await redis.del(lockKey);

const lock = LocksMap.get(lockKey);
clearTimeout(lock);
LocksMap.delete(lockKey);
}
41 changes: 41 additions & 0 deletions apps/yuudachi/src/functions/locks/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { setTimeout } from "node:timers";
import { container, kRedis, logger } from "@yuudachi/framework";
import type { GuildMember } from "discord.js";
import type { Redis } from "ioredis";
import { Counter } from "prom-client";

const expiredLocksCounter = new Counter({
name: "yuudachi_bot_v3_utils_expired_locks_total",
help: "Total number of unreleased keys that expired",
labelNames: ["memberId", "guildId"],
});

export async function checkMemberLock(guildId: string, memberId: string): Promise<boolean> {
const redis = container.resolve<Redis>(kRedis);

const lockKey = `guild:${guildId}:member-lock:${memberId}`;

const lock = await redis.exists(lockKey);

return Boolean(lock);
}

export function createLockTimeout(member: GuildMember, map: Map<string, NodeJS.Timeout>): NodeJS.Timeout {
const lockKey = `guild:${member.guild.id}:member-lock:${member.id}`;

return setTimeout(() => {
logger.warn({
msg: "Lock expired",
memberId: member.id,
guildId: member.guild.id,
lockKey,
});

expiredLocksCounter.inc({
memberId: member.id,
guildId: member.guild.id,
});

map.delete(lockKey);
}, 60_000);
}
27 changes: 27 additions & 0 deletions apps/yuudachi/src/util/findMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GuildMember } from "discord.js";

type NestedMember = {
member?: GuildMember;
};

type FindArgsOptions = {
[key: string]:
| NestedMember
| {
user: NestedMember;
};
user: NestedMember;
};

export function findMemberInArgs(args: FindArgsOptions): GuildMember | null {
if (args?.user?.member instanceof GuildMember) {
return args.user.member ?? null;
}

for (const key of Object.keys(args)) {
const member = (args[key as keyof typeof args] as FindArgsOptions).user?.member;
if (member instanceof GuildMember) return member;
}

return null;
}