feat: add thread_create and thread_delete automod triggers (#272)

Co-authored-by: metal <admin@metalruller.com>
This commit is contained in:
Almeida 2021-10-31 15:21:53 +00:00 committed by GitHub
parent 6e3a6249c7
commit 92dfbca362
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 177 additions and 5 deletions

View file

@ -26,6 +26,8 @@ import { NoteTrigger } from "./note";
import { RoleAddedTrigger } from "./roleAdded";
import { RoleRemovedTrigger } from "./roleRemoved";
import { StickerSpamTrigger } from "./stickerSpam";
import { ThreadCreateTrigger } from "./threadCreate";
import { ThreadDeleteTrigger } from "./threadDelete";
import { UnbanTrigger } from "./unban";
import { UnmuteTrigger } from "./unmute";
import { WarnTrigger } from "./warn";
@ -64,6 +66,9 @@ export const availableTriggers: Record<string, AutomodTriggerBlueprint<any, any>
unban: UnbanTrigger,
antiraid_level: AntiraidLevelTrigger,
thread_create: ThreadCreateTrigger,
thread_delete: ThreadDeleteTrigger,
};
export const AvailableTriggers = t.type({
@ -101,4 +106,7 @@ export const AvailableTriggers = t.type({
unban: UnbanTrigger.configType,
antiraid_level: AntiraidLevelTrigger.configType,
thread_create: ThreadCreateTrigger.configType,
thread_delete: ThreadDeleteTrigger.configType,
});

View file

@ -0,0 +1,48 @@
import { Snowflake } from "discord-api-types";
import { User, Util } from "discord.js";
import * as t from "io-ts";
import { automodTrigger } from "../helpers";
interface ThreadCreateResult {
matchedThreadId: Snowflake;
matchedThreadName: string;
matchedThreadParentId: Snowflake;
matchedThreadParentName: string;
matchedThreadOwner: User | undefined;
}
export const ThreadCreateTrigger = automodTrigger<ThreadCreateResult>()({
configType: t.type({}),
defaultConfig: {},
async match({ context }) {
if (!context.threadChange?.created) {
return;
}
const thread = context.threadChange.created;
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 created in the **#${parentName}** (\`${parentId}\`) channel`;
if (threadOwner) {
return `${base} by **${Util.escapeBold(threadOwner.tag)}** (\`${threadOwner.id}\`)`;
}
return base;
},
});

View file

@ -0,0 +1,51 @@
import { Snowflake } from "discord-api-types";
import { User, Util } from "discord.js";
import * as t from "io-ts";
import { automodTrigger } from "../helpers";
interface ThreadDeleteResult {
matchedThreadId: Snowflake;
matchedThreadName: string;
matchedThreadParentId: Snowflake;
matchedThreadParentName: string;
matchedThreadOwner: User | undefined;
}
export const ThreadDeleteTrigger = automodTrigger<ThreadDeleteResult>()({
configType: t.type({}),
defaultConfig: {},
async match({ context }) {
if (!context.threadChange?.deleted) {
return;
}
const thread = context.threadChange.deleted;
return {
extra: {
matchedThreadId: thread.id,
matchedThreadName: thread.name,
matchedThreadParentId: thread.parentId ?? "Unknown",
matchedThreadParentName: thread.parent?.name ?? "Unknown",
matchedThreadOwner: context.user,
},
};
},
renderMatchInformation({ matchResult }) {
const threadId = matchResult.extra.matchedThreadId;
const threadOwner = matchResult.extra.matchedThreadOwner;
const threadName = matchResult.extra.matchedThreadName;
const parentId = matchResult.extra.matchedThreadParentId;
const parentName = matchResult.extra.matchedThreadParentName;
if (threadOwner) {
return `Thread **#${threadName ?? "Unknown"}** (\`${threadId}\`) created by **${Util.escapeBold(
threadOwner.tag,
)}** (\`${threadOwner.id}\`) in the **#${parentName}** (\`${parentId}\`) channel has been deleted`;
}
return `Thread **#${
threadName ?? "Unknown"
}** (\`${threadId}\`) from the **#${parentName}** (\`${parentId}\`) channel has been deleted`;
},
});