2019-01-19 15:39:04 +02:00
|
|
|
import { IPluginOptions, Plugin } from "knub";
|
2018-11-25 17:04:26 +02:00
|
|
|
import { PluginRuntimeError } from "../PluginRuntimeError";
|
2019-01-19 15:39:04 +02:00
|
|
|
import Ajv, { ErrorObject } from "ajv";
|
2018-11-25 17:04:26 +02:00
|
|
|
|
|
|
|
export class ZeppelinPlugin extends Plugin {
|
2019-01-19 15:39:04 +02:00
|
|
|
protected configSchema: any;
|
|
|
|
protected permissionsSchema: any;
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
protected throwPluginRuntimeError(message: string) {
|
2019-01-03 06:17:39 +02:00
|
|
|
throw new PluginRuntimeError(message, this.runtimePluginName, this.guildId);
|
2018-11-25 17:04:26 +02:00
|
|
|
}
|
2018-12-15 17:24:09 +02:00
|
|
|
|
|
|
|
protected canActOn(member1, member2) {
|
|
|
|
if (member1.id === member2.id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ourLevel = this.getMemberLevel(member1);
|
|
|
|
const memberLevel = this.getMemberLevel(member2);
|
|
|
|
return ourLevel > memberLevel;
|
|
|
|
}
|
2019-01-19 15:39:04 +02:00
|
|
|
|
|
|
|
public validateOptions(options: IPluginOptions): ErrorObject[] | null {
|
|
|
|
// Validate config values
|
|
|
|
if (this.configSchema) {
|
|
|
|
const ajv = new Ajv();
|
|
|
|
const validate = ajv.compile(this.configSchema);
|
|
|
|
|
|
|
|
if (options.config) {
|
|
|
|
const isValid = validate(options.config);
|
|
|
|
if (!isValid) return validate.errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.overrides) {
|
|
|
|
for (const override of options.overrides) {
|
|
|
|
if (override.config) {
|
|
|
|
const isValid = validate(override.config);
|
|
|
|
if (!isValid) return validate.errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate permission values
|
|
|
|
if (this.permissionsSchema) {
|
|
|
|
const ajv = new Ajv();
|
|
|
|
const validate = ajv.compile(this.permissionsSchema);
|
|
|
|
|
|
|
|
if (options.permissions) {
|
|
|
|
const isValid = validate(options.permissions);
|
|
|
|
if (!isValid) return validate.errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.overrides) {
|
|
|
|
for (const override of options.overrides) {
|
|
|
|
if (override.permissions) {
|
|
|
|
const isValid = validate(override.permissions);
|
|
|
|
if (!isValid) return validate.errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No errors, return null
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async runLoad(): Promise<any> {
|
|
|
|
const mergedOptions = this.getMergedOptions();
|
|
|
|
const validationErrors = this.validateOptions(mergedOptions);
|
|
|
|
if (validationErrors) {
|
|
|
|
throw new Error(`Invalid options:\n${validationErrors.join("\n")}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.onLoad();
|
|
|
|
}
|
2018-11-25 17:04:26 +02:00
|
|
|
}
|