Skip to content

Commit

Permalink
assets fn complete
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed May 31, 2024
1 parent c823033 commit 898fdf5
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}


0 comments on commit 898fdf5

Please sign in to comment.