Skip to content

Commit

Permalink
Add ability for placeholders in help texts
Browse files Browse the repository at this point in the history
  • Loading branch information
holzmaster committed Mar 26, 2024
1 parent d473ac3 commit 2bb625e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/commands/hilfe.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as fs from "node:fs/promises";
import * as path from "node:path";

import { messageCommands } from "../handler/commandHandler.js";
import type { CommandFunction } from "../types.js";
import type { BotContext } from "../context.js";
import { messageCommands } from "../handler/commandHandler.js";

/**
* Retrieves commands in chunks that doesn't affect message limit
Expand Down Expand Up @@ -68,7 +69,10 @@ export const run: CommandFunction = async (
const commandStr =
context.prefix.command +
file.toLowerCase().replace(/\.js/gi, "");
commandObj[commandStr] = module.description;
commandObj[commandStr] = replacePrefixPlaceholders(
module.description,
context,
);
}
}
}
Expand All @@ -77,7 +81,10 @@ export const run: CommandFunction = async (
const userCommands = messageCommands.filter(cmd => !cmd.modCommand);
for (const cmd of userCommands) {
const commandStr = context.prefix.command + cmd.name;
commandObj[commandStr] = cmd.description;
commandObj[commandStr] = replacePrefixPlaceholders(
cmd.description,
context,
);
}

await message.author.send(
Expand All @@ -93,4 +100,13 @@ export const run: CommandFunction = async (
await message.react("✉"); // Send this last, so we only display a confirmation when everything actually worked
};

export function replacePrefixPlaceholders(
helpText: string,
context: BotContext,
): string {
return helpText
.replace("$MOD_COMMAND_PREFIX$", context.prefix.modCommand)
.replace("$COMMAND_PREFIX$", context.prefix.command);
}

export const description = "Listet alle commands auf";
8 changes: 6 additions & 2 deletions src/commands/modcommands/hilfe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { promises as fs } from "node:fs";
import * as path from "node:path";

import type { CommandFunction } from "../../types.js";
import { replacePrefixPlaceholders } from "../hilfe.js";

/**
* Enlists all mod-commands with descriptions
Expand Down Expand Up @@ -36,15 +37,18 @@ export const run: CommandFunction = async (

const module = await import(modulePath);

commandObj[commandStr] = module.description;
commandObj[commandStr] = replacePrefixPlaceholders(
module.description,
context,
);
}
}

let commandText = "";
for (const [commandName, description] of Object.entries(commandObj)) {
commandText += commandName;
commandText += ":\n";
commandText += description;
commandText += replacePrefixPlaceholders(description, context);
commandText += "\n\n";
}

Expand Down

0 comments on commit 2bb625e

Please sign in to comment.