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

Use actions/events for plugin interoperability. Move base case and mute functionality to their own plugins.

This commit is contained in:
Dragory 2018-11-25 17:04:26 +02:00
parent 22c515be38
commit 2e30a3b9e7
14 changed files with 674 additions and 332 deletions

24
src/data/GuildActions.ts Normal file
View file

@ -0,0 +1,24 @@
import { BaseRepository } from "./BaseRepository";
type ActionFn = (...args: any[]) => any | Promise<any>;
export class GuildActions extends BaseRepository {
private actions: Map<string, ActionFn>;
constructor(guildId) {
super(guildId);
this.actions = new Map();
}
public register(actionName: string, actionFn: ActionFn) {
this.actions.set(actionName, actionFn);
}
public unregister(actionName: string) {
this.actions.delete(actionName);
}
public fire(actionName: string, ...args: any[]): Promise<any> {
return this.actions.has(actionName) ? this.actions.get(actionName)(...args) : null;
}
}

View file

@ -53,26 +53,24 @@ export class GuildCases extends BaseRepository {
});
}
async create(data): Promise<number> {
async create(data): Promise<Case> {
const result = await this.cases.insert({
...data,
guild_id: this.guildId,
case_number: () => `(SELECT IFNULL(MAX(case_number)+1, 1) FROM cases AS ma2 WHERE guild_id = ${this.guildId})`
});
return result.identifiers[0].id;
return this.find(result.identifiers[0].id);
}
update(id, data) {
return this.cases.update(id, data);
}
async createNote(caseId: number, data: any): Promise<number> {
const result = await this.caseNotes.insert({
async createNote(caseId: number, data: any): Promise<void> {
await this.caseNotes.insert({
...data,
case_id: caseId
});
return result.identifiers[0].id;
}
}

42
src/data/GuildEvents.ts Normal file
View file

@ -0,0 +1,42 @@
import { BaseRepository } from "./BaseRepository";
import { QueuedEventEmitter } from "../QueuedEventEmitter";
export class GuildEvents extends BaseRepository {
private queuedEventEmitter: QueuedEventEmitter;
private pluginListeners: Map<string, Map<string, any[]>>;
constructor(guildId) {
super(guildId);
this.queuedEventEmitter = new QueuedEventEmitter();
}
public on(pluginName: string, eventName: string, fn) {
this.queuedEventEmitter.on(eventName, fn);
if (!this.pluginListeners.has(pluginName)) {
this.pluginListeners.set(pluginName, new Map());
}
const pluginListeners = this.pluginListeners.get(pluginName);
if (!pluginListeners.has(eventName)) {
pluginListeners.set(eventName, []);
}
const pluginEventListeners = pluginListeners.get(eventName);
pluginEventListeners.push(fn);
}
public offPlugin(pluginName: string) {
const pluginListeners = this.pluginListeners.get(pluginName) || new Map();
for (const [eventName, listeners] of Array.from(pluginListeners.entries())) {
for (const listener of listeners) {
this.queuedEventEmitter.off(eventName, listener);
}
}
this.pluginListeners.delete(pluginName);
}
public emit(eventName: string, args: any[] = []) {
return this.queuedEventEmitter.emit(eventName, args);
}
}

View file

@ -29,18 +29,20 @@ export class GuildMutes extends BaseRepository {
});
}
async addMute(userId, expiryTime) {
async addMute(userId, expiryTime): Promise<Mute> {
const expiresAt = expiryTime
? moment()
.add(expiryTime, "ms")
.format("YYYY-MM-DD HH:mm:ss")
: null;
return this.mutes.insert({
const result = await this.mutes.insert({
guild_id: this.guildId,
user_id: userId,
expires_at: expiresAt
});
return this.mutes.findOne(result.identifiers[0].id);
}
async updateExpiryTime(userId, newExpiryTime) {
@ -61,11 +63,12 @@ export class GuildMutes extends BaseRepository {
);
}
async addOrUpdateMute(userId, expiryTime) {
async addOrUpdateMute(userId, expiryTime): Promise<Mute> {
const existingMute = await this.findExistingMuteForUserId(userId);
if (existingMute) {
return this.updateExpiryTime(userId, expiryTime);
await this.updateExpiryTime(userId, expiryTime);
return this.findExistingMuteForUserId(userId);
} else {
return this.addMute(userId, expiryTime);
}
@ -83,7 +86,7 @@ export class GuildMutes extends BaseRepository {
.getMany();
}
async setCaseId(userId, caseId) {
async setCaseId(userId: string, caseId: number) {
await this.mutes.update(
{
guild_id: this.guildId,