mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-21 08:45:03 +00:00
feat(automod): MIME type trigger
This commit is contained in:
parent
6d28dcbfdf
commit
88f0e0e558
4 changed files with 234 additions and 2072 deletions
|
@ -13,6 +13,7 @@ import { LinkSpamTrigger } from "./linkSpam";
|
||||||
import { MatchAttachmentTypeTrigger } from "./matchAttachmentType";
|
import { MatchAttachmentTypeTrigger } from "./matchAttachmentType";
|
||||||
import { MatchInvitesTrigger } from "./matchInvites";
|
import { MatchInvitesTrigger } from "./matchInvites";
|
||||||
import { MatchLinksTrigger } from "./matchLinks";
|
import { MatchLinksTrigger } from "./matchLinks";
|
||||||
|
import { MatchMimeTypeTrigger } from "./matchMimeType";
|
||||||
import { MatchRegexTrigger } from "./matchRegex";
|
import { MatchRegexTrigger } from "./matchRegex";
|
||||||
import { MatchWordsTrigger } from "./matchWords";
|
import { MatchWordsTrigger } from "./matchWords";
|
||||||
import { MemberJoinTrigger } from "./memberJoin";
|
import { MemberJoinTrigger } from "./memberJoin";
|
||||||
|
@ -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,
|
||||||
|
|
84
backend/src/plugins/Automod/triggers/matchMimeType.ts
Normal file
84
backend/src/plugins/Automod/triggers/matchMimeType.ts
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
import { automodTrigger } from "../helpers";
|
||||||
|
import * as t from "io-ts";
|
||||||
|
import fetch from "node-fetch";
|
||||||
|
import { fromBuffer } from "file-type";
|
||||||
|
import { asSingleLine, disableInlineCode, messageSummary, verboseChannelMention } from "src/utils";
|
||||||
|
|
||||||
|
interface MatchResultType {
|
||||||
|
matchedType: string;
|
||||||
|
mode: "blacklist" | "whitelist";
|
||||||
|
}
|
||||||
|
|
||||||
|
export const MatchMimeTypeTrigger = automodTrigger<MatchResultType>()({
|
||||||
|
configType: t.type({
|
||||||
|
mimetype_blacklist: t.array(t.string),
|
||||||
|
blacklist_enabled: t.boolean,
|
||||||
|
mimetype_whitelist: t.array(t.string),
|
||||||
|
whitelist_enabled: t.boolean,
|
||||||
|
}),
|
||||||
|
|
||||||
|
defaultConfig: {
|
||||||
|
mimetype_blacklist: [],
|
||||||
|
blacklist_enabled: false,
|
||||||
|
mimetype_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 res = await fetch(attachment.proxyURL);
|
||||||
|
const mimeType = await fromBuffer(await res.buffer());
|
||||||
|
if (!mimeType) return;
|
||||||
|
|
||||||
|
const blacklist = trigger.blacklist_enabled
|
||||||
|
? (trigger.mimetype_blacklist || []).map(_t => _t.toLowerCase())
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (blacklist?.includes(mimeType.mime)) {
|
||||||
|
return {
|
||||||
|
extra: {
|
||||||
|
matchedType: mimeType.mime,
|
||||||
|
mode: "blacklist",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const whitelist = trigger.whitelist_enabled
|
||||||
|
? (trigger.mimetype_whitelist || []).map(_t => _t.toLowerCase())
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (whitelist?.includes(mimeType.mime)) {
|
||||||
|
return {
|
||||||
|
extra: {
|
||||||
|
matchedType: mimeType.mime,
|
||||||
|
mode: "whitelist",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
renderMatchInformation({ pluginData, contexts, matchResult }) {
|
||||||
|
const [context] = contexts;
|
||||||
|
const { message } = context;
|
||||||
|
if (!message) return;
|
||||||
|
const channel = pluginData.guild.channels.cache.get(message.channel_id);
|
||||||
|
const prettyChannel = verboseChannelMention(channel);
|
||||||
|
const { matchedType, mode } = matchResult.extra;
|
||||||
|
|
||||||
|
return (
|
||||||
|
asSingleLine(`
|
||||||
|
Matched MIME type \`${disableInlineCode(matchedType)}\`
|
||||||
|
(${mode === "blacklist" ? "(blacklisted)" : "(not in whitelist)"})
|
||||||
|
in message (\`${message.id}\`) in ${prettyChannel}
|
||||||
|
`) + messageSummary(message)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
2215
package-lock.json
generated
2215
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -9,6 +9,7 @@
|
||||||
"codestyle-check": "prettier --check \"./{backend,dashboard}/{,!(node_modules)/**/}/*.ts\""
|
"codestyle-check": "prettier --check \"./{backend,dashboard}/{,!(node_modules)/**/}/*.ts\""
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/node-fetch": "^2.5.12",
|
||||||
"husky": "^3.0.9",
|
"husky": "^3.0.9",
|
||||||
"lint-staged": "^9.4.2",
|
"lint-staged": "^9.4.2",
|
||||||
"prettier": "^1.19.1",
|
"prettier": "^1.19.1",
|
||||||
|
@ -27,5 +28,9 @@
|
||||||
"prettier --write",
|
"prettier --write",
|
||||||
"git add"
|
"git add"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"file-type": "^16.5.2",
|
||||||
|
"node-fetch": "^2.6.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue