Skip to content

Commit

Permalink
more intuitive context.options and Asset typings
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Jun 3, 2024
1 parent 898fdf5 commit 2120b18
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
21 changes: 6 additions & 15 deletions src/core/structures/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,20 @@ import * as assert from 'assert';
import type { ReplyOptions } from '../../types/utility';
import { fmt } from '../functions'

type ShitType = ChatInputCommandInteraction['options']

/**
* @since 1.0.0
* Provides values shared between
* Message and ChatInputCommandInteraction
*/
export class Context extends CoreContext<Message, ChatInputCommandInteraction> {

get options() {
return this.interaction.options;
}
args(type: 'message') : string[]
args(type: 'interaction') : ShitType
//TODO
args(type: 'message'|'interaction') {
switch(type) {
case 'message': {
const [, ...rest] = fmt(this.message.content, this.prefix);
return rest;
};
case 'interaction': {
return this.interaction.options;
};
if(this.isMessage()) {
const [, ...rest] = fmt(this.message.content, this.prefix);
return rest;
} else {
return this.interaction.options;
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ export * from './core/ioc';
export type AssetEncoding = "attachment"|"base64"|"binary"|"utf8"

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 is utf8.
*/
export async function Asset(p: string, opts: { name?: string, encoding?: AssetEncoding }) {
const encoding = opts.encoding || 'utf8';
export async function Asset(p: string, opts?: { name?: string, encoding: Exclude<AssetEncoding, 'attachment'> }): Promise<string>;
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)) {
Expand All @@ -76,11 +83,9 @@ export async function Asset(p: string, opts: { name?: string, encoding?: AssetEn
const filePath = path.join(ASSETS_DIR, relativePath);

if (encoding === 'attachment') {
const attachmentName = opts.name || path.basename(filePath);
const attachmentName = opts?.name || path.basename(filePath);
return new AttachmentBuilder(filePath, { name: attachmentName });
} else {
return fs.readFile(filePath, encoding);
}
}


0 comments on commit 2120b18

Please sign in to comment.