scripts/pushCommands: Refactor how commands are being read

this should reduce errors when reading commands
also this won't fail if reading one command fails
This commit is contained in:
Wroclaw 2023-10-12 11:55:53 +02:00
parent a05047ab7d
commit a186ba9e80

View file

@ -9,15 +9,21 @@ import Command from "../src/command";
const post: RESTPostAPIApplicationCommandsJSONBody[] = [];
const guildId = process.argv.slice(2)[0];
requireDirectory<{default: Command}, void>(module, "../src/commands", {
visit: function (obj) {
console.log(obj);
const importedCommands = requireDirectory(module, "../src/commands");
for (const obj in importedCommands) {
try {
const allExports = importedCommands[obj] as {default: unknown};
const defaultExport = allExports.default;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
post.push(new obj.default().toRESTPostApplicationCommands());
},
});
// @ts-expect-error
const constructedExport = new defaultExport() as unknown;
if (!(constructedExport instanceof Command)) throw new Error(`${obj}'s default does not extends Command`);
post.push(constructedExport.toRESTPostApplicationCommands());
} catch (e) {
console.error(e);
}
}
const rest = new REST().setToken(config.tokens.Discord);