From 898fdf52a3aae810b12b6e724495286af00ffdc2 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Fri, 31 May 2024 00:48:51 -0500 Subject: [PATCH] assets fn complete --- src/index.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index c82e48f3..9ee4c0b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -50,20 +50,37 @@ export { export * from './core/presences' export * from './core/interfaces' import type { controller } from './core/create-plugins'; +import { AttachmentBuilder } from 'discord.js'; export type Controller = typeof controller export * from './core/create-plugins'; export { CommandType, PluginType, PayloadType, EventType } from './core/structures/enums'; export { Context } from './core/structures/context'; export * from './core/ioc'; +export type AssetEncoding = "attachment"|"base64"|"binary"|"utf8" -export async function Asset(p: string) { - const assetsDir = path.resolve('assets'); +const ASSETS_DIR = path.resolve('assets'); +/** + * Reads an asset file from the 'assets' directory. + */ +export async function Asset(p: string, opts: { name?: string, encoding?: AssetEncoding }) { + const encoding = opts.encoding || 'utf8'; + + let relativePath: string; if (path.isAbsolute(p)) { - const relativePath = path.relative(assetsDir, "assets"+p); - return fs.readFile(path.join(assetsDir, relativePath), 'utf8'); + relativePath = path.relative(ASSETS_DIR, "assets" + p); + } else { + relativePath = p; + } + + const filePath = path.join(ASSETS_DIR, relativePath); + + if (encoding === 'attachment') { + const attachmentName = opts.name || path.basename(filePath); + return new AttachmentBuilder(filePath, { name: attachmentName }); + } else { + return fs.readFile(filePath, encoding); } - return fs.readFile(path.join(assetsDir, p), 'utf8'); }