more intuitive context.options and Asset typings

This commit is contained in:
Jacob Nguyen
2024-06-02 23:43:55 -05:00
parent 898fdf52a3
commit 2120b18c4e
2 changed files with 16 additions and 20 deletions

View File

@@ -14,7 +14,6 @@ import * as assert from 'assert';
import type { ReplyOptions } from '../../types/utility';
import { fmt } from '../functions'
type ShitType = ChatInputCommandInteraction['options']
/**
* @since 1.0.0
@@ -22,21 +21,13 @@ type ShitType = ChatInputCommandInteraction['options']
* 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;
}
}

View File

@@ -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)) {
@@ -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);
}
}