Skip to content

Commit

Permalink
feat(help): render as table
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Oct 9, 2023
1 parent 5afd039 commit 0b005ae
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
60 changes: 44 additions & 16 deletions src/handlers/comment/handlers/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,53 @@ export const listAvailableCommands = async (body: string) => {
return generateHelpMenu();
};

export const generateHelpMenu = () => {
export function generateHelpMenu() {
const config = Runtime.getState().botConfig;
const startEnabled = config.command.find((command) => command.name === "start");
let helpMenu = "### Available Commands\n```";
let helpMenu = "### Available Commands\n\n| Command | Description | Example |\n| --- | --- | --- |\n";
const commands = userCommands();
commands.map((command) => {
// if first command, add a new line
if (command.id === commands[0].id) {
helpMenu += `\n`;
if (!startEnabled) return;
}
helpMenu += `- ${command.id}: ${command.description}`;
// if not last command, add a new line (fixes too much space below)
if (command.id !== commands[commands.length - 1].id) {
helpMenu += `\n`;
}
});
commands.map(
(command) =>
(helpMenu += `| \`${command.id}\` | ${breakSentences(command.description)} | ${breakLongString(
command.example || ""
)} |\n`) // add to help menu
);

if (!startEnabled)
helpMenu += "```\n***_To assign yourself to an issue, please open a draft pull request that is linked to it._***";
helpMenu += "\n\n***_To assign yourself to an issue, please open a draft pull request that is linked to it._***";

return helpMenu;
};
}

function breakLongString(str: string, maxLen = 24) {
let newStr = "";
let spaceIndex = str.lastIndexOf(" ", maxLen);

while (str.length > maxLen) {
if (spaceIndex > -1) {
newStr += str.slice(0, spaceIndex) + "<br>";
str = str.slice(spaceIndex + 1);
} else {
const forcedBreakIndex = str.slice(0, maxLen).lastIndexOf(" ");
if (forcedBreakIndex !== -1) {
newStr += str.slice(0, forcedBreakIndex) + "<br>";
str = str.slice(forcedBreakIndex + 1);
} else {
newStr += str.slice(0, maxLen) + "<br>";
str = str.slice(maxLen);
}
}
spaceIndex = str.lastIndexOf(" ", maxLen);
}

newStr += str;
return newStr;
}

function breakSentences(str: string) {
const sentences = str.endsWith(".") ? str.slice(0, -1).split(". ") : str.split(". ");
if (sentences.length <= 1) {
return str;
}
return sentences.join(".<br><br>");
}
25 changes: 18 additions & 7 deletions src/handlers/comment/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function _renderErrorDiffWrapper(err: unknown, issue: Issue) {
let commentBody;
if (err instanceof Error) {
console.trace(err);
commentBody = `${err.message}\n\n${err.stack}`;
commentBody = `${err.message}${err.stack}`;
} else {
console.trace(err);
commentBody = JSON.stringify(err);
Expand Down Expand Up @@ -269,19 +269,22 @@ export function userCommands(): UserCommands[] {
{
id: IssueCommentCommands.START,
description: "Assign yourself to the issue.",
example: void 0,
handler: assign,
callback: commandCallback,
},
{
id: IssueCommentCommands.STOP,
description: "Unassign yourself from the issue.",
example: void 0,
handler: unassign,
callback: commandCallback,
},
{
handler: listAvailableCommands,
id: IssueCommentCommands.HELP,
description: "List all available commands.",
example: void 0,
callback: commandCallback,
},
// Commented out until Gnosis Safe is integrated (https://github.com/ubiquity/ubiquibot/issues/353)
Expand All @@ -294,42 +297,50 @@ export function userCommands(): UserCommands[] {
{
id: IssueCommentCommands.AUTOPAY,
description: "Toggle automatic payment for the completion of the current issue.",
example: void 0,
handler: autoPay,
callback: commandCallback,
},
{
id: IssueCommentCommands.QUERY,
description: `Comments the users multiplier and address`,
description: `Comments the users multiplier and address.`,
example: void 0,
handler: query,
callback: commandCallback,
},
{
id: IssueCommentCommands.ASK,
description: `Ask a technical question to UbiquiBot. \n example usage: "/ask How do I do X?"`,
description: `Ask a technical question to UbiquiBot.`,
example: "/ask how do I do x?",
handler: ask,
callback: commandCallback,
},
{
id: IssueCommentCommands.MULTIPLIER,
description: `Set the task payout multiplier for a specific contributor, and provide a reason for why.\n\te.g. /wallet @user 0.5 "Multiplier reason"`,
description: `Set the task payout multiplier for a specific contributor, and provide a reason for why.`,
example: '/wallet @user 0.5 "multiplier reason"',
handler: multiplier,
callback: commandCallback,
},
{
id: IssueCommentCommands.LABELS,
description: `Set access control. Superuser only.`,
description: `Set access control, for admins only.`,
example: void 0,
handler: setLabels,
callback: commandCallback,
},
{
id: IssueCommentCommands.AUTHORIZE,
description: `Approve a label change. Superuser only.`,
description: `Approve a label change, for admins only.`,
example: void 0,
handler: approveLabelChange,
callback: commandCallback,
},
{
id: IssueCommentCommands.WALLET,
description: `<WALLET_ADDRESS | ENS_NAME> <SIGNATURE_HASH>: Register your wallet address for payments.\n\tYour message to sign is: "UbiquiBot"\n\tYou can generate SIGNATURE_HASH at https://etherscan.io/verifiedSignatures\n\te.g. "/wallet 0x0000000000000000000000000000000000000000 0xe2a3e34a63f3def2c29605de82225b79e1398190b542be917ef88a8e93ff9dc91bdc3ef9b12ed711550f6d2cbbb50671aa3f14a665b709ec391f3e603d0899a41b"`,
description: `Register your wallet address for payments. Your message to sign is: "UbiquiBot". You can generate a signature hash using https://etherscan.io/verifiedSignatures`,
example:
"/wallet ubq.eth 0xe2a3e34a63f3def2c29605de82225b79e1398190b542be917ef88a8e93ff9dc91bdc3ef9b12ed711550f6d2cbbb50671aa3f14a665b709ec391f3e603d0899a41b",
handler: registerWallet,
callback: commandCallback,
},
Expand Down
1 change: 1 addition & 0 deletions src/types/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type Handler = {
export type UserCommands = {
id: string;
description: string;
example?: string;
handler: CommandsHandler;
callback: CallbackHandler;
// successComment?: string;
Expand Down

0 comments on commit 0b005ae

Please sign in to comment.