2020-06-30 17:48:18 +03:00
|
|
|
import { BasePluginType, configUtils, logger, PluginClass, PluginOptions, BasePluginConfig } from "knub";
|
2019-07-11 12:23:57 +03:00
|
|
|
import * as t from "io-ts";
|
2019-08-04 15:44:41 +03:00
|
|
|
import {
|
|
|
|
deepKeyIntersect,
|
2020-05-08 18:29:17 +03:00
|
|
|
isDiscordRESTError,
|
2019-08-04 15:44:41 +03:00
|
|
|
isSnowflake,
|
|
|
|
isUnicodeEmoji,
|
2019-10-13 00:21:35 +03:00
|
|
|
MINUTES,
|
2020-01-29 02:44:11 +02:00
|
|
|
Not,
|
2019-08-04 15:44:41 +03:00
|
|
|
resolveMember,
|
2020-01-29 02:44:11 +02:00
|
|
|
resolveRoleId,
|
2019-08-04 15:44:41 +03:00
|
|
|
resolveUser,
|
|
|
|
resolveUserId,
|
2019-11-28 02:34:41 +02:00
|
|
|
tDeepPartial,
|
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-05-02 18:14:36 +03:00
|
|
|
import { performance } from "perf_hooks";
|
2019-11-28 02:34:41 +02:00
|
|
|
import { decodeAndValidateStrict, StrictValidationError, validate } from "../validatorUtils";
|
2019-10-13 00:21:35 +03:00
|
|
|
import { SimpleCache } from "../SimpleCache";
|
2020-01-12 13:37:43 +02:00
|
|
|
import { TZeppelinKnub } from "../types";
|
2020-01-29 02:44:11 +02:00
|
|
|
import { ERRORS, RecoverablePluginError } from "../RecoverablePluginError";
|
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");
|
2019-12-01 15:57:17 +02:00
|
|
|
const firstLineIndentation = (lines[0].match(/^ +/g) || [""])[0].length;
|
|
|
|
return trimIndents(emptyLinesTrimmed, firstLineIndentation);
|
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);
|
|
|
|
|
2020-06-30 17:48:18 +03:00
|
|
|
export class ZeppelinPluginClass<TPluginType extends BasePluginType = BasePluginType> extends PluginClass<TPluginType> {
|
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 = [];
|
|
|
|
|
2020-01-12 13:37:43 +02:00
|
|
|
protected readonly knub: TZeppelinKnub;
|
|
|
|
|
2020-01-29 02:44:11 +02:00
|
|
|
protected throwRecoverablePluginError(code: ERRORS) {
|
|
|
|
throw new RecoverablePluginError(code, this.guild);
|
2018-11-25 17:04:26 +02:00
|
|
|
}
|
2018-12-15 17:24:09 +02:00
|
|
|
|
2020-01-12 11:38:12 +02:00
|
|
|
protected canActOn(member1: Member, member2: Member, allowSameLevel = false) {
|
2020-06-30 17:48:18 +03:00
|
|
|
if (member2.id === this.client.user.id) {
|
2018-12-15 17:24:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ourLevel = this.getMemberLevel(member1);
|
|
|
|
const memberLevel = this.getMemberLevel(member2);
|
2020-01-12 11:38:12 +02:00
|
|
|
return allowSameLevel ? ourLevel >= memberLevel : ourLevel > memberLevel;
|
2018-12-15 17:24:09 +02:00
|
|
|
}
|
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()
|
|
|
|
*/
|
2020-06-30 17:48:18 +03:00
|
|
|
protected getDefaultOptions(): PluginOptions<TPluginType> {
|
|
|
|
return (this.constructor as typeof ZeppelinPluginClass).getStaticDefaultOptions() as PluginOptions<TPluginType>;
|
2019-07-22 00:09:45 +03:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-06-30 17:48:18 +03:00
|
|
|
protected static mergeAndDecodeStaticOptions(options: any): PluginOptions<any> {
|
2020-06-04 03:46:33 +03:00
|
|
|
if (options == null) {
|
|
|
|
options = {
|
|
|
|
enabled: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-04 15:44:41 +03:00
|
|
|
const defaultOptions: any = this.getStaticDefaultOptions();
|
2019-11-02 22:11:26 +02:00
|
|
|
let mergedConfig = configUtils.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-11-28 02:34:41 +02:00
|
|
|
// Before preprocessing the static config, do a loose check by checking the schema as deeply partial.
|
|
|
|
// This way the preprocessing function can trust that if a property exists, its value will be the correct (partial) type.
|
|
|
|
const initialLooseCheck = this.configSchema ? validate(tDeepPartial(this.configSchema), mergedConfig) : null;
|
|
|
|
if (initialLooseCheck) {
|
|
|
|
throw initialLooseCheck;
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-11-02 22:11:26 +02:00
|
|
|
const overrideConfigMergedWithBaseConfig = configUtils.mergeConfig({}, mergedConfig, override.config || {});
|
2019-08-04 15:44:41 +03:00
|
|
|
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()
|
|
|
|
*/
|
2020-06-30 17:48:18 +03:00
|
|
|
protected getMergedOptions(): PluginOptions<TPluginType> {
|
2019-08-04 15:44:41 +03:00
|
|
|
if (!this.mergedPluginOptions) {
|
2020-06-30 17:48:18 +03:00
|
|
|
this.mergedPluginOptions = ((this
|
|
|
|
.constructor as unknown) as typeof ZeppelinPluginClass).mergeAndDecodeStaticOptions(this.pluginOptions);
|
2019-08-04 15:44:41 +03:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:48:18 +03:00
|
|
|
return this.mergedPluginOptions as PluginOptions<TPluginType>;
|
2019-08-04 15:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)) {
|
2020-06-30 17:48:18 +03:00
|
|
|
for (const guild of this.client.guilds.values()) {
|
2019-02-09 14:34:42 +02:00
|
|
|
if (guild.emojis.some(e => (e as any).id === snowflake)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-29 02:44:11 +02:00
|
|
|
this.throwRecoverablePluginError(ERRORS.INVALID_EMOJI);
|
2019-02-09 14:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2020-06-30 17:48:18 +03:00
|
|
|
const id = resolveUserId(this.client, userResolvable);
|
|
|
|
return id ? this.client.users.get(id) || new UnknownUser({ id }) : new UnknownUser();
|
2019-10-11 23:16:15 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2020-01-29 02:44:11 +02:00
|
|
|
async resolveUser(userResolvable: string): Promise<User | UnknownUser>;
|
|
|
|
async resolveUser<T>(userResolvable: Not<T, string>): Promise<UnknownUser>;
|
|
|
|
async resolveUser(userResolvable) {
|
2019-05-02 18:14:36 +03:00
|
|
|
const start = performance.now();
|
2020-06-30 17:48:18 +03:00
|
|
|
const user = await resolveUser(this.client, userResolvable);
|
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 user resolve (${rounded}ms): ${userResolvable}`);
|
|
|
|
}
|
|
|
|
return user;
|
2019-04-20 17:36:28 +03:00
|
|
|
}
|
|
|
|
|
2020-01-10 01:04:58 +11:00
|
|
|
/**
|
|
|
|
* Resolves a role from the passed string. The passed string can be a role ID, a role mention or a role name.
|
|
|
|
* In the event of duplicate role names, this function will return the first one it comes across.
|
2020-01-12 11:38:12 +02:00
|
|
|
* @param roleResolvable
|
2020-01-10 01:04:58 +11:00
|
|
|
*/
|
2020-01-12 11:39:26 +02:00
|
|
|
async resolveRoleId(roleResolvable: string): Promise<string | null> {
|
2020-06-30 17:48:18 +03:00
|
|
|
const roleId = await resolveRoleId(this.client, this.guildId, roleResolvable);
|
2020-01-10 01:04:58 +11:00
|
|
|
return roleId;
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-06-30 17:48:18 +03:00
|
|
|
const userId = await resolveUserId(this.client, memberResolvable);
|
2019-08-04 16:51:42 +03:00
|
|
|
try {
|
2020-06-30 17:48:18 +03:00
|
|
|
member = userId && (await this.client.getRESTGuildMember(this.guild.id, userId));
|
2019-08-04 16:51:42 +03:00
|
|
|
} catch (e) {
|
2020-05-08 18:29:17 +03:00
|
|
|
if (!isDiscordRESTError(e)) {
|
2019-08-04 16:51:42 +03:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 13:14:23 +03:00
|
|
|
if (member) member.id = member.user.id;
|
|
|
|
} else {
|
2020-06-30 17:48:18 +03:00
|
|
|
member = await resolveMember(this.client, this.guild, memberResolvable);
|
2019-08-04 13:14:23 +03:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-30 17:48:18 +03:00
|
|
|
const promise = this.client.getInvite(code).catch(() => null);
|
2019-10-13 00:21:35 +03:00
|
|
|
inviteCache.set(code, promise);
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
2018-11-25 17:04:26 +02:00
|
|
|
}
|