mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Knub 30 conversion base work; Work on Utility plugin Knub 30 conversion
This commit is contained in:
parent
1bf5a7fa28
commit
d62a4e26ae
18 changed files with 585 additions and 324 deletions
|
@ -1,4 +1,4 @@
|
|||
import { PluginInfo, trimPluginDescription } from "../ZeppelinPluginClass";
|
||||
import { ZeppelinPluginInfo, trimPluginDescription } from "../ZeppelinPluginClass";
|
||||
|
||||
export const pluginInfo: PluginInfo = {
|
||||
prettyName: "Automod",
|
||||
|
|
|
@ -4,7 +4,7 @@ import { CaseTypes } from "../data/CaseTypes";
|
|||
import { Case } from "../data/entities/Case";
|
||||
import moment from "moment-timezone";
|
||||
import { CaseTypeColors } from "../data/CaseTypeColors";
|
||||
import { PluginInfo, trimPluginDescription, ZeppelinPluginClass } from "./ZeppelinPluginClass";
|
||||
import { ZeppelinPluginInfo, trimPluginDescription, ZeppelinPluginClass } from "./ZeppelinPluginClass";
|
||||
import { GuildArchives } from "../data/GuildArchives";
|
||||
import { IPluginOptions, logger } from "knub";
|
||||
import { GuildLogs } from "../data/GuildLogs";
|
||||
|
|
30
backend/src/plugins/Utility/UtilityPlugin.ts
Normal file
30
backend/src/plugins/Utility/UtilityPlugin.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import { zeppelinPlugin } from "../ZeppelinPluginBlueprint";
|
||||
import { ConfigSchema, UtilityPluginType } from "./types";
|
||||
import { GuildLogs } from "../../data/GuildLogs";
|
||||
import { GuildCases } from "../../data/GuildCases";
|
||||
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
|
||||
import { GuildArchives } from "../../data/GuildArchives";
|
||||
import { Supporters } from "../../data/Supporters";
|
||||
import { ServerCmd } from "./commands/ServerCmd";
|
||||
import { RolesCmd } from "./commands/RolesCmd";
|
||||
import { LevelCmd } from "./commands/LevelCmd";
|
||||
|
||||
export const UtilityPlugin = zeppelinPlugin<UtilityPluginType>()("utility", {
|
||||
configSchema: ConfigSchema,
|
||||
|
||||
commands: [
|
||||
LevelCmd,
|
||||
RolesCmd,
|
||||
ServerCmd,
|
||||
],
|
||||
|
||||
onLoad({ state, guild }) {
|
||||
state.logs = new GuildLogs(guild.id);
|
||||
state.cases = GuildCases.getGuildInstance(guild.id);
|
||||
state.savedMessages = GuildSavedMessages.getGuildInstance(guild.id);
|
||||
state.archives = GuildArchives.getGuildInstance(guild.id);
|
||||
state.supporters = new Supporters();
|
||||
|
||||
state.lastReload = Date.now();
|
||||
}
|
||||
});
|
23
backend/src/plugins/Utility/commands/LevelCmd.ts
Normal file
23
backend/src/plugins/Utility/commands/LevelCmd.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { utilityCmd } from "../types";
|
||||
import { customArgumentHelpers as ct } from "../../../customArgumentTypes";
|
||||
import { helpers } from "knub";
|
||||
|
||||
const { getMemberLevel } = helpers;
|
||||
|
||||
export const LevelCmd = utilityCmd(
|
||||
"level",
|
||||
{
|
||||
member: ct.resolvedMember({ required: false }),
|
||||
},
|
||||
|
||||
{
|
||||
description: "Show the permission level of a user",
|
||||
usage: "!level 106391128718245888",
|
||||
},
|
||||
|
||||
({ message, args, pluginData }) => {
|
||||
const member = args.member || message.member;
|
||||
const level = getMemberLevel(pluginData, member);
|
||||
message.channel.createMessage(`The permission level of ${member.username}#${member.discriminator} is **${level}**`);
|
||||
}
|
||||
);
|
113
backend/src/plugins/Utility/commands/RolesCmd.ts
Normal file
113
backend/src/plugins/Utility/commands/RolesCmd.ts
Normal file
|
@ -0,0 +1,113 @@
|
|||
import { utilityCmd } from "../types";
|
||||
import { baseTypeHelpers as t } from "knub";
|
||||
import { Role, TextChannel } from "eris";
|
||||
import { chunkArray, sorter, trimLines } from "../../../utils";
|
||||
import { refreshMembersIfNeeded } from "../refreshMembers";
|
||||
import { sendErrorMessage } from "../../../pluginUtils";
|
||||
|
||||
export const RolesCmd = utilityCmd(
|
||||
"roles",
|
||||
{
|
||||
search: t.string({ catchAll: true }),
|
||||
|
||||
counts: t.switchOption(),
|
||||
sort: t.string({ option: true }),
|
||||
},
|
||||
|
||||
{
|
||||
description: "List all roles or roles matching a search",
|
||||
usage: "!roles mod",
|
||||
permission: "can_roles",
|
||||
},
|
||||
|
||||
async ({ message: msg, args, pluginData }) => {
|
||||
const { guild } = pluginData;
|
||||
|
||||
let roles: Array<{ _memberCount?: number } & Role> = Array.from((msg.channel as TextChannel).guild.roles.values());
|
||||
let sort = args.sort;
|
||||
|
||||
if (args.search) {
|
||||
const searchStr = args.search.toLowerCase();
|
||||
roles = roles.filter(r => r.name.toLowerCase().includes(searchStr) || r.id === searchStr);
|
||||
}
|
||||
|
||||
if (args.counts) {
|
||||
await refreshMembersIfNeeded(guild);
|
||||
|
||||
// If the user requested role member counts as well, calculate them and sort the roles by their member count
|
||||
const roleCounts: Map<string, number> = Array.from(guild.members.values()).reduce((map, member) => {
|
||||
for (const roleId of member.roles) {
|
||||
if (!map.has(roleId)) map.set(roleId, 0);
|
||||
map.set(roleId, map.get(roleId) + 1);
|
||||
}
|
||||
|
||||
return map;
|
||||
}, new Map());
|
||||
|
||||
// The "everyone" role always has all members in it
|
||||
roleCounts.set(guild.id, guild.memberCount);
|
||||
|
||||
for (const role of roles) {
|
||||
role._memberCount = roleCounts.has(role.id) ? roleCounts.get(role.id) : 0;
|
||||
}
|
||||
|
||||
if (!sort) sort = "-memberCount";
|
||||
roles.sort((a, b) => {
|
||||
if (a._memberCount > b._memberCount) return -1;
|
||||
if (a._memberCount < b._memberCount) return 1;
|
||||
return 0;
|
||||
});
|
||||
} else {
|
||||
// Otherwise sort by name
|
||||
roles.sort((a, b) => {
|
||||
if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
|
||||
if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
if (!sort) sort = "name";
|
||||
|
||||
let sortDir: "ASC" | "DESC" = "ASC";
|
||||
if (sort && sort[0] === "-") {
|
||||
sort = sort.slice(1);
|
||||
sortDir = "DESC";
|
||||
}
|
||||
|
||||
if (sort === "position" || sort === "order") {
|
||||
roles.sort(sorter("position", sortDir));
|
||||
} else if (sort === "memberCount" && args.counts) {
|
||||
roles.sort(sorter("_memberCount", sortDir));
|
||||
} else if (sort === "name") {
|
||||
roles.sort(sorter(r => r.name.toLowerCase(), sortDir));
|
||||
} else {
|
||||
sendErrorMessage(pluginData, msg.channel, "Unknown sorting method");
|
||||
return;
|
||||
}
|
||||
|
||||
const longestId = roles.reduce((longest, role) => Math.max(longest, role.id.length), 0);
|
||||
|
||||
const chunks = chunkArray(roles, 20);
|
||||
for (const [i, chunk] of chunks.entries()) {
|
||||
const roleLines = chunk.map(role => {
|
||||
const paddedId = role.id.padEnd(longestId, " ");
|
||||
let line = `${paddedId} ${role.name}`;
|
||||
if (role._memberCount != null) {
|
||||
line += role._memberCount === 1 ? ` (${role._memberCount} member)` : ` (${role._memberCount} members)`;
|
||||
}
|
||||
return line;
|
||||
});
|
||||
|
||||
if (i === 0) {
|
||||
msg.channel.createMessage(
|
||||
trimLines(`
|
||||
${args.search ? "Total roles found" : "Total roles"}: ${roles.length}
|
||||
\`\`\`py\n${roleLines.join("\n")}\`\`\`
|
||||
`),
|
||||
);
|
||||
} else {
|
||||
msg.channel.createMessage("```py\n" + roleLines.join("\n") + "```");
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
12
backend/src/plugins/Utility/commands/SearchCmd.ts
Normal file
12
backend/src/plugins/Utility/commands/SearchCmd.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { utilityCmd } from "../types";
|
||||
import { baseTypeHelpers as t } from "knub";
|
||||
|
||||
export const SearchCmd = utilityCmd(
|
||||
["search", "s"],
|
||||
{
|
||||
query: t.string({ catchAll: true }),
|
||||
|
||||
},
|
||||
{},
|
||||
() => {}
|
||||
);
|
126
backend/src/plugins/Utility/commands/ServerCmd.ts
Normal file
126
backend/src/plugins/Utility/commands/ServerCmd.ts
Normal file
|
@ -0,0 +1,126 @@
|
|||
import { CategoryChannel, EmbedOptions, TextChannel, VoiceChannel } from "eris";
|
||||
import moment from "moment-timezone";
|
||||
import { embedPadding, formatNumber, memoize, MINUTES, trimLines } from "../../../utils";
|
||||
import { utilityCmd } from "../types";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
|
||||
export const ServerCmd = utilityCmd(
|
||||
"server",
|
||||
{},
|
||||
|
||||
{
|
||||
permission: "can_server",
|
||||
description: "Show information about the server",
|
||||
usage: "!server",
|
||||
},
|
||||
|
||||
async ({ message }) => {
|
||||
const embed: EmbedOptions = {
|
||||
fields: [],
|
||||
color: parseInt("6b80cf", 16),
|
||||
};
|
||||
|
||||
embed.thumbnail = { url: this.guild.iconURL };
|
||||
|
||||
const createdAt = moment(this.guild.createdAt);
|
||||
const serverAge = humanizeDuration(moment().valueOf() - this.guild.createdAt, {
|
||||
largest: 2,
|
||||
round: true,
|
||||
});
|
||||
|
||||
const owner = this.bot.users.get(this.guild.ownerID);
|
||||
const ownerName = owner ? `${owner.username}#${owner.discriminator}` : "Unknown#0000";
|
||||
|
||||
embed.fields.push({
|
||||
name: `Server information - ${this.guild.name}`,
|
||||
value:
|
||||
trimLines(`
|
||||
Created: **${serverAge} ago** (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
||||
Owner: **${ownerName}** (${this.guild.ownerID})
|
||||
Voice region: **${this.guild.region}**
|
||||
${this.guild.features.length > 0 ? "Features: " + this.guild.features.join(", ") : ""}
|
||||
`) + embedPadding,
|
||||
});
|
||||
|
||||
const restGuild = await memoize(
|
||||
() => this.bot.getRESTGuild(this.guildId),
|
||||
`getRESTGuild_${this.guildId}`,
|
||||
10 * MINUTES,
|
||||
);
|
||||
|
||||
// For servers with a vanity URL, we can use the numbers from the invite for online count
|
||||
// (which is nowadays usually more accurate for large servers)
|
||||
const invite = this.guild.vanityURL
|
||||
? await memoize(
|
||||
() => this.bot.getInvite(this.guild.vanityURL, true),
|
||||
`getInvite_${this.guild.vanityURL}`,
|
||||
10 * MINUTES,
|
||||
)
|
||||
: null;
|
||||
|
||||
const totalMembers = invite ? invite.memberCount : this.guild.memberCount;
|
||||
|
||||
const onlineMemberCount = invite
|
||||
? invite.presenceCount
|
||||
: this.guild.members.filter(m => m.status !== "offline").length;
|
||||
const offlineMemberCount = this.guild.memberCount - onlineMemberCount;
|
||||
|
||||
let memberCountTotalLines = `Total: **${formatNumber(totalMembers)}**`;
|
||||
if (restGuild.maxMembers) {
|
||||
memberCountTotalLines += `\nMax: **${formatNumber(restGuild.maxMembers)}**`;
|
||||
}
|
||||
|
||||
let memberCountOnlineLines = `Online: **${formatNumber(onlineMemberCount)}**`;
|
||||
if (restGuild.maxPresences) {
|
||||
memberCountOnlineLines += `\nMax online: **${formatNumber(restGuild.maxPresences)}**`;
|
||||
}
|
||||
|
||||
embed.fields.push({
|
||||
name: "Members",
|
||||
inline: true,
|
||||
value: trimLines(`
|
||||
${memberCountTotalLines}
|
||||
${memberCountOnlineLines}
|
||||
Offline: **${formatNumber(offlineMemberCount)}**
|
||||
`),
|
||||
});
|
||||
|
||||
const totalChannels = this.guild.channels.size;
|
||||
const categories = this.guild.channels.filter(channel => channel instanceof CategoryChannel);
|
||||
const textChannels = this.guild.channels.filter(channel => channel instanceof TextChannel);
|
||||
const voiceChannels = this.guild.channels.filter(channel => channel instanceof VoiceChannel);
|
||||
|
||||
embed.fields.push({
|
||||
name: "Channels",
|
||||
inline: true,
|
||||
value:
|
||||
trimLines(`
|
||||
Total: **${totalChannels}** / 500
|
||||
Categories: **${categories.length}**
|
||||
Text: **${textChannels.length}**
|
||||
Voice: **${voiceChannels.length}**
|
||||
`) + embedPadding,
|
||||
});
|
||||
|
||||
const maxEmojis =
|
||||
{
|
||||
0: 50,
|
||||
1: 100,
|
||||
2: 150,
|
||||
3: 250,
|
||||
}[this.guild.premiumTier] || 50;
|
||||
|
||||
embed.fields.push({
|
||||
name: "Other stats",
|
||||
inline: true,
|
||||
value:
|
||||
trimLines(`
|
||||
Roles: **${this.guild.roles.size}** / 250
|
||||
Emojis: **${this.guild.emojis.length}** / ${maxEmojis}
|
||||
Boosts: **${this.guild.premiumSubscriptionCount ?? 0}** (level ${this.guild.premiumTier})
|
||||
`) + embedPadding,
|
||||
});
|
||||
|
||||
message.channel.createMessage({ embed });
|
||||
}
|
||||
);
|
20
backend/src/plugins/Utility/refreshMembers.ts
Normal file
20
backend/src/plugins/Utility/refreshMembers.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { Guild } from "eris";
|
||||
import { MINUTES, noop } from "../../utils";
|
||||
|
||||
const MEMBER_REFRESH_FREQUENCY = 10 * MINUTES; // How often to do a full member refresh when using commands that need it
|
||||
const memberRefreshLog = new Map<string, { time: number, promise: Promise<void> }>();
|
||||
|
||||
export async function refreshMembersIfNeeded(guild: Guild) {
|
||||
const lastRefresh = memberRefreshLog.get(guild.id);
|
||||
if (lastRefresh && Date.now() < lastRefresh.time + MEMBER_REFRESH_FREQUENCY) {
|
||||
return lastRefresh.promise;
|
||||
}
|
||||
|
||||
const loadPromise = guild.fetchAllMembers().then(noop);
|
||||
memberRefreshLog.set(guild.id, {
|
||||
time: Date.now(),
|
||||
promise: loadPromise,
|
||||
});
|
||||
|
||||
return loadPromise;
|
||||
}
|
44
backend/src/plugins/Utility/types.ts
Normal file
44
backend/src/plugins/Utility/types.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import * as t from "io-ts";
|
||||
import { BasePluginType, command, eventListener } from "knub";
|
||||
import { GuildLogs } from "../../data/GuildLogs";
|
||||
import { GuildCases } from "../../data/GuildCases";
|
||||
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
|
||||
import { GuildArchives } from "../../data/GuildArchives";
|
||||
import { Supporters } from "../../data/Supporters";
|
||||
|
||||
export const ConfigSchema = t.type({
|
||||
can_roles: t.boolean,
|
||||
can_level: t.boolean,
|
||||
can_search: t.boolean,
|
||||
can_clean: t.boolean,
|
||||
can_info: t.boolean,
|
||||
can_server: t.boolean,
|
||||
can_reload_guild: t.boolean,
|
||||
can_nickname: t.boolean,
|
||||
can_ping: t.boolean,
|
||||
can_source: t.boolean,
|
||||
can_vcmove: t.boolean,
|
||||
can_help: t.boolean,
|
||||
can_about: t.boolean,
|
||||
can_context: t.boolean,
|
||||
can_jumbo: t.boolean,
|
||||
jumbo_size: t.Integer,
|
||||
can_avatar: t.boolean,
|
||||
});
|
||||
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||
|
||||
export interface UtilityPluginType extends BasePluginType {
|
||||
config: TConfigSchema;
|
||||
state: {
|
||||
logs: GuildLogs;
|
||||
cases: GuildCases;
|
||||
savedMessages: GuildSavedMessages;
|
||||
archives: GuildArchives;
|
||||
supporters: Supporters;
|
||||
|
||||
lastReload: number;
|
||||
};
|
||||
}
|
||||
|
||||
export const utilityCmd = command<UtilityPluginType>();
|
||||
export const utilityEvent = eventListener<UtilityPluginType>();
|
7
backend/src/plugins/ZeppelinPlugin.ts
Normal file
7
backend/src/plugins/ZeppelinPlugin.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { ZeppelinPluginClass } from "./ZeppelinPluginClass";
|
||||
import { ZeppelinPluginBlueprint } from "./ZeppelinPluginBlueprint";
|
||||
|
||||
// prettier-ignore
|
||||
export type ZeppelinPlugin =
|
||||
| typeof ZeppelinPluginClass
|
||||
| ZeppelinPluginBlueprint;
|
20
backend/src/plugins/ZeppelinPluginBlueprint.ts
Normal file
20
backend/src/plugins/ZeppelinPluginBlueprint.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { BasePluginType, plugin, PluginBlueprint } from "knub";
|
||||
import * as t from "io-ts";
|
||||
import { pluginConfigPreprocessor } from "../pluginUtils";
|
||||
|
||||
export interface ZeppelinPluginBlueprint<TPluginType extends BasePluginType = BasePluginType> extends PluginBlueprint<TPluginType> {
|
||||
configSchema?: t.TypeC<any>;
|
||||
}
|
||||
|
||||
export function zeppelinPlugin(name: string, blueprint: Omit<ZeppelinPluginBlueprint, "name">): ZeppelinPluginBlueprint;
|
||||
export function zeppelinPlugin<TPluginType extends BasePluginType>():
|
||||
(name: string, blueprint: Omit<ZeppelinPluginBlueprint<TPluginType>, "name">) => ZeppelinPluginBlueprint<TPluginType>;
|
||||
export function zeppelinPlugin(...args) {
|
||||
if (args.length) {
|
||||
const blueprint: ZeppelinPluginBlueprint = plugin(...(args as Parameters<typeof plugin>));
|
||||
blueprint.configPreprocessor = pluginConfigPreprocessor.bind(blueprint);
|
||||
return blueprint;
|
||||
} else {
|
||||
return zeppelinPlugin;
|
||||
}
|
||||
}
|
|
@ -1,301 +1,16 @@
|
|||
import { BasePluginType, configUtils, logger, PluginClass, PluginOptions, BasePluginConfig } from "knub";
|
||||
import { BasePluginType, PluginClass, PluginOptions } from "knub";
|
||||
import * as t from "io-ts";
|
||||
import {
|
||||
deepKeyIntersect,
|
||||
isDiscordRESTError,
|
||||
isSnowflake,
|
||||
isUnicodeEmoji,
|
||||
MINUTES,
|
||||
Not,
|
||||
resolveMember,
|
||||
resolveRoleId,
|
||||
resolveUser,
|
||||
resolveUserId,
|
||||
tDeepPartial,
|
||||
trimEmptyStartEndLines,
|
||||
trimIndents,
|
||||
UnknownUser,
|
||||
} from "../utils";
|
||||
import { Invite, Member, User } from "eris";
|
||||
import { performance } from "perf_hooks";
|
||||
import { decodeAndValidateStrict, StrictValidationError, validate } from "../validatorUtils";
|
||||
import { SimpleCache } from "../SimpleCache";
|
||||
import { TZeppelinKnub } from "../types";
|
||||
import { ERRORS, RecoverablePluginError } from "../RecoverablePluginError";
|
||||
|
||||
const SLOW_RESOLVE_THRESHOLD = 1500;
|
||||
|
||||
/**
|
||||
* Wrapper for the string type that indicates the text will be parsed as Markdown later
|
||||
*/
|
||||
type TMarkdown = string;
|
||||
|
||||
export interface PluginInfo {
|
||||
prettyName: string;
|
||||
description?: TMarkdown;
|
||||
usageGuide?: TMarkdown;
|
||||
configurationGuide?: TMarkdown;
|
||||
}
|
||||
|
||||
export interface CommandInfo {
|
||||
description?: TMarkdown;
|
||||
basicUsage?: TMarkdown;
|
||||
examples?: TMarkdown;
|
||||
usageGuide?: TMarkdown;
|
||||
parameterDescriptions?: {
|
||||
[key: string]: TMarkdown;
|
||||
};
|
||||
optionDescriptions?: {
|
||||
[key: string]: TMarkdown;
|
||||
};
|
||||
}
|
||||
|
||||
export function trimPluginDescription(str) {
|
||||
const emptyLinesTrimmed = trimEmptyStartEndLines(str);
|
||||
const lines = emptyLinesTrimmed.split("\n");
|
||||
const firstLineIndentation = (lines[0].match(/^ +/g) || [""])[0].length;
|
||||
return trimIndents(emptyLinesTrimmed, firstLineIndentation);
|
||||
}
|
||||
|
||||
const inviteCache = new SimpleCache<Promise<Invite>>(10 * MINUTES, 200);
|
||||
import { TZeppelinKnub, ZeppelinPluginInfo } from "../types";
|
||||
import { pluginConfigPreprocessor } from "../pluginUtils";
|
||||
|
||||
export class ZeppelinPluginClass<TPluginType extends BasePluginType = BasePluginType> extends PluginClass<TPluginType> {
|
||||
public static pluginInfo: PluginInfo;
|
||||
public static pluginInfo: ZeppelinPluginInfo;
|
||||
public static showInDocs: boolean = true;
|
||||
|
||||
public static configSchema: t.TypeC<any>;
|
||||
public static dependencies = [];
|
||||
|
||||
protected readonly knub: TZeppelinKnub;
|
||||
|
||||
protected throwRecoverablePluginError(code: ERRORS) {
|
||||
throw new RecoverablePluginError(code, this.guild);
|
||||
}
|
||||
|
||||
protected canActOn(member1: Member, member2: Member, allowSameLevel = false) {
|
||||
if (member2.id === this.client.user.id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ourLevel = this.getMemberLevel(member1);
|
||||
const memberLevel = this.getMemberLevel(member2);
|
||||
return allowSameLevel ? ourLevel >= memberLevel : ourLevel > memberLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public static getStaticDefaultOptions() {
|
||||
// Implemented by plugin
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to fetch the real default options from getStaticDefaultOptions()
|
||||
*/
|
||||
protected getDefaultOptions(): PluginOptions<TPluginType> {
|
||||
return (this.constructor as typeof ZeppelinPluginClass).getStaticDefaultOptions() as PluginOptions<TPluginType>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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): PluginOptions<any> {
|
||||
if (options == null) {
|
||||
options = {
|
||||
enabled: false,
|
||||
};
|
||||
}
|
||||
|
||||
const defaultOptions: any = this.getStaticDefaultOptions();
|
||||
let mergedConfig = configUtils.mergeConfig({}, defaultOptions.config || {}, options.config || {});
|
||||
const mergedOverrides = options.replaceDefaultOverrides
|
||||
? options.overrides
|
||||
: (defaultOptions.overrides || []).concat(options.overrides || []);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
mergedConfig = this.preprocessStaticConfig(mergedConfig);
|
||||
|
||||
const decodedConfig = this.configSchema ? decodeAndValidateStrict(this.configSchema, mergedConfig) : mergedConfig;
|
||||
if (decodedConfig instanceof StrictValidationError) {
|
||||
throw decodedConfig;
|
||||
}
|
||||
|
||||
const decodedOverrides = [];
|
||||
for (const override of mergedOverrides) {
|
||||
const overrideConfigMergedWithBaseConfig = configUtils.mergeConfig({}, mergedConfig, override.config || {});
|
||||
const decodedOverrideConfig = this.configSchema
|
||||
? decodeAndValidateStrict(this.configSchema, overrideConfigMergedWithBaseConfig)
|
||||
: overrideConfigMergedWithBaseConfig;
|
||||
if (decodedOverrideConfig instanceof StrictValidationError) {
|
||||
throw decodedOverrideConfig;
|
||||
}
|
||||
decodedOverrides.push({
|
||||
...override,
|
||||
config: deepKeyIntersect(decodedOverrideConfig, override.config || {}),
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
config: decodedConfig,
|
||||
overrides: decodedOverrides,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper that calls mergeAndValidateStaticOptions()
|
||||
*/
|
||||
protected getMergedOptions(): PluginOptions<TPluginType> {
|
||||
if (!this.mergedPluginOptions) {
|
||||
this.mergedPluginOptions = ((this
|
||||
.constructor as unknown) as typeof ZeppelinPluginClass).mergeAndDecodeStaticOptions(this.pluginOptions);
|
||||
}
|
||||
|
||||
return this.mergedPluginOptions as PluginOptions<TPluginType>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run static type checks and other validations on the given options
|
||||
*/
|
||||
public static validateOptions(options: any): string[] | null {
|
||||
// Validate config values
|
||||
if (this.configSchema) {
|
||||
try {
|
||||
this.mergeAndDecodeStaticOptions(options);
|
||||
} catch (e) {
|
||||
if (e instanceof StrictValidationError) {
|
||||
return e.getErrors();
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// No errors, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
public async runLoad(): Promise<any> {
|
||||
const mergedOptions = this.getMergedOptions(); // This implicitly also validates the config
|
||||
return super.runLoad();
|
||||
}
|
||||
|
||||
public canUseEmoji(snowflake): boolean {
|
||||
if (isUnicodeEmoji(snowflake)) {
|
||||
return true;
|
||||
} else if (isSnowflake(snowflake)) {
|
||||
for (const guild of this.client.guilds.values()) {
|
||||
if (guild.emojis.some(e => (e as any).id === snowflake)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.throwRecoverablePluginError(ERRORS.INVALID_EMOJI);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Intended for cross-plugin functionality
|
||||
*/
|
||||
public getRuntimeOptions() {
|
||||
return this.getMergedOptions();
|
||||
}
|
||||
|
||||
getUser(userResolvable: string): User | UnknownUser {
|
||||
const id = resolveUserId(this.client, userResolvable);
|
||||
return id ? this.client.users.get(id) || new UnknownUser({ id }) : new UnknownUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a user from the passed string. The passed string can be a user id, a user mention, a full username (with discrim), etc.
|
||||
* If the user is not found in the cache, it's fetched from the API.
|
||||
*/
|
||||
async resolveUser(userResolvable: string): Promise<User | UnknownUser>;
|
||||
async resolveUser<T>(userResolvable: Not<T, string>): Promise<UnknownUser>;
|
||||
async resolveUser(userResolvable) {
|
||||
const start = performance.now();
|
||||
const user = await resolveUser(this.client, 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @param roleResolvable
|
||||
*/
|
||||
async resolveRoleId(roleResolvable: string): Promise<string | null> {
|
||||
const roleId = await resolveRoleId(this.client, this.guildId, roleResolvable);
|
||||
return roleId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
async getMember(memberResolvable: string, forceFresh = false): Promise<Member> {
|
||||
const start = performance.now();
|
||||
|
||||
let member;
|
||||
if (forceFresh) {
|
||||
const userId = await resolveUserId(this.client, memberResolvable);
|
||||
try {
|
||||
member = userId && (await this.client.getRESTGuildMember(this.guild.id, userId));
|
||||
} catch (e) {
|
||||
if (!isDiscordRESTError(e)) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
if (member) member.id = member.user.id;
|
||||
} else {
|
||||
member = await resolveMember(this.client, 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;
|
||||
}
|
||||
|
||||
async resolveInvite(code: string): Promise<Invite | null> {
|
||||
if (inviteCache.has(code)) {
|
||||
return inviteCache.get(code);
|
||||
}
|
||||
|
||||
const promise = this.client.getInvite(code).catch(() => null);
|
||||
inviteCache.set(code, promise);
|
||||
|
||||
return promise;
|
||||
public static configPreprocessor(options: PluginOptions<any>) {
|
||||
return pluginConfigPreprocessor.bind(this)(options);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ import { AutoDeletePlugin } from "./AutoDelete";
|
|||
/**
|
||||
* Plugins available to be loaded for individual guilds
|
||||
*/
|
||||
export const availablePlugins = [
|
||||
export const oldAvailablePlugins = [
|
||||
AutomodPlugin,
|
||||
MessageSaverPlugin,
|
||||
NameHistoryPlugin,
|
||||
|
@ -67,7 +67,7 @@ export const availablePlugins = [
|
|||
/**
|
||||
* Plugins that are always loaded (subset of the names of the plugins in availablePlugins)
|
||||
*/
|
||||
export const basePlugins = [
|
||||
export const oldBasePlugins = [
|
||||
GuildInfoSaverPlugin.pluginName,
|
||||
MessageSaverPlugin.pluginName,
|
||||
NameHistoryPlugin.pluginName,
|
||||
|
@ -78,4 +78,12 @@ export const basePlugins = [
|
|||
/**
|
||||
* Available global plugins (can't be loaded per-guild, only globally)
|
||||
*/
|
||||
export const availableGlobalPlugins = [BotControlPlugin, UsernameSaver, GuildConfigReloader];
|
||||
export const oldAvailableGlobalPlugins = [BotControlPlugin, UsernameSaver, GuildConfigReloader];
|
||||
|
||||
export const availablePlugins = [
|
||||
UtilityPlugin,
|
||||
];
|
||||
|
||||
export const basePlugins = [];
|
||||
|
||||
export const availableGlobalPlugins = [];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue