3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-07-11 13:07:20 +00:00

added thread_create and thread_delete automod triggers

This commit is contained in:
almeidx 2021-08-29 15:05:02 +01:00
parent 831ff1893a
commit fcfc96c30d
No known key found for this signature in database
GPG key ID: 01C5E03866747F46
7 changed files with 170 additions and 2 deletions

View file

@ -24,6 +24,7 @@ import { RunAutomodOnJoinEvt, RunAutomodOnLeaveEvt } from "./events/RunAutomodOn
import { RunAutomodOnMemberUpdate } from "./events/RunAutomodOnMemberUpdate"; import { RunAutomodOnMemberUpdate } from "./events/RunAutomodOnMemberUpdate";
import { runAutomodOnMessage } from "./events/runAutomodOnMessage"; import { runAutomodOnMessage } from "./events/runAutomodOnMessage";
import { runAutomodOnModAction } from "./events/runAutomodOnModAction"; import { runAutomodOnModAction } from "./events/runAutomodOnModAction";
import { RunAutomodOnThreadCreate, RunAutomodOnThreadDelete } from "./events/runAutomodOnThreadEvents";
import { clearOldRecentNicknameChanges } from "./functions/clearOldNicknameChanges"; import { clearOldRecentNicknameChanges } from "./functions/clearOldNicknameChanges";
import { clearOldRecentActions } from "./functions/clearOldRecentActions"; import { clearOldRecentActions } from "./functions/clearOldRecentActions";
import { clearOldRecentSpam } from "./functions/clearOldRecentSpam"; import { clearOldRecentSpam } from "./functions/clearOldRecentSpam";
@ -200,6 +201,8 @@ export const AutomodPlugin = zeppelinGuildPlugin<AutomodPluginType>()({
RunAutomodOnJoinEvt, RunAutomodOnJoinEvt,
RunAutomodOnMemberUpdate, RunAutomodOnMemberUpdate,
RunAutomodOnLeaveEvt, RunAutomodOnLeaveEvt,
RunAutomodOnThreadCreate,
RunAutomodOnThreadDelete,
// Messages use message events from SavedMessages, see onLoad below // Messages use message events from SavedMessages, see onLoad below
], ],

View file

@ -0,0 +1,41 @@
import { typedGuildEventListener } from "knub";
import { runAutomod } from "../functions/runAutomod";
import { AutomodContext, AutomodPluginType } from "../types";
export const RunAutomodOnThreadCreate = typedGuildEventListener<AutomodPluginType>()({
event: "threadCreate",
async listener({ pluginData, args: { thread } }) {
const user = thread.ownerId ? await pluginData.client.users.fetch(thread.ownerId).catch(() => void 0) : void 0;
const context: AutomodContext = {
timestamp: Date.now(),
threadChange: {
created: thread,
},
user,
};
pluginData.state.queue.add(() => {
runAutomod(pluginData, context);
});
},
});
export const RunAutomodOnThreadDelete = typedGuildEventListener<AutomodPluginType>()({
event: "threadDelete",
async listener({ pluginData, args: { thread } }) {
const user = thread.ownerId ? await pluginData.client.users.fetch(thread.ownerId).catch(() => void 0) : void 0;
const context: AutomodContext = {
timestamp: Date.now(),
threadChange: {
deleted: thread,
},
user,
};
pluginData.state.queue.add(() => {
runAutomod(pluginData, context);
});
},
});

View file

@ -33,7 +33,15 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
for (const [ruleName, rule] of Object.entries(config.rules)) { for (const [ruleName, rule] of Object.entries(config.rules)) {
if (rule.enabled === false) continue; if (rule.enabled === false) continue;
if (!rule.affects_bots && (!user || user.bot) && !context.counterTrigger && !context.antiraid) continue; if (
!rule.affects_bots &&
(!user || user.bot) &&
!context.counterTrigger &&
!context.antiraid &&
!context.threadChange?.deleted
) {
continue;
}
if (!rule.affects_self && userId && userId === pluginData.client.user?.id) continue; if (!rule.affects_self && userId && userId === pluginData.client.user?.id) continue;
if (rule.cooldown && checkAndUpdateCooldown(pluginData, rule, context)) { if (rule.cooldown && checkAndUpdateCooldown(pluginData, rule, context)) {

View file

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

View file

@ -0,0 +1,52 @@
import { Snowflake } from "discord-api-types";
import { ThreadChannel, User, Util } from "discord.js";
import * as t from "io-ts";
import { tNullable } from "../../../utils";
import { automodTrigger } from "../helpers";
interface ThreadCreateResult {
matchedThreadId: Snowflake;
matchedThreadName: string;
matchedThreadOwner: User | undefined;
}
export const ThreadCreateTrigger = automodTrigger<ThreadCreateResult>()({
configType: t.type({
parent: tNullable(t.union([t.string, t.array(t.string)])),
}),
defaultConfig: {},
async match({ context, triggerConfig }) {
if (!context.threadChange?.created) {
return;
}
const thread = context.threadChange.created;
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,
matchedThreadOwner: context.user,
},
};
},
async renderMatchInformation({ matchResult }) {
const threadId = matchResult.extra.matchedThreadId;
const threadName = matchResult.extra.matchedThreadName;
const threadOwner = matchResult.extra.matchedThreadOwner;
if (threadOwner) {
return `Thread **#${Util.escapeBold(threadName)}** (\`${threadId}\`) has been created by **${
threadOwner.tag
}** (\`${threadOwner.id}\`)`;
}
return `Thread **#${Util.escapeBold(threadName)}** (\`${threadId}\`) has been created`;
},
});

View file

@ -0,0 +1,52 @@
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 ThreadDeleteResult {
matchedThreadId: Snowflake;
matchedThreadName: string;
matchedThreadOwner: User | undefined;
}
export const ThreadDeleteTrigger = automodTrigger<ThreadDeleteResult>()({
configType: t.type({
parent: tNullable(t.union([t.string, t.array(t.string)])),
}),
defaultConfig: {},
async match({ context, triggerConfig }) {
if (!context.threadChange?.deleted) {
return;
}
const thread = context.threadChange.deleted;
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,
matchedThreadOwner: context.user,
},
};
},
renderMatchInformation({ matchResult }) {
const threadId = matchResult.extra.matchedThreadId;
const threadName = matchResult.extra.matchedThreadName;
const threadOwner = matchResult.extra.matchedThreadOwner;
if (threadOwner) {
return `Thread **${Util.escapeBold(threadName ?? "Unknown")}** (\`${threadId}\`) created by **${Util.escapeBold(
threadOwner.tag,
)}** (\`${threadOwner.id}\`) has been deleted`;
}
return `Thread **${Util.escapeBold(threadName ?? "Unknown")}** (\`${threadId}\`) has been deleted`;
},
});

View file

@ -1,4 +1,4 @@
import { GuildMember, PartialGuildMember, User } from "discord.js"; import { GuildMember, PartialGuildMember, ThreadChannel, User } from "discord.js";
import * as t from "io-ts"; import * as t from "io-ts";
import { BasePluginType, CooldownManager } from "knub"; import { BasePluginType, CooldownManager } from "knub";
import { SavedMessage } from "../../data/entities/SavedMessage"; import { SavedMessage } from "../../data/entities/SavedMessage";
@ -131,6 +131,10 @@ export interface AutomodContext {
antiraid?: { antiraid?: {
level: string | null; level: string | null;
}; };
threadChange?: {
created?: ThreadChannel;
deleted?: ThreadChannel;
};
} }
export interface RecentAction { export interface RecentAction {