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-08-04 15:44:41 +03:00
|
|
|
import {
|
|
|
|
deepKeyIntersect,
|
|
|
|
isSnowflake,
|
|
|
|
isUnicodeEmoji,
|
2019-10-13 00:21:35 +03:00
|
|
|
MINUTES,
|
2019-08-04 15:44:41 +03:00
|
|
|
resolveMember,
|
|
|
|
resolveUser,
|
|
|
|
resolveUserId,
|
2019-08-22 02:58:32 +03:00
|
|
|
trimEmptyStartEndLines,
|
|
|
|
trimIndents,
|
2019-08-04 15:44:41 +03:00
|
|
|
UnknownUser,
|
|
|
|
} from "../utils";
|
2019-10-13 00:21:35 +03:00
|
|
|
import { Invite, Member, User } from "eris";
|
2019-08-04 16:51:42 +03:00
|
|
|
import DiscordRESTError from "eris/lib/errors/DiscordRESTError"; // tslint:disable-line
|
2019-05-02 18:14:36 +03:00
|
|
|
import { performance } from "perf_hooks";
|
2019-08-04 15:46:36 +03:00
|
|
|
import { decodeAndValidateStrict, StrictValidationError } from "../validatorUtils";
|
2019-08-04 15:44:41 +03:00
|
|
|
import { mergeConfig } from "knub/dist/configUtils";
|
2019-10-13 00:21:35 +03:00
|
|
|
import { SimpleCache } from "../SimpleCache";
|
2019-05-02 18:14:36 +03:00
|
|
|
|
|
|
|
const SLOW_RESOLVE_THRESHOLD = 1500;
|
|
|
|
|
2019-09-29 15:55:17 +03:00
|
|
|
/**
|
|
|
|
* Wrapper for the string type that indicates the text will be parsed as Markdown later
|
|
|
|
*/
|
|
|
|
type TMarkdown = string;
|
|
|
|
|
2019-08-21 22:07:13 +03:00
|
|
|
export interface PluginInfo {
|
2019-08-22 01:22:26 +03:00
|
|
|
prettyName: string;
|
2019-09-29 15:55:17 +03:00
|
|
|
description?: TMarkdown;
|
|
|
|
usageGuide?: TMarkdown;
|
|
|
|
configurationGuide?: TMarkdown;
|
2019-08-21 22:07:13 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 01:22:26 +03:00
|
|
|
export interface CommandInfo {
|
2019-09-29 15:55:17 +03:00
|
|
|
description?: TMarkdown;
|
|
|
|
basicUsage?: TMarkdown;
|
2019-10-25 23:14:21 +03:00
|
|
|
examples?: TMarkdown;
|
|
|
|
usageGuide?: TMarkdown;
|
2019-08-22 01:22:26 +03:00
|
|
|
parameterDescriptions?: {
|
2019-09-29 15:55:17 +03:00
|
|
|
[key: string]: TMarkdown;
|
2019-08-22 01:22:26 +03:00
|
|
|
};
|
2019-10-05 14:46:00 +03:00
|
|
|
optionDescriptions?: {
|
|
|
|
[key: string]: TMarkdown;
|
|
|
|
};
|
2019-08-22 01:22:26 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 02:58:32 +03:00
|
|
|
export function trimPluginDescription(str) {
|
2019-09-29 15:55:17 +03:00
|
|
|
const emptyLinesTrimmed = trimEmptyStartEndLines(str);
|
|
|
|
const lines = emptyLinesTrimmed.split("\n");
|
|
|
|
const lastLineIndentation = (lines[lines.length - 1].match(/^ +/g) || [""])[0].length;
|
|
|
|
return trimIndents(emptyLinesTrimmed, lastLineIndentation);
|
2019-08-22 02:58:32 +03:00
|
|
|
}
|
|
|
|
|
2019-10-13 00:21:35 +03:00
|
|
|
const inviteCache = new SimpleCache<Promise<Invite>>(10 * MINUTES, 200);
|
|
|
|
|
2019-04-13 01:44:18 +03:00
|
|
|
export class ZeppelinPlugin<TConfig extends {} = IBasePluginConfig> extends Plugin<TConfig> {
|
2019-08-21 22:07:13 +03:00
|
|
|
public static pluginInfo: PluginInfo;
|
2019-08-22 01:22:26 +03:00
|
|
|
public static showInDocs: boolean = true;
|
2019-08-21 22:07:13 +03:00
|
|
|
|
2019-08-22 02:58:32 +03:00
|
|
|
public 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-08-04 15:44:41 +03:00
|
|
|
/**
|
|
|
|
* Since we want to do type checking without creating instances of every plugin,
|
|
|
|
* we need a static version of getDefaultOptions(). This static version is then,
|
|
|
|
* by turn, called from getDefaultOptions() so everything still works as expected.
|
|
|
|
*/
|
2019-08-22 01:22:26 +03:00
|
|
|
public static getStaticDefaultOptions() {
|
2019-07-22 00:09:45 +03:00
|
|
|
// Implemented by plugin
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-08-04 15:44:41 +03:00
|
|
|
/**
|
|
|
|
* Wrapper to fetch the real default options from getStaticDefaultOptions()
|
|
|
|
*/
|
2019-07-22 00:09:45 +03:00
|
|
|
protected getDefaultOptions(): IPluginOptions<TConfig> {
|
|
|
|
return (this.constructor as typeof ZeppelinPlugin).getStaticDefaultOptions() as IPluginOptions<TConfig>;
|
|
|
|
}
|
|
|
|
|
2019-08-18 16:40:15 +03:00
|
|
|
/**
|
|
|
|
* Allows the plugin to preprocess the config before it's validated.
|
|
|
|
* Useful for e.g. adding default properties to dynamic objects.
|
|
|
|
*/
|
|
|
|
protected static preprocessStaticConfig(config: any) {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2019-08-04 15:44:41 +03:00
|
|
|
/**
|
|
|
|
* Merges the given options and default options and decodes them according to the config schema of the plugin (if any).
|
|
|
|
* Throws on any decoding/validation errors.
|
|
|
|
*
|
|
|
|
* Intended as an augmented, static replacement for Plugin.getMergedConfig() which is why this is also called from
|
|
|
|
* getMergedConfig().
|
|
|
|
*
|
|
|
|
* Like getStaticDefaultOptions(), we also want to use this function for type checking without creating an instance of
|
|
|
|
* the plugin, which is why this has to be a static function.
|
|
|
|
*/
|
|
|
|
protected static mergeAndDecodeStaticOptions(options: any): IPluginOptions {
|
|
|
|
const defaultOptions: any = this.getStaticDefaultOptions();
|
2019-08-18 16:40:15 +03:00
|
|
|
let mergedConfig = mergeConfig({}, defaultOptions.config || {}, options.config || {});
|
2019-10-25 20:22:00 +03:00
|
|
|
const mergedOverrides = options.replaceDefaultOverrides
|
|
|
|
? options.overrides
|
2019-10-25 21:44:39 +03:00
|
|
|
: (defaultOptions.overrides || []).concat(options.overrides || []);
|
2019-08-04 15:44:41 +03:00
|
|
|
|
2019-08-18 16:40:15 +03:00
|
|
|
mergedConfig = this.preprocessStaticConfig(mergedConfig);
|
|
|
|
|
2019-08-04 15:44:41 +03:00
|
|
|
const decodedConfig = this.configSchema ? decodeAndValidateStrict(this.configSchema, mergedConfig) : mergedConfig;
|
2019-08-04 15:46:36 +03:00
|
|
|
if (decodedConfig instanceof StrictValidationError) {
|
|
|
|
throw decodedConfig;
|
2019-08-04 15:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const decodedOverrides = [];
|
|
|
|
for (const override of mergedOverrides) {
|
|
|
|
const overrideConfigMergedWithBaseConfig = mergeConfig({}, mergedConfig, override.config || {});
|
|
|
|
const decodedOverrideConfig = this.configSchema
|
|
|
|
? decodeAndValidateStrict(this.configSchema, overrideConfigMergedWithBaseConfig)
|
|
|
|
: overrideConfigMergedWithBaseConfig;
|
2019-08-04 15:46:36 +03:00
|
|
|
if (decodedOverrideConfig instanceof StrictValidationError) {
|
|
|
|
throw decodedOverrideConfig;
|
2019-08-04 15:44:41 +03:00
|
|
|
}
|
2019-08-04 16:47:42 +03:00
|
|
|
decodedOverrides.push({
|
|
|
|
...override,
|
|
|
|
config: deepKeyIntersect(decodedOverrideConfig, override.config || {}),
|
|
|
|
});
|
2019-08-04 15:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
config: decodedConfig,
|
|
|
|
overrides: decodedOverrides,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper that calls mergeAndValidateStaticOptions()
|
|
|
|
*/
|
|
|
|
protected getMergedOptions(): IPluginOptions<TConfig> {
|
|
|
|
if (!this.mergedPluginOptions) {
|
|
|
|
this.mergedPluginOptions = ((this.constructor as unknown) as typeof ZeppelinPlugin).mergeAndDecodeStaticOptions(
|
|
|
|
this.pluginOptions,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.mergedPluginOptions as IPluginOptions<TConfig>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run static type checks and other validations on the given options
|
|
|
|
*/
|
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) {
|
2019-08-04 15:46:36 +03:00
|
|
|
try {
|
|
|
|
this.mergeAndDecodeStaticOptions(options);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof StrictValidationError) {
|
|
|
|
return e.getErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
2019-01-19 15:39:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No errors, return null
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async runLoad(): Promise<any> {
|
2019-08-04 15:44:41 +03:00
|
|
|
const mergedOptions = this.getMergedOptions(); // This implicitly also validates the config
|
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
|
|
|
|
2019-08-04 15:44:41 +03:00
|
|
|
/**
|
|
|
|
* Intended for cross-plugin functionality
|
|
|
|
*/
|
|
|
|
public getRuntimeOptions() {
|
|
|
|
return this.getMergedOptions();
|
|
|
|
}
|
|
|
|
|
2019-10-11 23:16:15 +03:00
|
|
|
getUser(userResolvable: string): User | UnknownUser {
|
|
|
|
const id = resolveUserId(this.bot, userResolvable);
|
|
|
|
return id ? this.bot.users.get(id) || new UnknownUser({ id }) : new UnknownUser();
|
|
|
|
}
|
|
|
|
|
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-08-04 13:14:23 +03:00
|
|
|
async getMember(memberResolvable: string, forceFresh = false): Promise<Member> {
|
2019-05-02 18:14:36 +03:00
|
|
|
const start = performance.now();
|
2019-08-04 13:14:23 +03:00
|
|
|
|
|
|
|
let member;
|
|
|
|
if (forceFresh) {
|
|
|
|
const userId = await resolveUserId(this.bot, memberResolvable);
|
2019-08-04 16:51:42 +03:00
|
|
|
try {
|
|
|
|
member = userId && (await this.bot.getRESTGuildMember(this.guild.id, userId));
|
|
|
|
} catch (e) {
|
|
|
|
if (!(e instanceof DiscordRESTError)) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 13:14:23 +03:00
|
|
|
if (member) member.id = member.user.id;
|
|
|
|
} else {
|
|
|
|
member = await resolveMember(this.bot, this.guild, memberResolvable);
|
|
|
|
}
|
|
|
|
|
2019-05-02 18:14:36 +03:00
|
|
|
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})`);
|
|
|
|
}
|
2019-08-04 13:14:23 +03:00
|
|
|
|
2019-05-02 18:14:36 +03:00
|
|
|
return member;
|
2019-04-20 17:36:28 +03:00
|
|
|
}
|
2019-10-13 00:21:35 +03:00
|
|
|
|
|
|
|
async resolveInvite(code: string): Promise<Invite | null> {
|
|
|
|
if (inviteCache.has(code)) {
|
|
|
|
return inviteCache.get(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
const promise = this.bot.getInvite(code).catch(() => null);
|
|
|
|
inviteCache.set(code, promise);
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
2018-11-25 17:04:26 +02:00
|
|
|
}
|