Skip to content

Commit

Permalink
remove asset
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Jun 13, 2024
1 parent 222ecd9 commit 30feb79
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export async function callInitPlugins(module: Module, deps: Dependencies, emit?:
absPath: _module.meta.absPath,
deps
});
if (!res) throw Error("Plugin did not return anything.");
if (!res) throw Error("Plugin did not return anything. " + inspect(plugin, false, Infinity, true));
if(res.isErr()) {
if(emit) {
emitter?.emit('module.register',
Expand Down
3 changes: 1 addition & 2 deletions src/handlers/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { createInteractionHandler, executeModule, intoTask, sharedEventStream, f
import { SernError } from '../core/structures/enums'
import { isAutocomplete, isCommand, isMessageComponent, isModal, resultPayload } from '../core/functions'
import { UnpackedDependencies } from '../types/utility';
import { Emitter } from '../core/interfaces';

export default function interactionHandler(deps: UnpackedDependencies, defaultPrefix?: string) {
//i wish javascript had clojure destructuring
const { '@sern/client': client,
'@sern/emitter': emitter } = deps
const interactionStream$ = sharedEventStream<Interaction>(client as unknown as Emitter, 'interactionCreate');
const interactionStream$ = sharedEventStream<Interaction>(client, 'interactionCreate');
const handle = createInteractionHandler(interactionStream$, deps, defaultPrefix);

const interactionHandler$ = merge(handle(isMessageComponent),
Expand Down
37 changes: 1 addition & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'node:fs/promises'
import path from 'node:path'
export * as Sern from './sern';

export type {
Expand Down Expand Up @@ -51,45 +49,12 @@ 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 { Asset } from './core/structures/asset';
export * from './core/ioc';

export type AssetEncoding = "attachment"|"base64"|"binary"|"utf8"|"json"
type PartialAssetEncoding = Exclude<AssetEncoding, 'attachment' | 'json' >
const ASSETS_DIR = path.resolve('assets');

/**
* Reads an asset file from the 'assets' directory.
* If encoding is 'attachment', a discord.js AttachmentBuilder is provided, else
* fs.promises.readFile is called. The default encoding is utf8.
*/
export async function Asset(p: string, opts?: { name?: never, encoding: PartialAssetEncoding }): Promise<string>;
export async function Asset(p: string, opts?: { name?: never, encoding: 'json' }): Promise<any>;
export async function Asset(p: string, opts?: { name?: string, encoding: 'attachment' }): Promise<AttachmentBuilder>;
export async function Asset(p: string, opts?: { name?: string, encoding: AssetEncoding }): Promise<string|AttachmentBuilder> {
const encoding = opts?.encoding || 'utf8';

let relativePath: string;
if (path.isAbsolute(p)) {
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 if(encoding === 'json') {
return fs.readFile(filePath, 'utf8')
.then(JSON.parse)
} else {
return fs.readFile(filePath, encoding);
}
}

0 comments on commit 30feb79

Please sign in to comment.