mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Combine Knub's type helpers with Zeppelin's, continue Utility plugin port
This commit is contained in:
parent
b338351e37
commit
9f059f33af
13 changed files with 533 additions and 13 deletions
91
backend/src/plugins/Utility/commands/HelpCmd.ts
Normal file
91
backend/src/plugins/Utility/commands/HelpCmd.ts
Normal file
|
@ -0,0 +1,91 @@
|
|||
import { utilityCmd } from "../types";
|
||||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { createChunkedMessage, messageLink } from "../../../utils";
|
||||
import { sendErrorMessage } from "../../../pluginUtils";
|
||||
import { TextChannel } from "eris";
|
||||
import { ZeppelinPlugin } from "../../ZeppelinPlugin";
|
||||
import { PluginCommandDefinition } from "knub/dist/commands/commandUtils";
|
||||
import { LoadedPlugin } from "knub";
|
||||
|
||||
export const HelpCmd = utilityCmd({
|
||||
trigger: "help",
|
||||
description: "Show a quick reference for the specified command's usage",
|
||||
usage: "!help clean",
|
||||
permission: "can_help",
|
||||
|
||||
signature: {
|
||||
command: ct.string({ catchAll: true }),
|
||||
},
|
||||
|
||||
async run({ message: msg, args, pluginData }) {
|
||||
const searchStr = args.command.toLowerCase();
|
||||
|
||||
const matchingCommands: Array<{
|
||||
plugin: LoadedPlugin;
|
||||
command: PluginCommandDefinition;
|
||||
}> = [];
|
||||
|
||||
const guildData = pluginData.getKnubInstance().getLoadedGuild(pluginData.guild.id);
|
||||
for (const plugin of guildData.loadedPlugins.values()) {
|
||||
const registeredCommands = plugin.pluginData.commands.getAll();
|
||||
for (const registeredCommand of registeredCommands) {
|
||||
for (const trigger of registeredCommand.originalTriggers) {
|
||||
const strTrigger = typeof trigger === "string" ? trigger : trigger.source;
|
||||
|
||||
if (strTrigger.startsWith(searchStr)) {
|
||||
matchingCommands.push({
|
||||
plugin,
|
||||
command: registeredCommand,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const totalResults = matchingCommands.length;
|
||||
const limitedResults = matchingCommands.slice(0, 3);
|
||||
const commandSnippets = limitedResults.map(({ plugin, command }) => {
|
||||
const prefix: string = command.originalPrefix
|
||||
? typeof command.originalPrefix === "string"
|
||||
? command.originalPrefix
|
||||
: command.originalPrefix.source
|
||||
: "";
|
||||
|
||||
const originalTrigger = command.originalTriggers[0];
|
||||
const trigger: string = originalTrigger
|
||||
? typeof originalTrigger === "string"
|
||||
? originalTrigger
|
||||
: originalTrigger.source
|
||||
: "";
|
||||
|
||||
const description = command.config.extra.blueprint.description;
|
||||
const usage = command.config.extra.blueprint.usage;
|
||||
const commandSlug = trigger
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/\s/g, "-");
|
||||
|
||||
const pluginName = plugin.blueprint?.name || plugin.class?.pluginName;
|
||||
|
||||
let snippet = `**${prefix}${trigger}**`;
|
||||
if (description) snippet += `\n${description}`;
|
||||
if (usage) snippet += `\nBasic usage: \`${usage}\``;
|
||||
snippet += `\n<https://zeppelin.gg/docs/plugins/${pluginName}/usage#command-${commandSlug}>`;
|
||||
|
||||
return snippet;
|
||||
});
|
||||
|
||||
if (totalResults === 0) {
|
||||
msg.channel.createMessage("No matching commands found!");
|
||||
return;
|
||||
}
|
||||
|
||||
let message =
|
||||
totalResults !== limitedResults.length
|
||||
? `Results (${totalResults} total, showing first ${limitedResults.length}):\n\n`
|
||||
: "";
|
||||
|
||||
message += `${commandSnippets.join("\n\n")}`;
|
||||
createChunkedMessage(msg.channel, message);
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue