mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
more intuitive context.options and Asset typings
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
15
src/index.ts
15
src/index.ts
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user