feat(automod): MIME type trigger (#247)
This commit is contained in:
parent
535659a2b7
commit
6a45ce67fa
3 changed files with 97 additions and 0 deletions
|
@ -114,6 +114,21 @@ const configPreprocessor: ConfigPreprocessorFn<AutomodPluginType> = options => {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (triggerObj[triggerName].match_mime_type) {
|
||||||
|
const white = triggerObj[triggerName].match_mime_type.whitelist_enabled;
|
||||||
|
const black = triggerObj[triggerName].match_mime_type.blacklist_enabled;
|
||||||
|
|
||||||
|
if (white && black) {
|
||||||
|
throw new StrictValidationError([
|
||||||
|
`Cannot have both blacklist and whitelist enabled at rule <${rule.name}/match_mime_type>`,
|
||||||
|
]);
|
||||||
|
} else if (!white && !black) {
|
||||||
|
throw new StrictValidationError([
|
||||||
|
`Must have either blacklist or whitelist enabled at rule <${rule.name}/match_mime_type>`,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import { KickTrigger } from "./kick";
|
||||||
import { LineSpamTrigger } from "./lineSpam";
|
import { LineSpamTrigger } from "./lineSpam";
|
||||||
import { LinkSpamTrigger } from "./linkSpam";
|
import { LinkSpamTrigger } from "./linkSpam";
|
||||||
import { MatchAttachmentTypeTrigger } from "./matchAttachmentType";
|
import { MatchAttachmentTypeTrigger } from "./matchAttachmentType";
|
||||||
|
import { MatchMimeTypeTrigger } from "./matchMimeType";
|
||||||
import { MatchInvitesTrigger } from "./matchInvites";
|
import { MatchInvitesTrigger } from "./matchInvites";
|
||||||
import { MatchLinksTrigger } from "./matchLinks";
|
import { MatchLinksTrigger } from "./matchLinks";
|
||||||
import { MatchRegexTrigger } from "./matchRegex";
|
import { MatchRegexTrigger } from "./matchRegex";
|
||||||
|
@ -37,6 +38,7 @@ export const availableTriggers: Record<string, AutomodTriggerBlueprint<any, any>
|
||||||
match_invites: MatchInvitesTrigger,
|
match_invites: MatchInvitesTrigger,
|
||||||
match_links: MatchLinksTrigger,
|
match_links: MatchLinksTrigger,
|
||||||
match_attachment_type: MatchAttachmentTypeTrigger,
|
match_attachment_type: MatchAttachmentTypeTrigger,
|
||||||
|
match_mime_type: MatchMimeTypeTrigger,
|
||||||
member_join: MemberJoinTrigger,
|
member_join: MemberJoinTrigger,
|
||||||
role_added: RoleAddedTrigger,
|
role_added: RoleAddedTrigger,
|
||||||
role_removed: RoleRemovedTrigger,
|
role_removed: RoleRemovedTrigger,
|
||||||
|
@ -72,6 +74,7 @@ export const AvailableTriggers = t.type({
|
||||||
match_invites: MatchInvitesTrigger.configType,
|
match_invites: MatchInvitesTrigger.configType,
|
||||||
match_links: MatchLinksTrigger.configType,
|
match_links: MatchLinksTrigger.configType,
|
||||||
match_attachment_type: MatchAttachmentTypeTrigger.configType,
|
match_attachment_type: MatchAttachmentTypeTrigger.configType,
|
||||||
|
match_mime_type: MatchMimeTypeTrigger.configType,
|
||||||
member_join: MemberJoinTrigger.configType,
|
member_join: MemberJoinTrigger.configType,
|
||||||
member_leave: MemberLeaveTrigger.configType,
|
member_leave: MemberLeaveTrigger.configType,
|
||||||
role_added: RoleAddedTrigger.configType,
|
role_added: RoleAddedTrigger.configType,
|
||||||
|
|
79
backend/src/plugins/Automod/triggers/matchMimeType.ts
Normal file
79
backend/src/plugins/Automod/triggers/matchMimeType.ts
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
import { automodTrigger } from "../helpers";
|
||||||
|
import * as t from "io-ts";
|
||||||
|
import { asSingleLine, messageSummary, verboseChannelMention } from "../../../utils";
|
||||||
|
import { GuildChannel, Util } from "discord.js";
|
||||||
|
|
||||||
|
interface MatchResultType {
|
||||||
|
matchedType: string;
|
||||||
|
mode: "blacklist" | "whitelist";
|
||||||
|
}
|
||||||
|
|
||||||
|
export const MatchMimeTypeTrigger = automodTrigger<MatchResultType>()({
|
||||||
|
configType: t.type({
|
||||||
|
mime_type_blacklist: t.array(t.string),
|
||||||
|
blacklist_enabled: t.boolean,
|
||||||
|
mime_type_whitelist: t.array(t.string),
|
||||||
|
whitelist_enabled: t.boolean,
|
||||||
|
}),
|
||||||
|
|
||||||
|
defaultConfig: {
|
||||||
|
mime_type_blacklist: [],
|
||||||
|
blacklist_enabled: false,
|
||||||
|
mime_type_whitelist: [],
|
||||||
|
whitelist_enabled: false,
|
||||||
|
},
|
||||||
|
|
||||||
|
async match({ context, triggerConfig: trigger }) {
|
||||||
|
if (!context.message) return;
|
||||||
|
|
||||||
|
const { attachments } = context.message.data;
|
||||||
|
if (!attachments) return null;
|
||||||
|
|
||||||
|
for (const attachment of attachments) {
|
||||||
|
const { contentType } = attachment;
|
||||||
|
|
||||||
|
const blacklist = trigger.blacklist_enabled
|
||||||
|
? (trigger.mime_type_blacklist ?? []).map(_t => _t.toLowerCase())
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (contentType && blacklist?.includes(contentType)) {
|
||||||
|
return {
|
||||||
|
extra: {
|
||||||
|
matchedType: contentType,
|
||||||
|
mode: "blacklist",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const whitelist = trigger.whitelist_enabled
|
||||||
|
? (trigger.mime_type_whitelist ?? []).map(_t => _t.toLowerCase())
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (whitelist && (!contentType || !whitelist.includes(contentType))) {
|
||||||
|
return {
|
||||||
|
extra: {
|
||||||
|
matchedType: contentType || "<unknown>",
|
||||||
|
mode: "whitelist",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
renderMatchInformation({ pluginData, contexts, matchResult }) {
|
||||||
|
const { message } = contexts[0];
|
||||||
|
const channel = pluginData.guild.channels.resolve(message!.channel_id);
|
||||||
|
const prettyChannel = verboseChannelMention(channel as GuildChannel);
|
||||||
|
const { matchedType, mode } = matchResult.extra;
|
||||||
|
|
||||||
|
return (
|
||||||
|
asSingleLine(`
|
||||||
|
Matched MIME type \`${Util.escapeInlineCode(matchedType)}\`
|
||||||
|
(${mode === "blacklist" ? "blacklisted" : "not in whitelist"})
|
||||||
|
in message (\`${message!.id}\`) in ${prettyChannel}
|
||||||
|
`) + messageSummary(message!)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue