Skip to content

Instantly share code, notes, and snippets.

@TheEssem
Created April 17, 2025 23:29
Show Gist options
  • Save TheEssem/70668c326b236ef00cde40cb598cb8c1 to your computer and use it in GitHub Desktop.
Save TheEssem/70668c326b236ef00cde40cb598cb8c1 to your computer and use it in GitHub Desktop.
A script I wrote to extract the command metadata strings (and some responses) for i18n usage in esmBot.
import { promises } from "node:fs";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import ImageCommand from "./classes/imageCommand.js";
async function* getFiles(dir) {
const dirents = await promises.readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const name = dir + (dir.charAt(dir.length - 1) !== "/" ? "/" : "") + dirent.name;
if (dirent.isDirectory()) {
yield* getFiles(name);
} else if (dirent.name.endsWith(".js")) {
yield name;
}
}
}
const template = {
commands: {
descriptions: {},
flagNames: {},
flags: {},
names: {},
noImage: {},
noText: {}
}
};
ImageCommand.textOptional = true;
ImageCommand.init();
for await (const commandFile of getFiles(resolve(dirname(fileURLToPath(import.meta.url)), "./commands/"))) {
const { default: props } = await import(commandFile);
const commandArray = commandFile.split("/");
let commandName = commandArray[commandArray.length - 1].split(".")[0];
const category = commandArray[commandArray.length - 2];
if (category === "message" || category === "user") {
const nameStringArray = commandName.split("-");
for (const index of nameStringArray.keys()) {
nameStringArray[index] = nameStringArray[index].charAt(0).toUpperCase() + nameStringArray[index].slice(1);
}
commandName = nameStringArray.join(" ");
}
props.init();
template.commands.descriptions[commandName] = props.description;
template.commands.flagNames[commandName] = {};
template.commands.flags[commandName] = {};
template.commands.names[commandName] = commandName;
if (props.noImage && props.noImage !== ImageCommand.noImage) template.commands.noImage[commandName] = props.noImage;
if (props.noText && props.noText !== ImageCommand.noText) template.commands.noText[commandName] = props.noText;
extractFlags(props.flags, commandName);
}
function extractFlags(flags, commandName) {
for (const flag of flags) {
const filtered = ImageCommand.flags.find((v) => v.name === flag.name && v.description === flag.description);
if (filtered) continue;
if (flag.type === 1 && flag.options) {
const nameWithFlag = `${commandName} ${flag.name}`;
template.commands.flagNames[nameWithFlag] = {};
template.commands.flags[nameWithFlag] = {};
extractFlags(flag.options, nameWithFlag);
}
template.commands.flagNames[commandName][flag.name] = flag.name;
template.commands.flags[commandName][flag.name] = flag.description;
}
if (Object.keys(template.commands.flagNames[commandName]).length === 0) {
template.commands.flagNames[commandName] = undefined;
template.commands.flags[commandName] = undefined;
}
}
console.log(JSON.stringify(template));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment