mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
counters: move triggers to counters plugin; architectural tweaks
This commit is contained in:
parent
7f75d6d8d3
commit
ab8ea2e7e5
17 changed files with 357 additions and 200 deletions
|
@ -29,7 +29,6 @@ import { logger } from "../../logger";
|
|||
import { discardRegExpRunner, getRegExpRunner } from "../../regExpRunners";
|
||||
import { RunAutomodOnMemberUpdate } from "./events/RunAutomodOnMemberUpdate";
|
||||
import { CountersPlugin } from "../Counters/CountersPlugin";
|
||||
import { parseCondition } from "../../data/GuildCounters";
|
||||
import { runAutomodOnCounterTrigger } from "./events/runAutomodOnCounterTrigger";
|
||||
import { runAutomodOnModAction } from "./events/runAutomodOnModAction";
|
||||
import { registerEventListenersFromMap } from "../../utils/registerEventListenersFromMap";
|
||||
|
@ -114,15 +113,6 @@ const configPreprocessor: ConfigPreprocessorFn<AutomodPluginType> = options => {
|
|||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (triggerName === "counter") {
|
||||
const parsedCondition = parseCondition(triggerObj[triggerName]!.condition);
|
||||
if (parsedCondition == null) {
|
||||
throw new StrictValidationError([
|
||||
`Invalid counter condition '${triggerObj[triggerName]!.condition}' in rule <${rule.name}>`,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -229,23 +219,14 @@ export const AutomodPlugin = zeppelinGuildPlugin<AutomodPluginType>()("automod",
|
|||
async onAfterLoad(pluginData) {
|
||||
const countersPlugin = pluginData.getPlugin(CountersPlugin);
|
||||
|
||||
pluginData.state.onCounterTrigger = (name, condition, channelId, userId) => {
|
||||
runAutomodOnCounterTrigger(pluginData, name, condition, channelId, userId, false);
|
||||
pluginData.state.onCounterTrigger = (name, triggerName, channelId, userId) => {
|
||||
runAutomodOnCounterTrigger(pluginData, name, triggerName, channelId, userId, false);
|
||||
};
|
||||
|
||||
pluginData.state.onCounterReverseTrigger = (name, condition, channelId, userId) => {
|
||||
runAutomodOnCounterTrigger(pluginData, name, condition, channelId, userId, true);
|
||||
pluginData.state.onCounterReverseTrigger = (name, triggerName, channelId, userId) => {
|
||||
runAutomodOnCounterTrigger(pluginData, name, triggerName, channelId, userId, true);
|
||||
};
|
||||
|
||||
const config = pluginData.config.get();
|
||||
for (const rule of Object.values(config.rules)) {
|
||||
for (const trigger of rule.triggers) {
|
||||
if (trigger.counter) {
|
||||
await countersPlugin.initCounterTrigger(trigger.counter.name, trigger.counter.condition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
countersPlugin.onCounterEvent("trigger", pluginData.state.onCounterTrigger);
|
||||
countersPlugin.onCounterEvent("reverseTrigger", pluginData.state.onCounterReverseTrigger);
|
||||
|
||||
|
|
|
@ -2,30 +2,38 @@ import { GuildPluginData } from "knub";
|
|||
import { AutomodContext, AutomodPluginType } from "../types";
|
||||
import { runAutomod } from "../functions/runAutomod";
|
||||
import { resolveMember, resolveUser, UnknownUser } from "../../../utils";
|
||||
import { CountersPlugin } from "../../Counters/CountersPlugin";
|
||||
|
||||
export async function runAutomodOnCounterTrigger(
|
||||
pluginData: GuildPluginData<AutomodPluginType>,
|
||||
counterName: string,
|
||||
condition: string,
|
||||
triggerName: string,
|
||||
channelId: string | null,
|
||||
userId: string | null,
|
||||
reverse: boolean,
|
||||
) {
|
||||
const user = userId ? await resolveUser(pluginData.client, userId) : undefined;
|
||||
|
||||
const member = (userId && (await resolveMember(pluginData.client, pluginData.guild, userId))) || undefined;
|
||||
|
||||
const prettyCounterName = pluginData.getPlugin(CountersPlugin).getPrettyNameForCounter(counterName);
|
||||
const prettyTriggerName = pluginData
|
||||
.getPlugin(CountersPlugin)
|
||||
.getPrettyNameForCounterTrigger(counterName, triggerName);
|
||||
|
||||
const context: AutomodContext = {
|
||||
timestamp: Date.now(),
|
||||
counterTrigger: {
|
||||
name: counterName,
|
||||
condition,
|
||||
counter: counterName,
|
||||
trigger: triggerName,
|
||||
prettyCounter: prettyCounterName,
|
||||
prettyTrigger: prettyTriggerName,
|
||||
channelId,
|
||||
userId,
|
||||
reverse,
|
||||
},
|
||||
user: user instanceof UnknownUser ? undefined : user,
|
||||
member,
|
||||
// TODO: Channel
|
||||
};
|
||||
|
||||
pluginData.state.queue.add(async () => {
|
||||
|
|
|
@ -9,8 +9,8 @@ interface CounterTriggerResult {}
|
|||
|
||||
export const CounterTrigger = automodTrigger<CounterTriggerResult>()({
|
||||
configType: t.type({
|
||||
name: t.string,
|
||||
condition: t.string,
|
||||
counter: t.string,
|
||||
trigger: t.string,
|
||||
reverse: tNullable(t.boolean),
|
||||
}),
|
||||
|
||||
|
@ -21,11 +21,11 @@ export const CounterTrigger = automodTrigger<CounterTriggerResult>()({
|
|||
return;
|
||||
}
|
||||
|
||||
if (context.counterTrigger.name !== triggerConfig.name) {
|
||||
if (context.counterTrigger.counter !== triggerConfig.counter) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.counterTrigger.condition !== triggerConfig.condition) {
|
||||
if (context.counterTrigger.trigger !== triggerConfig.trigger) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -40,7 +40,13 @@ export const CounterTrigger = automodTrigger<CounterTriggerResult>()({
|
|||
},
|
||||
|
||||
renderMatchInformation({ matchResult, pluginData, contexts, triggerConfig }) {
|
||||
// TODO: Show user, channel, reverse
|
||||
return `Matched counter \`${triggerConfig.name} ${triggerConfig.condition}\``;
|
||||
let str = `Matched counter trigger \`${contexts[0].counterTrigger!.prettyCounter} / ${
|
||||
contexts[0].counterTrigger!.prettyTrigger
|
||||
}\``;
|
||||
if (contexts[0].counterTrigger!.reverse) {
|
||||
str += " (reverse)";
|
||||
}
|
||||
|
||||
return str;
|
||||
},
|
||||
});
|
||||
|
|
|
@ -103,8 +103,10 @@ export interface AutomodContext {
|
|||
actioned?: boolean;
|
||||
|
||||
counterTrigger?: {
|
||||
name: string;
|
||||
condition: string;
|
||||
counter: string;
|
||||
trigger: string;
|
||||
prettyCounter: string;
|
||||
prettyTrigger: string;
|
||||
channelId: string | null;
|
||||
userId: string | null;
|
||||
reverse: boolean;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue