import { ApplicationCommandType, ComponentType, InteractionType, MessageComponentInteraction, MessageComponentType } from 'discord.js'; import { readdirSync, statSync } from 'fs'; import { join } from 'path'; import { from, Observable } from 'rxjs'; import { CommandType } from '../sern'; import type { PluggedModule } from '../structures/modules/module'; export const BothCommand = new Map(); export const ApplicationCommandStore = { [ApplicationCommandType.User] : new Map(), [ApplicationCommandType.Message] : new Map(), [ApplicationCommandType.ChatInput] : new Map(), } as {[K in ApplicationCommandType] : Map }; export const MessageCompCommandStore = { [ComponentType.Button] : new Map(), [ComponentType.SelectMenu] : new Map() }; export const TextCommandStore = { text : new Map(), aliases : new Map() }; // Courtesy @Townsy45 function readPath(dir: string, arrayOfFiles: string[] = []): string[] { try { const files = readdirSync(dir); for (const file of files) { if (statSync(dir + '/' + file).isDirectory()) readPath(dir + '/' + file, arrayOfFiles); else arrayOfFiles.push(join(dir, '/', file)); } } catch (err) { throw err; } return arrayOfFiles; } export const fmtFileName = (n: string) => n.substring(0, n.length - 3); /** * * @param {commandsDir} Relative path to commands directory * @returns {Promise<{ mod: PluggedModule; absPath: string; }[]>} data from command files */ export function buildData(commandDir: string ): Observable< { plugged: PluggedModule; absPath: string; }> { return from(getCommands(commandDir).map(absPath => { const plugged = ( require(absPath).module); return { plugged, absPath }; })); } export function getCommands(dir: string): string[] { return readPath(join(process.cwd(), dir)); }