import { BaseRepository } from "./BaseRepository"; import { Member, TextableChannel } from "eris"; import { CaseTypes } from "./CaseTypes"; type KnownActions = "mute" | "unmute"; // https://github.com/Microsoft/TypeScript/issues/4183#issuecomment-382867018 type UnknownAction = T extends KnownActions ? never : T; type ActionFn = (args: T) => any | Promise; type MuteActionArgs = { member: Member; muteTime?: number; reason?: string }; type UnmuteActionArgs = { member: Member; unmuteTime?: number; reason?: string }; type CreateCaseActionArgs = { userId: string; modId: string; type: CaseTypes; auditLogId?: string; reason?: string; automatic?: boolean; postInCaseLog?: boolean; ppId?: string; }; type CreateCaseNoteActionArgs = { caseId: number; modId: string; note: string; automatic?: boolean; postInCaseLog?: boolean; }; type PostCaseActionArgs = { caseId: number; channel: TextableChannel; }; export class GuildActions extends BaseRepository { private actions: Map>; constructor(guildId) { super(guildId); this.actions = new Map(); } public register(actionName: "mute", actionFn: ActionFn): void; public register(actionName: "unmute", actionFn: ActionFn): void; public register(actionName: "createCase", actionFn: ActionFn): void; public register(actionName: "createCaseNote", actionFn: ActionFn): void; public register(actionName: "postCase", actionFn: ActionFn): void; // https://github.com/Microsoft/TypeScript/issues/4183#issuecomment-382867018 public register, U extends string = T>( actionName: T, actionFn: ActionFn, ): void; public register(actionName, actionFn): void { if (this.actions.has(actionName)) { throw new Error("Action is already registered!"); } this.actions.set(actionName, actionFn); } public unregister(actionName: string): void { this.actions.delete(actionName); } public fire(actionName: "mute", args: MuteActionArgs): Promise; public fire(actionName: "unmute", args: UnmuteActionArgs): Promise; public fire(actionName: "createCase", args: CreateCaseActionArgs): Promise; public fire(actionName: "createCaseNote", args: CreateCaseNoteActionArgs): Promise; public fire(actionName: "postCase", args: PostCaseActionArgs): Promise; // https://github.com/Microsoft/TypeScript/issues/4183#issuecomment-382867018 public fire, U extends string = T>(actionName: T, args: any): Promise; public fire(actionName, args): Promise { return this.actions.has(actionName) ? this.actions.get(actionName)(args) : null; } }