2019-07-22 00:09:45 +03:00
|
|
|
import { IBasePluginConfig, IPluginOptions, logger, Plugin, configUtils } from "knub";
|
2018-11-25 17:04:26 +02:00
|
|
|
import { PluginRuntimeError } from "../PluginRuntimeError";
|
2019-07-11 12:23:57 +03:00
|
|
|
import * as t from "io-ts";
|
|
|
|
import { pipe } from "fp-ts/lib/pipeable";
|
|
|
|
import { fold } from "fp-ts/lib/Either";
|
|
|
|
import { PathReporter } from "io-ts/lib/PathReporter";
|
2019-04-20 19:03:30 +03:00
|
|
|
import { isSnowflake, isUnicodeEmoji, resolveMember, resolveUser, UnknownUser } from "../utils";
|
2019-04-20 17:36:28 +03:00
|
|
|
import { Member, User } from "eris";
|
2019-05-02 18:14:36 +03:00
|
|
|
import { performance } from "perf_hooks";
|
2019-07-11 12:23:57 +03:00
|
|
|
import { validateStrict } from "../validatorUtils";
|
2019-05-02 18:14:36 +03:00
|
|
|
|
|
|
|
const SLOW_RESOLVE_THRESHOLD = 1500;
|
|
|
|
|
2019-04-13 01:44:18 +03:00
|
|
|
export class ZeppelinPlugin<TConfig extends {} = IBasePluginConfig> extends Plugin<TConfig> {
|
2019-07-11 12:23:57 +03:00
|
|
|
protected static configSchema: t.TypeC<any>;
|
2019-04-20 17:36:28 +03:00
|
|
|
public static dependencies = [];
|
|
|
|
|
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) {
|
2019-06-17 19:08:15 +02:00
|
|
|
if (member1.id === member2.id || member2.id === this.bot.user.id) {
|
2018-12-15 17:24:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ourLevel = this.getMemberLevel(member1);
|
|
|
|
const memberLevel = this.getMemberLevel(member2);
|
|
|
|
return ourLevel > memberLevel;
|
|
|
|
}
|
2019-01-19 15:39:04 +02:00
|
|
|
|
2019-07-22 00:09:45 +03:00
|
|
|
protected static getStaticDefaultOptions() {
|
|
|
|
// Implemented by plugin
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getDefaultOptions(): IPluginOptions<TConfig> {
|
|
|
|
return (this.constructor as typeof ZeppelinPlugin).getStaticDefaultOptions() as IPluginOptions<TConfig>;
|
|
|
|
}
|
|
|
|
|
2019-07-11 12:23:57 +03:00
|
|
|
public static validateOptions(options: any): string[] | null {
|
2019-01-19 15:39:04 +02:00
|
|
|
// Validate config values
|
|
|
|
if (this.configSchema) {
|
|
|
|
if (options.config) {
|
2019-07-22 00:09:45 +03:00
|
|
|
const merged = configUtils.mergeConfig(
|
|
|
|
{},
|
|
|
|
(this.getStaticDefaultOptions() as any).config || {},
|
|
|
|
options.config,
|
|
|
|
);
|
|
|
|
const errors = validateStrict(this.configSchema, merged);
|
|
|
|
if (errors) {
|
|
|
|
return errors;
|
|
|
|
}
|
2019-01-19 15:39:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.overrides) {
|
2019-07-22 00:09:45 +03:00
|
|
|
for (const [i, override] of options.overrides.entries()) {
|
2019-01-19 15:39:04 +02:00
|
|
|
if (override.config) {
|
2019-07-22 00:09:45 +03:00
|
|
|
// For type checking overrides, apply default config + supplied config + any overrides preceding this override + finally this override
|
|
|
|
// Exhaustive type checking would require checking against all combinations of preceding overrides but that's... costy. This will do for now.
|
|
|
|
// TODO: Override default config retrieval functions and do some sort of memoized checking there?
|
|
|
|
const merged = configUtils.mergeConfig(
|
|
|
|
{},
|
|
|
|
(this.getStaticDefaultOptions() as any).config || {},
|
|
|
|
options.config || {},
|
2019-07-22 13:09:05 +03:00
|
|
|
...options.overrides.slice(0, i).map(o => o.config || {}),
|
2019-07-22 00:09:45 +03:00
|
|
|
override.config,
|
|
|
|
);
|
|
|
|
const errors = validateStrict(this.configSchema, merged);
|
|
|
|
if (errors) {
|
|
|
|
return errors;
|
|
|
|
}
|
2019-01-19 15:39:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No errors, return null
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async runLoad(): Promise<any> {
|
|
|
|
const mergedOptions = this.getMergedOptions();
|
2019-05-25 14:39:26 +03:00
|
|
|
const validationErrors = ((this.constructor as unknown) as typeof ZeppelinPlugin).validateOptions(mergedOptions);
|
2019-01-19 15:39:04 +02:00
|
|
|
if (validationErrors) {
|
2019-07-22 00:09:45 +03:00
|
|
|
throw new Error(validationErrors.join("\n"));
|
2019-01-19 15:39:04 +02:00
|
|
|
}
|
|
|
|
|
2019-02-06 20:05:48 +02:00
|
|
|
return super.runLoad();
|
2019-01-19 15:39:04 +02:00
|
|
|
}
|
2019-02-09 14:34:42 +02:00
|
|
|
|
|
|
|
public canUseEmoji(snowflake): boolean {
|
|
|
|
if (isUnicodeEmoji(snowflake)) {
|
|
|
|
return true;
|
|
|
|
} else if (isSnowflake(snowflake)) {
|
|
|
|
for (const guild of this.bot.guilds.values()) {
|
|
|
|
if (guild.emojis.some(e => (e as any).id === snowflake)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new PluginRuntimeError(`Invalid emoji: ${snowflake}`, this.runtimePluginName, this.guildId);
|
|
|
|
}
|
|
|
|
}
|
2019-04-13 03:54:36 +03:00
|
|
|
|
|
|
|
public getRegisteredCommands() {
|
|
|
|
return this.commands.commands;
|
|
|
|
}
|
2019-04-20 17:36:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves a user from the passed string. The passed string can be a user id, a user mention, a full username (with discrim), etc.
|
2019-05-25 14:39:26 +03:00
|
|
|
* If the user is not found in the cache, it's fetched from the API.
|
2019-04-20 17:36:28 +03:00
|
|
|
*/
|
|
|
|
async resolveUser(userResolvable: string): Promise<User | UnknownUser> {
|
2019-05-02 18:14:36 +03:00
|
|
|
const start = performance.now();
|
|
|
|
const user = await resolveUser(this.bot, userResolvable);
|
|
|
|
const time = performance.now() - start;
|
|
|
|
if (time >= SLOW_RESOLVE_THRESHOLD) {
|
|
|
|
const rounded = Math.round(time);
|
|
|
|
logger.warn(`Slow user resolve (${rounded}ms): ${userResolvable}`);
|
|
|
|
}
|
|
|
|
return user;
|
2019-04-20 17:36:28 +03:00
|
|
|
}
|
|
|
|
|
2019-05-25 14:39:26 +03:00
|
|
|
/**
|
|
|
|
* Resolves a member from the passed string. The passed string can be a user id, a user mention, a full username (with discrim), etc.
|
|
|
|
* If the member is not found in the cache, it's fetched from the API.
|
|
|
|
*/
|
2019-04-20 19:03:30 +03:00
|
|
|
async getMember(memberResolvable: string): Promise<Member> {
|
2019-05-02 18:14:36 +03:00
|
|
|
const start = performance.now();
|
|
|
|
const member = await resolveMember(this.bot, this.guild, memberResolvable);
|
|
|
|
const time = performance.now() - start;
|
|
|
|
if (time >= SLOW_RESOLVE_THRESHOLD) {
|
|
|
|
const rounded = Math.round(time);
|
|
|
|
logger.warn(`Slow member resolve (${rounded}ms): ${memberResolvable} in ${this.guild.name} (${this.guild.id})`);
|
|
|
|
}
|
|
|
|
return member;
|
2019-04-20 17:36:28 +03:00
|
|
|
}
|
2018-11-25 17:04:26 +02:00
|
|
|
}
|