Fix error when using an unknown counter from Automod
This commit is contained in:
parent
8763a06ef1
commit
ad8aab3937
4 changed files with 28 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
||||||
import * as t from "io-ts";
|
import * as t from "io-ts";
|
||||||
import { automodAction } from "../helpers";
|
import { automodAction } from "../helpers";
|
||||||
import { CountersPlugin } from "../../Counters/CountersPlugin";
|
import { CountersPlugin } from "../../Counters/CountersPlugin";
|
||||||
|
import { LogType } from "../../../data/LogType";
|
||||||
|
|
||||||
export const AddToCounterAction = automodAction({
|
export const AddToCounterAction = automodAction({
|
||||||
configType: t.type({
|
configType: t.type({
|
||||||
|
@ -10,8 +11,15 @@ export const AddToCounterAction = automodAction({
|
||||||
|
|
||||||
defaultConfig: {},
|
defaultConfig: {},
|
||||||
|
|
||||||
async apply({ pluginData, contexts, actionConfig, matchResult }) {
|
async apply({ pluginData, contexts, actionConfig, matchResult, ruleName }) {
|
||||||
const countersPlugin = pluginData.getPlugin(CountersPlugin);
|
const countersPlugin = pluginData.getPlugin(CountersPlugin);
|
||||||
|
if (!countersPlugin.counterExists(actionConfig.counter)) {
|
||||||
|
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
||||||
|
body: `Unknown counter \`${actionConfig.counter}\` in \`add_to_counter\` action of Automod rule \`${ruleName}\``,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
countersPlugin.changeCounterValue(
|
countersPlugin.changeCounterValue(
|
||||||
actionConfig.counter,
|
actionConfig.counter,
|
||||||
contexts[0].message?.channel_id || null,
|
contexts[0].message?.channel_id || null,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import * as t from "io-ts";
|
import * as t from "io-ts";
|
||||||
import { automodAction } from "../helpers";
|
import { automodAction } from "../helpers";
|
||||||
import { CountersPlugin } from "../../Counters/CountersPlugin";
|
import { CountersPlugin } from "../../Counters/CountersPlugin";
|
||||||
|
import { LogType } from "../../../data/LogType";
|
||||||
|
|
||||||
export const SetCounterAction = automodAction({
|
export const SetCounterAction = automodAction({
|
||||||
configType: t.type({
|
configType: t.type({
|
||||||
|
@ -10,8 +11,15 @@ export const SetCounterAction = automodAction({
|
||||||
|
|
||||||
defaultConfig: {},
|
defaultConfig: {},
|
||||||
|
|
||||||
async apply({ pluginData, contexts, actionConfig, matchResult }) {
|
async apply({ pluginData, contexts, actionConfig, matchResult, ruleName }) {
|
||||||
const countersPlugin = pluginData.getPlugin(CountersPlugin);
|
const countersPlugin = pluginData.getPlugin(CountersPlugin);
|
||||||
|
if (!countersPlugin.counterExists(actionConfig.counter)) {
|
||||||
|
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
||||||
|
body: `Unknown counter \`${actionConfig.counter}\` in \`add_to_counter\` action of Automod rule \`${ruleName}\``,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
countersPlugin.setCounterValue(
|
countersPlugin.setCounterValue(
|
||||||
actionConfig.counter,
|
actionConfig.counter,
|
||||||
contexts[0].message?.channel_id || null,
|
contexts[0].message?.channel_id || null,
|
||||||
|
|
|
@ -24,6 +24,7 @@ import {
|
||||||
} from "../../data/entities/CounterTrigger";
|
} from "../../data/entities/CounterTrigger";
|
||||||
import { getPrettyNameForCounter } from "./functions/getPrettyNameForCounter";
|
import { getPrettyNameForCounter } from "./functions/getPrettyNameForCounter";
|
||||||
import { getPrettyNameForCounterTrigger } from "./functions/getPrettyNameForCounterTrigger";
|
import { getPrettyNameForCounterTrigger } from "./functions/getPrettyNameForCounterTrigger";
|
||||||
|
import { counterExists } from "./functions/counterExists";
|
||||||
|
|
||||||
const MAX_COUNTERS = 5;
|
const MAX_COUNTERS = 5;
|
||||||
const MAX_TRIGGERS_PER_COUNTER = 5;
|
const MAX_TRIGGERS_PER_COUNTER = 5;
|
||||||
|
@ -115,6 +116,8 @@ export const CountersPlugin = zeppelinGuildPlugin<CountersPluginType>()("counter
|
||||||
configPreprocessor,
|
configPreprocessor,
|
||||||
|
|
||||||
public: {
|
public: {
|
||||||
|
counterExists: mapToPublicFn(counterExists),
|
||||||
|
|
||||||
// Change a counter's value by a relative amount, e.g. +5
|
// Change a counter's value by a relative amount, e.g. +5
|
||||||
changeCounterValue: mapToPublicFn(changeCounterValue),
|
changeCounterValue: mapToPublicFn(changeCounterValue),
|
||||||
|
|
||||||
|
|
7
backend/src/plugins/Counters/functions/counterExists.ts
Normal file
7
backend/src/plugins/Counters/functions/counterExists.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { GuildPluginData } from "knub";
|
||||||
|
import { CountersPluginType } from "../types";
|
||||||
|
|
||||||
|
export function counterExists(pluginData: GuildPluginData<CountersPluginType>, counterName: string) {
|
||||||
|
const config = pluginData.config.get();
|
||||||
|
return config.counters[counterName] != null;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue