3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-24 10:05:03 +00:00
zeppelin/backend/src/plugins/Automod/triggers/threadArchive.ts
2021-10-31 18:13:13 +00:00

64 lines
2.1 KiB
TypeScript

import { Snowflake } from "discord-api-types";
import { User, Util } from "discord.js";
import * as t from "io-ts";
import { tNullable } from "../../../utils";
import { automodTrigger } from "../helpers";
interface ThreadArchiveResult {
matchedThreadId: Snowflake;
matchedThreadName: string;
matchedThreadParentId: Snowflake;
matchedThreadParentName: string;
matchedThreadOwner: User | undefined;
}
export const ThreadArchiveTrigger = automodTrigger<ThreadArchiveResult>()({
configType: t.type({
parent: tNullable(t.union([t.string, t.array(t.string)])),
locked: tNullable(t.boolean),
}),
defaultConfig: {},
async match({ context, triggerConfig }) {
if (!context.threadChange?.archived) {
return;
}
if (triggerConfig.locked && !context.threadChange.locked) {
return;
} else if (triggerConfig.locked === false && !context.threadChange.unlocked) {
return;
}
const thread = context.threadChange.archived;
if (triggerConfig.parent) {
const parentIds = Array.isArray(triggerConfig.parent) ? triggerConfig.parent : [triggerConfig.parent];
if (thread.parentId && !parentIds.includes(thread.parentId)) return;
}
return {
extra: {
matchedThreadId: thread.id,
matchedThreadName: thread.name,
matchedThreadParentId: thread.parentId ?? "Unknown",
matchedThreadParentName: thread.parent?.name ?? "Unknown",
matchedThreadOwner: context.user,
},
};
},
async renderMatchInformation({ matchResult }) {
const threadId = matchResult.extra.matchedThreadId;
const threadName = matchResult.extra.matchedThreadName;
const threadOwner = matchResult.extra.matchedThreadOwner;
const parentId = matchResult.extra.matchedThreadParentId;
const parentName = matchResult.extra.matchedThreadParentName;
const base = `Thread **#${threadName}** (\`${threadId}\`) has been archived in the **#${parentName}** (\`${parentId}\`) channel`;
if (threadOwner) {
return `${base} by **${Util.escapeBold(threadOwner.tag)}** (\`${threadOwner.id}\`)`;
}
return base;
},
});