Skip to content

Commit

Permalink
create command loader file
Browse files Browse the repository at this point in the history
  • Loading branch information
yorunoken committed Oct 18, 2024
1 parent 47f621d commit d75fbc2
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/utils/loadCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { readdir } from "node:fs/promises";
import type { Client as LilybirdClient, ApplicationCommand } from "lilybird";
import { DefaultPrefixCommand, DefaultApplicationCommand } from "types/interfaces/commands";

export const prefixCommands = new Map<string, DefaultPrefixCommand>();
export const prefixCommandAliases = new Map<string, string>();
export const applicationCommands = new Map<string, DefaultApplicationCommand>();

export async function loadPrefixCommands(): Promise<void> {
// Temporary array to store promises of PrefixCommands
const tempCommands: Array<Promise<DefaultPrefixCommand>> = [];

const items = await readdir("./src/commands-prefix", { recursive: true });
for (let i = 0; i < items.length; i++) {
const item = items[i];
const [category, cmd] = item.split(process.platform === "win32" ? "\\" : "/");
if (!category || !cmd) continue;

const command = import(`../commands-prefix/${category}/${cmd}`) as Promise<DefaultPrefixCommand>;
tempCommands.push(command);
}

const commands = await Promise.all(tempCommands);
for (let i = 0; i < commands.length; i++) {
const defaultCommand = commands[i];
const { default: command } = defaultCommand;
const { aliases, name } = command.data;

// Add the command to the command map
prefixCommands.set(name, defaultCommand);

// Check if the command has aliases and add them to the command map
if (aliases && aliases.length > 0 && Array.isArray(aliases)) {
for (let idx = 0; idx < aliases.length; idx++) {
const alias = aliases[idx];
prefixCommandAliases.set(alias, name);
}
}
}
}

export async function loadApplicationCommands(lilybirdClient: LilybirdClient): Promise<void> {
const applicationCommandsInfo: Array<ApplicationCommand.Create.ApplicationCommandJSONParams> = [];
const tempCommands: Array<Promise<DefaultApplicationCommand>> = [];

const items = await readdir("./src/commands-application", { recursive: true });
for (let i = 0; i < items.length; i++) {
const item = items[i];
const [category, cmd] = item.split(process.platform === "win32" ? "\\" : "/");
if (!category || !cmd) continue;
if (category === "data") continue;

const command = import(`../commands-application/${category}/${cmd}`) as Promise<DefaultApplicationCommand>;
tempCommands.push(command);
}

const commands = await Promise.all(tempCommands);
for (let i = 0; i < commands.length; i++) {
const command = commands[i];
const { default: cmd } = command;
applicationCommandsInfo.push(cmd.data);
applicationCommands.set(cmd.data.name, command);
}

const commandsIds = await lilybirdClient.rest.bulkOverwriteGlobalApplicationCommand(lilybirdClient.user.id, applicationCommandsInfo);
for (let i = 0; i < commandsIds.length; i++) {
const { name, id } = commandsIds[i];
// slashCommandsIds.set(name, `</${name}:${id}>`);
}
}

0 comments on commit d75fbc2

Please sign in to comment.