Skip to content

Commit

Permalink
feat: option to trigger antiraid_level only on change (#424)
Browse files Browse the repository at this point in the history
Co-authored-by: Almeida <[email protected]>
  • Loading branch information
Benricheson101 and almeidx authored Dec 29, 2023
1 parent d51461e commit 8a4a2d3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
3 changes: 2 additions & 1 deletion backend/src/plugins/Automod/functions/setAntiraidLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions backend/src/plugins/Automod/triggers/antiraidLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand All @@ -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: {},
};
Expand Down
1 change: 1 addition & 0 deletions backend/src/plugins/Automod/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export interface AutomodContext {
};
antiraid?: {
level: string | null;
oldLevel?: string | null;
};
threadChange?: {
created?: ThreadChannel;
Expand Down

0 comments on commit 8a4a2d3

Please sign in to comment.