3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 20:35:02 +00:00

automod: add triggers for mod actions

This commit is contained in:
Dragory 2021-02-14 16:58:02 +02:00
parent 5ffc3e7cc4
commit 93912541b4
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
34 changed files with 412 additions and 3 deletions

View file

@ -17,6 +17,9 @@ import { Member } from "eris";
import { ClearActiveMuteOnMemberBanEvt } from "./events/ClearActiveMuteOnMemberBanEvt";
import { ReapplyActiveMuteOnJoinEvt } from "./events/ReapplyActiveMuteOnJoinEvt";
import { mapToPublicFn } from "../../pluginUtils";
import { EventEmitter } from "events";
import { onMutesEvent } from "./functions/onMutesEvent";
import { offMutesEvent } from "./functions/offMutesEvent";
const defaultOptions = {
config: {
@ -92,6 +95,12 @@ export const MutesPlugin = zeppelinGuildPlugin<MutesPluginType>()("mutes", {
return muteRole ? member.roles.includes(muteRole) : false;
};
},
on: mapToPublicFn(onMutesEvent),
off: mapToPublicFn(offMutesEvent),
getEventEmitter(pluginData) {
return () => pluginData.state.events;
},
},
onLoad(pluginData) {
@ -100,6 +109,8 @@ export const MutesPlugin = zeppelinGuildPlugin<MutesPluginType>()("mutes", {
pluginData.state.serverLogs = new GuildLogs(pluginData.guild.id);
pluginData.state.archives = GuildArchives.getGuildInstance(pluginData.guild.id);
pluginData.state.events = new EventEmitter();
// Check for expired mutes every 5s
const firstCheckTime = Math.max(Date.now(), FIRST_CHECK_TIME) + FIRST_CHECK_INCREMENT;
FIRST_CHECK_TIME = firstCheckTime;
@ -115,5 +126,6 @@ export const MutesPlugin = zeppelinGuildPlugin<MutesPluginType>()("mutes", {
onUnload(pluginData) {
clearInterval(pluginData.state.muteClearIntervalId);
pluginData.state.events.removeAllListeners();
},
});

View file

@ -38,5 +38,7 @@ export async function clearExpiredMutes(pluginData: GuildPluginData<MutesPluginT
? stripObjectToScalars(member, ["user", "roles"])
: { id: mute.user_id, user: new UnknownUser({ id: mute.user_id }) },
});
pluginData.state.events.emit("unmute", mute.user_id);
}
}

View file

@ -246,6 +246,8 @@ export async function muteUser(
lock.unlock();
pluginData.state.events.emit("mute", user.id, reason);
return {
case: theCase,
notifyResult,

View file

@ -0,0 +1,10 @@
import { GuildPluginData } from "knub";
import { MutesEvents, MutesPluginType } from "../types";
export function offMutesEvent<TEvent extends keyof MutesEvents>(
pluginData: GuildPluginData<MutesPluginType>,
event: TEvent,
listener: MutesEvents[TEvent],
) {
return pluginData.state.events.off(event, listener);
}

View file

@ -0,0 +1,10 @@
import { GuildPluginData } from "knub";
import { MutesEvents, MutesPluginType } from "../types";
export function onMutesEvent<TEvent extends keyof MutesEvents>(
pluginData: GuildPluginData<MutesPluginType>,
event: TEvent,
listener: MutesEvents[TEvent],
) {
return pluginData.state.events.on(event, listener);
}

View file

@ -45,6 +45,7 @@ export async function unmuteUser(
member.edit(memberOptions);
}
} else {
// tslint:disable-next-line:no-console
console.warn(
`Member ${userId} not found in guild ${pluginData.guild.name} (${pluginData.guild.id}) when attempting to unmute`,
);
@ -95,6 +96,12 @@ export async function unmuteUser(
});
}
if (!unmuteTime) {
// If the member was unmuted, not just scheduled to be unmuted, fire the unmute event as well
// Scheduled unmutes have their event fired in clearExpiredMutes()
pluginData.state.events.emit("unmute", user.id, caseArgs.reason);
}
return {
case: createdCase,
};

View file

@ -10,6 +10,7 @@ import { GuildArchives } from "../../data/GuildArchives";
import { GuildMutes } from "../../data/GuildMutes";
import { CaseArgs } from "../Cases/types";
import Timeout = NodeJS.Timeout;
import { EventEmitter } from "events";
export const ConfigSchema = t.type({
mute_role: tNullable(t.string),
@ -31,6 +32,16 @@ export const ConfigSchema = t.type({
});
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
export interface MutesEvents {
mute: (userId: string, reason?: string) => void;
unmute: (userId: string, reason?: string) => void;
}
export interface MutesEventEmitter extends EventEmitter {
on<U extends keyof MutesEvents>(event: U, listener: MutesEvents[U]): this;
emit<U extends keyof MutesEvents>(event: U, ...args: Parameters<MutesEvents[U]>): boolean;
}
export interface MutesPluginType extends BasePluginType {
config: TConfigSchema;
state: {
@ -40,6 +51,8 @@ export interface MutesPluginType extends BasePluginType {
archives: GuildArchives;
muteClearIntervalId: Timeout;
events: MutesEventEmitter;
};
}