2020-07-27 20:42:10 +03:00
|
|
|
import * as t from "io-ts";
|
|
|
|
import { transliterate } from "transliteration";
|
|
|
|
import { automodTrigger } from "../helpers";
|
|
|
|
import { disableInlineCode, verboseChannelMention } from "../../../utils";
|
|
|
|
import { MatchableTextType, matchMultipleTextTypesOnMessage } from "../functions/matchMultipleTextTypesOnMessage";
|
2020-07-29 22:42:17 +03:00
|
|
|
import { getTextMatchPartialSummary } from "../functions/getTextMatchPartialSummary";
|
2020-08-05 01:15:36 +03:00
|
|
|
import { allowTimeout } from "../../../RegExpRunner";
|
|
|
|
import { TRegex } from "../../../validatorUtils";
|
2020-07-27 20:42:10 +03:00
|
|
|
|
2020-07-27 21:51:03 +03:00
|
|
|
interface MatchResultType {
|
|
|
|
pattern: string;
|
|
|
|
type: MatchableTextType;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MatchRegexTrigger = automodTrigger<MatchResultType>()({
|
2020-07-27 20:42:10 +03:00
|
|
|
configType: t.type({
|
2020-08-05 01:15:36 +03:00
|
|
|
patterns: t.array(TRegex),
|
2020-07-27 20:42:10 +03:00
|
|
|
case_sensitive: t.boolean,
|
|
|
|
normalize: t.boolean,
|
|
|
|
match_messages: t.boolean,
|
|
|
|
match_embeds: t.boolean,
|
|
|
|
match_visible_names: t.boolean,
|
|
|
|
match_usernames: t.boolean,
|
|
|
|
match_nicknames: t.boolean,
|
|
|
|
match_custom_status: t.boolean,
|
|
|
|
}),
|
|
|
|
|
|
|
|
defaultConfig: {
|
|
|
|
case_sensitive: false,
|
|
|
|
normalize: false,
|
|
|
|
match_messages: true,
|
|
|
|
match_embeds: true,
|
|
|
|
match_visible_names: false,
|
|
|
|
match_usernames: false,
|
|
|
|
match_nicknames: false,
|
|
|
|
match_custom_status: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
async match({ pluginData, context, triggerConfig: trigger }) {
|
|
|
|
if (!context.message) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for await (let [type, str] of matchMultipleTextTypesOnMessage(pluginData, trigger, context.message)) {
|
|
|
|
if (trigger.normalize) {
|
|
|
|
str = transliterate(str);
|
|
|
|
}
|
|
|
|
|
2020-07-27 21:51:03 +03:00
|
|
|
for (const sourceRegex of trigger.patterns) {
|
2020-08-05 01:15:36 +03:00
|
|
|
const regex = new RegExp(sourceRegex.source, trigger.case_sensitive && !sourceRegex.ignoreCase ? "" : "i");
|
|
|
|
const matches = await pluginData.state.regexRunner.exec(regex, str).catch(allowTimeout);
|
|
|
|
if (matches?.length) {
|
2020-07-27 20:42:10 +03:00
|
|
|
return {
|
|
|
|
extra: {
|
2020-07-27 21:51:03 +03:00
|
|
|
pattern: sourceRegex.source,
|
2020-07-27 20:42:10 +03:00
|
|
|
type,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderMatchInformation({ pluginData, contexts, matchResult }) {
|
2020-07-29 22:42:17 +03:00
|
|
|
const partialSummary = getTextMatchPartialSummary(pluginData, matchResult.extra.type, contexts[0]);
|
|
|
|
return `Matched regex \`${disableInlineCode(matchResult.extra.pattern)}\` in ${partialSummary}`;
|
2020-07-27 20:42:10 +03:00
|
|
|
},
|
|
|
|
});
|