feat: option to trigger antiraid_level only on change (#424)

Co-authored-by: Almeida <github@almeidx.dev>
This commit is contained in:
Ben Richeson 2023-12-29 07:52:43 -05:00 committed by GitHub
parent d51461ee3a
commit 8a4a2d3647
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 3 deletions

View file

@ -5,13 +5,15 @@ import { AutomodContext, AutomodPluginType } from "../types";
export async function runAutomodOnAntiraidLevel(
pluginData: GuildPluginData<AutomodPluginType>,
level: string | null,
newLevel: string | null,
oldLevel: string | null,
user?: User,
) {
const context: AutomodContext = {
timestamp: Date.now(),
antiraid: {
level,
level: newLevel,
oldLevel,
},
user,
};

View file

@ -9,10 +9,11 @@ export async function setAntiraidLevel(
newLevel: string | null,
user?: User,
) {
const oldLevel = pluginData.state.cachedAntiraidLevel;
pluginData.state.cachedAntiraidLevel = newLevel;
await pluginData.state.antiraidLevels.set(newLevel);
runAutomodOnAntiraidLevel(pluginData, newLevel, user);
runAutomodOnAntiraidLevel(pluginData, newLevel, oldLevel, user);
const logs = pluginData.getPlugin(LogsPlugin);

View file

@ -7,6 +7,7 @@ interface AntiraidLevelTriggerResult {}
export const AntiraidLevelTrigger = automodTrigger<AntiraidLevelTriggerResult>()({
configType: t.type({
level: tNullable(t.string),
only_on_change: tNullable(t.boolean),
}),
defaultConfig: {},
@ -20,6 +21,14 @@ export const AntiraidLevelTrigger = automodTrigger<AntiraidLevelTriggerResult>()
return;
}
if (
triggerConfig.only_on_change &&
context.antiraid.oldLevel !== undefined &&
context.antiraid.level === context.antiraid.oldLevel
) {
return;
}
return {
extra: {},
};

View file

@ -130,6 +130,7 @@ export interface AutomodContext {
};
antiraid?: {
level: string | null;
oldLevel?: string | null;
};
threadChange?: {
created?: ThreadChannel;