mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
add match_mentions automod trigger
This commit is contained in:
parent
8445c37f64
commit
d477a16d06
4 changed files with 85 additions and 0 deletions
|
@ -120,6 +120,16 @@ export class GuildSavedMessages extends BaseGuildRepository<SavedMessage> {
|
|||
}));
|
||||
}
|
||||
|
||||
if (msg.mentions) {
|
||||
data.mentions = {
|
||||
channels: Array.from(msg.mentions.channels.keys()),
|
||||
everyone: msg.mentions.everyone,
|
||||
roles: Array.from(msg.mentions.roles.keys()),
|
||||
repliedUser: msg.mentions.repliedUser?.id ?? null,
|
||||
users: Array.from(msg.mentions.users.keys()),
|
||||
};
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
|
|
@ -64,6 +64,14 @@ export interface ISavedMessageStickerData {
|
|||
type: string | null;
|
||||
}
|
||||
|
||||
export interface ISavedMessageMentionsData {
|
||||
channels: Snowflake[];
|
||||
everyone: boolean;
|
||||
roles: Snowflake[];
|
||||
users: Snowflake[];
|
||||
repliedUser: Snowflake | null;
|
||||
}
|
||||
|
||||
export interface ISavedMessageData {
|
||||
attachments?: ISavedMessageAttachmentData[];
|
||||
author: {
|
||||
|
@ -74,6 +82,7 @@ export interface ISavedMessageData {
|
|||
embeds?: ISavedMessageEmbedData[];
|
||||
stickers?: ISavedMessageStickerData[];
|
||||
timestamp: number;
|
||||
mentions?: ISavedMessageMentionsData;
|
||||
}
|
||||
|
||||
@Entity("messages")
|
||||
|
|
|
@ -34,6 +34,7 @@ import { UnmuteTrigger } from "./unmute";
|
|||
import { WarnTrigger } from "./warn";
|
||||
import { ThreadArchiveTrigger } from "./threadArchive";
|
||||
import { ThreadUnarchiveTrigger } from "./threadUnarchive";
|
||||
import { MatchMentionsTrigger } from "./matchMentions";
|
||||
|
||||
export const availableTriggers: Record<string, AutomodTriggerBlueprint<any, any>> = {
|
||||
any_message: AnyMessageTrigger,
|
||||
|
@ -44,6 +45,7 @@ export const availableTriggers: Record<string, AutomodTriggerBlueprint<any, any>
|
|||
match_links: MatchLinksTrigger,
|
||||
match_attachment_type: MatchAttachmentTypeTrigger,
|
||||
match_mime_type: MatchMimeTypeTrigger,
|
||||
match_mentions: MatchMentionsTrigger,
|
||||
member_join: MemberJoinTrigger,
|
||||
role_added: RoleAddedTrigger,
|
||||
role_removed: RoleRemovedTrigger,
|
||||
|
@ -86,6 +88,7 @@ export const AvailableTriggers = t.type({
|
|||
match_links: MatchLinksTrigger.configType,
|
||||
match_attachment_type: MatchAttachmentTypeTrigger.configType,
|
||||
match_mime_type: MatchMimeTypeTrigger.configType,
|
||||
match_mentions: MatchMentionsTrigger.configType,
|
||||
member_join: MemberJoinTrigger.configType,
|
||||
member_leave: MemberLeaveTrigger.configType,
|
||||
role_added: RoleAddedTrigger.configType,
|
||||
|
|
63
backend/src/plugins/Automod/triggers/matchMentions.ts
Normal file
63
backend/src/plugins/Automod/triggers/matchMentions.ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
import { Snowflake } from "discord.js";
|
||||
import * as t from "io-ts";
|
||||
import { tNullable } from "../../../utils";
|
||||
import { getTextMatchPartialSummary } from "../functions/getTextMatchPartialSummary";
|
||||
import { automodTrigger } from "../helpers";
|
||||
|
||||
const configType = t.type({
|
||||
channels: tNullable(t.array(t.string)),
|
||||
everyone: tNullable(t.boolean),
|
||||
roles: tNullable(t.array(t.string)),
|
||||
users: tNullable(t.array(t.string)),
|
||||
});
|
||||
|
||||
type ConfigKeys = keyof t.TypeOf<typeof configType>;
|
||||
|
||||
const summaryType: Record<ConfigKeys, string> = {
|
||||
channels: "channel",
|
||||
everyone: "everyone",
|
||||
roles: "role",
|
||||
users: "user",
|
||||
};
|
||||
|
||||
interface MatchResultType {
|
||||
reason: typeof summaryType[ConfigKeys];
|
||||
}
|
||||
|
||||
const predicate = (items: Snowflake[], configIds?: Snowflake[] | null): boolean =>
|
||||
!!configIds?.length && items.some((item) => configIds.includes(item));
|
||||
|
||||
export const MatchMentionsTrigger = automodTrigger<MatchResultType>()({
|
||||
configType,
|
||||
|
||||
defaultConfig: {
|
||||
channels: [],
|
||||
everyone: false,
|
||||
roles: [],
|
||||
users: [],
|
||||
},
|
||||
|
||||
async match({ context, triggerConfig }) {
|
||||
if (!context.message?.data.mentions) return;
|
||||
|
||||
for (const key of Object.keys(summaryType) as Array<keyof typeof summaryType>) {
|
||||
if (key === "everyone") {
|
||||
if (context.message.data.mentions.everyone && triggerConfig.everyone) {
|
||||
return { extra: { reason: summaryType.everyone } };
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (predicate(context.message.data.mentions[key], triggerConfig[key])) {
|
||||
return { extra: { reason: summaryType[key] } };
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
renderMatchInformation({ pluginData, contexts, matchResult }) {
|
||||
const partialSummary = getTextMatchPartialSummary(pluginData, "message", contexts[0]);
|
||||
return `Matched ${matchResult.extra.reason} mention in ${partialSummary}`;
|
||||
},
|
||||
});
|
Loading…
Add table
Reference in a new issue