Automod work
This commit is contained in:
parent
140ba84544
commit
f657b169df
32 changed files with 1099 additions and 5 deletions
83
backend/src/plugins/Automod/functions/runAutomod.ts
Normal file
83
backend/src/plugins/Automod/functions/runAutomod.ts
Normal file
|
@ -0,0 +1,83 @@
|
|||
import { PluginData } from "knub";
|
||||
import { AutomodContext, AutomodPluginType, TRule } from "../types";
|
||||
import { availableTriggers } from "../triggers/availableTriggers";
|
||||
import { availableActions } from "../actions/availableActions";
|
||||
import { AutomodTriggerMatchResult } from "../helpers";
|
||||
import { CleanAction } from "../actions/clean";
|
||||
|
||||
export async function runAutomod(pluginData: PluginData<AutomodPluginType>, context: AutomodContext) {
|
||||
const userId = context.user?.id || context.message?.user_id;
|
||||
const member = userId && pluginData.guild.members.get(userId);
|
||||
const channelId = context.message?.channel_id;
|
||||
const channel = channelId && pluginData.guild.channels.get(channelId);
|
||||
const categoryId = channel?.parentID;
|
||||
|
||||
const config = pluginData.config.getMatchingConfig({
|
||||
channelId,
|
||||
categoryId,
|
||||
userId,
|
||||
member,
|
||||
});
|
||||
|
||||
for (const [ruleName, rule] of Object.entries(config.rules)) {
|
||||
if (rule.enabled === false) continue;
|
||||
|
||||
let matchResult: AutomodTriggerMatchResult<any>;
|
||||
let matchSummary: string;
|
||||
let contexts: AutomodContext[];
|
||||
|
||||
triggerLoop: for (const triggerItem of rule.triggers) {
|
||||
for (const [triggerName, triggerConfig] of Object.entries(triggerItem)) {
|
||||
const trigger = availableTriggers[triggerName];
|
||||
matchResult = await trigger.match({
|
||||
ruleName,
|
||||
pluginData,
|
||||
context,
|
||||
triggerConfig,
|
||||
});
|
||||
|
||||
if (matchResult) {
|
||||
contexts = [context, ...(matchResult.extraContexts || [])];
|
||||
|
||||
for (const _context of contexts) {
|
||||
_context.actioned = true;
|
||||
}
|
||||
|
||||
if (matchResult.silentClean) {
|
||||
await CleanAction.apply({
|
||||
ruleName,
|
||||
pluginData,
|
||||
contexts,
|
||||
actionConfig: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
matchSummary = await trigger.renderMatchInformation({
|
||||
ruleName,
|
||||
pluginData,
|
||||
contexts,
|
||||
matchResult,
|
||||
triggerConfig,
|
||||
});
|
||||
|
||||
break triggerLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (matchResult) {
|
||||
for (const [actionName, actionConfig] of Object.entries(rule.actions)) {
|
||||
const action = availableActions[actionName];
|
||||
action.apply({
|
||||
ruleName,
|
||||
pluginData,
|
||||
contexts,
|
||||
actionConfig,
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue