Automod work vol 3

This commit is contained in:
Dragory 2020-07-27 22:19:34 +03:00
parent 0e9f65e0d5
commit 0f0728bc1c
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
18 changed files with 133 additions and 38 deletions

View file

@ -1,7 +1,7 @@
import { SavedMessage } from "../../../data/entities/SavedMessage";
import { eventListener, PluginData } from "knub";
import { eventListener } from "knub";
import { AutomodContext, AutomodPluginType } from "../types";
import { runAutomod } from "../functions/runAutomod";
import { RecentActionType } from "../constants";
export const RunAutomodOnJoinEvt = eventListener<AutomodPluginType>()(
"guildMemberAdd",
@ -9,8 +9,19 @@ export const RunAutomodOnJoinEvt = eventListener<AutomodPluginType>()(
const context: AutomodContext = {
timestamp: Date.now(),
user: member.user,
member,
joined: true,
};
pluginData.state.queue.add(() => runAutomod(pluginData, context));
pluginData.state.queue.add(() => {
pluginData.state.recentActions.push({
type: RecentActionType.MemberJoin,
context,
count: 1,
identifier: null,
});
runAutomod(pluginData, context);
});
},
);

View file

@ -6,9 +6,14 @@ import { addRecentActionsFromMessage } from "../functions/addRecentActionsFromMe
import moment from "moment-timezone";
export function runAutomodOnMessage(pluginData: PluginData<AutomodPluginType>, message: SavedMessage, isEdit: boolean) {
const user = pluginData.client.users.get(message.user_id);
const member = pluginData.guild.members.get(message.user_id);
const context: AutomodContext = {
timestamp: moment.utc(message.posted_at).valueOf(),
message,
user,
member,
};
pluginData.state.queue.add(async () => {

View file

@ -1,4 +1,3 @@
import moment from "moment-timezone";
import { AutomodContext, AutomodPluginType } from "../types";
import { PluginData } from "knub";
import { RECENT_ACTION_EXPIRY_TIME, RecentActionType } from "../constants";

View file

@ -52,7 +52,7 @@ export function createMessageSpamTrigger(spamType: RecentActionType, prettyName:
pluginData.state.recentSpam.push({
type: spamType,
userId: context.message.user_id,
userIds: [context.message.user_id],
archiveId,
timestamp: Date.now(),
});

View file

@ -2,8 +2,8 @@ import { PluginData } from "knub";
import { AutomodPluginType } from "../types";
import { RecentActionType } from "../constants";
export function findRecentSpam(pluginData: PluginData<AutomodPluginType>, type: RecentActionType, userId: string) {
export function findRecentSpam(pluginData: PluginData<AutomodPluginType>, type: RecentActionType, userId?: string) {
return pluginData.state.recentSpam.find(spam => {
return spam.type === type && spam.userId === userId;
return spam.type === type && (!userId || spam.userIds.includes(userId));
});
}

View file

@ -7,8 +7,10 @@ export function getMatchingRecentActions(
type: RecentActionType,
identifier: string | null,
since: number,
to: number,
to?: number,
) {
to = to || Date.now();
return pluginData.state.recentActions.filter(action => {
return (
action.type === type &&

View file

@ -1,4 +1,3 @@
import * as t from "io-ts";
import { SavedMessage } from "../../../data/entities/SavedMessage";
import { resolveMember } from "../../../utils";
import { PluginData } from "knub";

View file

@ -1,5 +1,5 @@
import { PluginData } from "knub";
import { AutomodContext, AutomodPluginType, TRule } from "../types";
import { AutomodContext, AutomodPluginType } from "../types";
import { availableTriggers } from "../triggers/availableTriggers";
import { availableActions } from "../actions/availableActions";
import { AutomodTriggerMatchResult } from "../helpers";

View file

@ -0,0 +1,5 @@
import { RecentAction } from "../types";
export function sumRecentActionCounts(actions: RecentAction[]) {
return actions.reduce((total, action) => total + action.count, 0);
}

View file

@ -7,7 +7,7 @@ export interface AutomodTriggerMatchResult<TExtra extends any = unknown> {
extraContexts?: AutomodContext[];
extra?: TExtra;
silentClean?: boolean;
silentClean?: boolean; // TODO: Maybe generalize to a "silent" value in general, which mutes alert/log
}
type AutomodTriggerMatchFn<TConfigType, TMatchResultExtra> = (meta: {

View file

@ -12,6 +12,8 @@ import { MatchRegexTrigger } from "./matchRegex";
import { MatchInvitesTrigger } from "./matchInvites";
import { MatchLinksTrigger } from "./matchLinks";
import { MatchAttachmentTypeTrigger } from "./matchAttachmentType";
import { MemberJoinSpamTrigger } from "./memberJoinSpam";
import { MemberJoinTrigger } from "./memberJoin";
export const availableTriggers: Record<string, AutomodTriggerBlueprint<any, any>> = {
match_words: MatchWordsTrigger,
@ -19,6 +21,7 @@ export const availableTriggers: Record<string, AutomodTriggerBlueprint<any, any>
match_invites: MatchInvitesTrigger,
match_links: MatchLinksTrigger,
match_attachment_type: MatchAttachmentTypeTrigger,
member_join: MemberJoinTrigger,
message_spam: MessageSpamTrigger,
mention_spam: MentionSpamTrigger,
@ -27,6 +30,7 @@ export const availableTriggers: Record<string, AutomodTriggerBlueprint<any, any>
emoji_spam: EmojiSpamTrigger,
line_spam: LineSpamTrigger,
character_spam: CharacterSpamTrigger,
member_join_spam: MemberJoinSpamTrigger,
};
export const AvailableTriggers = t.type({
@ -35,6 +39,7 @@ export const AvailableTriggers = t.type({
match_invites: MatchInvitesTrigger.configType,
match_links: MatchLinksTrigger.configType,
match_attachment_type: MatchAttachmentTypeTrigger.configType,
member_join: MemberJoinTrigger.configType,
message_spam: MessageSpamTrigger.configType,
mention_spam: MentionSpamTrigger.configType,
@ -43,4 +48,5 @@ export const AvailableTriggers = t.type({
emoji_spam: EmojiSpamTrigger.configType,
line_spam: LineSpamTrigger.configType,
character_spam: CharacterSpamTrigger.configType,
member_join_spam: MemberJoinSpamTrigger.configType,
});

View file

@ -1,19 +1,6 @@
import * as t from "io-ts";
import { transliterate } from "transliteration";
import escapeStringRegexp from "escape-string-regexp";
import { AnyInvite, Attachment, GuildInvite } from "eris";
import { automodTrigger } from "../helpers";
import {
asSingleLine,
disableCodeBlocks,
disableInlineCode,
getInviteCodesInString,
isGuildInvite,
resolveInvite,
tNullable,
verboseChannelMention,
} from "../../../utils";
import { MatchableTextType, matchMultipleTextTypesOnMessage } from "../functions/matchMultipleTextTypesOnMessage";
import { asSingleLine, disableCodeBlocks, disableInlineCode, verboseChannelMention } from "../../../utils";
interface MatchResultType {
matchedType: string;

View file

@ -1,12 +1,8 @@
import * as t from "io-ts";
import { transliterate } from "transliteration";
import escapeStringRegexp from "escape-string-regexp";
import { AnyInvite, GuildInvite } from "eris";
import { GuildInvite } from "eris";
import { automodTrigger } from "../helpers";
import {
asSingleLine,
disableCodeBlocks,
disableInlineCode,
getInviteCodesInString,
isGuildInvite,
resolveInvite,

View file

@ -1,16 +1,11 @@
import * as t from "io-ts";
import { transliterate } from "transliteration";
import escapeStringRegexp from "escape-string-regexp";
import { AnyInvite, GuildInvite } from "eris";
import { automodTrigger } from "../helpers";
import {
asSingleLine,
disableCodeBlocks,
disableInlineCode,
getInviteCodesInString,
getUrlsInString,
isGuildInvite,
resolveInvite,
tNullable,
verboseChannelMention,
} from "../../../utils";

View file

@ -1,6 +1,5 @@
import * as t from "io-ts";
import { transliterate } from "transliteration";
import escapeStringRegexp from "escape-string-regexp";
import { automodTrigger } from "../helpers";
import { disableInlineCode, verboseChannelMention } from "../../../utils";
import { MatchableTextType, matchMultipleTextTypesOnMessage } from "../functions/matchMultipleTextTypesOnMessage";

View file

@ -0,0 +1,34 @@
import * as t from "io-ts";
import { automodTrigger } from "../helpers";
import { convertDelayStringToMS, tDelayString } from "../../../utils";
export const MemberJoinTrigger = automodTrigger<unknown>()({
configType: t.type({
only_new: t.boolean,
new_threshold: tDelayString,
}),
defaultConfig: {
only_new: false,
new_threshold: "1h",
},
async match({ pluginData, context, triggerConfig }) {
if (!context.joined || !context.member) {
return;
}
if (triggerConfig.only_new) {
const threshold = Date.now() - convertDelayStringToMS(triggerConfig.new_threshold);
if (context.member.createdAt >= threshold) {
return {};
}
}
return {};
},
renderMatchInformation({ pluginData, contexts, triggerConfig }) {
return null;
},
});

View file

@ -0,0 +1,55 @@
import * as t from "io-ts";
import { automodTrigger } from "../helpers";
import { convertDelayStringToMS, tDelayString } from "../../../utils";
import { getMatchingRecentActions } from "../functions/getMatchingRecentActions";
import { RecentActionType } from "../constants";
import { sumRecentActionCounts } from "../functions/sumRecentActionCounts";
import { findRecentSpam } from "../functions/findRecentSpam";
export const MemberJoinSpamTrigger = automodTrigger<unknown>()({
configType: t.type({
amount: t.number,
within: tDelayString,
}),
defaultConfig: {},
async match({ pluginData, context, triggerConfig }) {
if (!context.joined || !context.member) {
return;
}
const recentSpam = findRecentSpam(pluginData, RecentActionType.MemberJoin);
if (recentSpam) {
context.actioned = true;
return {};
}
const since = Date.now() - convertDelayStringToMS(triggerConfig.within);
const matchingActions = getMatchingRecentActions(pluginData, RecentActionType.MemberJoin, null, since);
const totalCount = sumRecentActionCounts(matchingActions);
if (totalCount >= triggerConfig.amount) {
const contexts = [context, ...matchingActions.map(a => a.context).filter(c => c !== context)];
for (const _context of contexts) {
_context.actioned = true;
}
pluginData.state.recentSpam.push({
type: RecentActionType.MemberJoin,
timestamp: Date.now(),
archiveId: null,
userIds: [],
});
return {
extraContexts: contexts,
};
}
},
renderMatchInformation({ pluginData, contexts, triggerConfig }) {
return null;
},
});

View file

@ -4,7 +4,7 @@ import { BasePluginType } from "knub";
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
import { GuildLogs } from "../../data/GuildLogs";
import { SavedMessage } from "../../data/entities/SavedMessage";
import { User } from "eris";
import { Member, User } from "eris";
import { AvailableTriggers } from "./triggers/availableTriggers";
import { AvailableActions } from "./actions/availableActions";
import { Queue } from "../../Queue";
@ -71,6 +71,8 @@ export interface AutomodContext {
user?: User | UnknownUser;
message?: SavedMessage;
member?: Member;
joined?: boolean;
}
export interface RecentAction {
@ -83,6 +85,6 @@ export interface RecentAction {
export interface RecentSpam {
archiveId: string;
type: RecentActionType;
userId: string;
userIds: string[];
timestamp: number;
}