automod: add role_added and role_removed triggers
This commit is contained in:
parent
bfa9cf55a7
commit
4c7a51f586
9 changed files with 187 additions and 4 deletions
38
backend/src/plugins/Automod/functions/ignoredRoleChanges.ts
Normal file
38
backend/src/plugins/Automod/functions/ignoredRoleChanges.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import { PluginData } from "knub";
|
||||
import { AutomodPluginType } from "../types";
|
||||
import { MINUTES } from "../../../utils";
|
||||
|
||||
const IGNORED_ROLE_CHANGE_LIFETIME = 5 * MINUTES;
|
||||
|
||||
function cleanupIgnoredRoleChanges(pluginData: PluginData<AutomodPluginType>) {
|
||||
const cutoff = Date.now() - IGNORED_ROLE_CHANGE_LIFETIME;
|
||||
for (const ignoredChange of pluginData.state.ignoredRoleChanges.values()) {
|
||||
if (ignoredChange.timestamp < cutoff) {
|
||||
pluginData.state.ignoredRoleChanges.delete(ignoredChange);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function ignoreRoleChange(pluginData: PluginData<AutomodPluginType>, memberId: string, roleId: string) {
|
||||
pluginData.state.ignoredRoleChanges.add({
|
||||
memberId,
|
||||
roleId,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
cleanupIgnoredRoleChanges(pluginData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Whether the role change should be ignored
|
||||
*/
|
||||
export function consumeIgnoredRoleChange(pluginData: PluginData<AutomodPluginType>, memberId: string, roleId: string) {
|
||||
for (const ignoredChange of pluginData.state.ignoredRoleChanges.values()) {
|
||||
if (ignoredChange.memberId === memberId && ignoredChange.roleId === roleId) {
|
||||
pluginData.state.ignoredRoleChanges.delete(ignoredChange);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue