fix: the function was being misused

This commit is contained in:
MARCROCK22 2024-08-14 21:23:09 +00:00
parent 9066f510e3
commit 603865917b
4 changed files with 9 additions and 17 deletions

View File

@ -1,7 +1,7 @@
import type { APIAttachment, RESTAPIAttachment } from '../types'; import type { APIAttachment, RESTAPIAttachment } from '../types';
import { randomBytes } from 'node:crypto'; import { randomBytes } from 'node:crypto';
import path from 'node:path'; import path from 'node:path';
import { type UsingClient, throwError, type RawFile } from '..'; import type { UsingClient, RawFile } from '..';
import type { ImageResolvable, ObjectToLower } from '../common'; import type { ImageResolvable, ObjectToLower } from '../common';
import { Base } from '../structures/extra/Base'; import { Base } from '../structures/extra/Base';
import { promises } from 'node:fs'; import { promises } from 'node:fs';
@ -183,14 +183,14 @@ export async function resolveAttachmentData(
}> { }> {
if (data instanceof AttachmentBuilder) { if (data instanceof AttachmentBuilder) {
if (!data.data.resolvable) if (!data.data.resolvable)
return throwError('The attachment type has been expressed as attachment but cannot be resolved as one.'); throw new Error('The attachment type has been expressed as attachment but cannot be resolved as one.');
return { data: data.data.resolvable! }; return { data: data.data.resolvable! };
} }
switch (type) { switch (type) {
case 'url': { case 'url': {
if (!/^https?:\/\//.test(data as string)) if (!/^https?:\/\//.test(data as string))
return throwError( throw new Error(
`The attachment type has been expressed as ${type.toUpperCase()} but cannot be resolved as one.`, `The attachment type has been expressed as ${type.toUpperCase()} but cannot be resolved as one.`,
); );
const res = await fetch(data as string); const res = await fetch(data as string);
@ -200,7 +200,7 @@ export async function resolveAttachmentData(
const file = path.resolve(data as string); const file = path.resolve(data as string);
const stats = await promises.stat(file); const stats = await promises.stat(file);
if (!stats.isFile()) if (!stats.isFile())
return throwError( throw new Error(
`The attachment type has been expressed as ${type.toUpperCase()} but cannot be resolved as one.`, `The attachment type has been expressed as ${type.toUpperCase()} but cannot be resolved as one.`,
); );
return { data: await promises.readFile(file) }; return { data: await promises.readFile(file) };
@ -213,12 +213,10 @@ export async function resolveAttachmentData(
for await (const resource of data as unknown as AsyncIterable<ArrayBuffer>) buffers.push(Buffer.from(resource)); for await (const resource of data as unknown as AsyncIterable<ArrayBuffer>) buffers.push(Buffer.from(resource));
return { data: Buffer.concat(buffers) }; return { data: Buffer.concat(buffers) };
} }
return throwError( throw new Error(`The attachment type has been expressed as ${type.toUpperCase()} but cannot be resolved as one.`);
`The attachment type has been expressed as ${type.toUpperCase()} but cannot be resolved as one.`,
);
} }
default: { default: {
return throwError(`The attachment type has been expressed as ${type} but cannot be resolved as one.`); throw new Error(`The attachment type has been expressed as ${type} but cannot be resolved as one.`);
} }
} }
} }
@ -244,7 +242,7 @@ export async function resolveImage(image: ImageResolvable): Promise<string> {
data: { type, resolvable }, data: { type, resolvable },
} = image; } = image;
if (type && resolvable) return resolveBase64((await resolveAttachmentData(resolvable, type)).data as Buffer); if (type && resolvable) return resolveBase64((await resolveAttachmentData(resolvable, type)).data as Buffer);
return throwError( throw new Error(
`The attachment type has been expressed as ${(type ?? 'Attachment').toUpperCase()} but cannot be resolved as one.`, `The attachment type has been expressed as ${(type ?? 'Attachment').toUpperCase()} but cannot be resolved as one.`,
); );
} }

View File

@ -1,6 +1,5 @@
import { type APIPollMedia, PollLayoutType, type RESTAPIPollCreate } from '../types'; import { type APIPollMedia, PollLayoutType, type RESTAPIPollCreate } from '../types';
import type { DeepPartial, EmojiResolvable, RestOrArray } from '../common'; import type { DeepPartial, EmojiResolvable, RestOrArray } from '../common';
import { throwError } from '..';
import { resolvePartialEmoji } from '../structures/extra/functions'; import { resolvePartialEmoji } from '../structures/extra/functions';
export class PollBuilder { export class PollBuilder {
@ -45,7 +44,7 @@ export class PollBuilder {
private resolvedPollMedia(data: PollMedia) { private resolvedPollMedia(data: PollMedia) {
if (!data.emoji) return { text: data.text }; if (!data.emoji) return { text: data.text };
const resolve = resolvePartialEmoji(data.emoji); const resolve = resolvePartialEmoji(data.emoji);
if (!resolve) return throwError('Invalid Emoji'); if (!resolve) throw new Error('Invalid Emoji');
return { text: data.text, emoji: resolve }; return { text: data.text, emoji: resolve };
} }
} }

View File

@ -12,7 +12,6 @@ import {
ComponentType, ComponentType,
SelectMenuDefaultValueType, SelectMenuDefaultValueType,
} from '../types'; } from '../types';
import { throwError } from '..';
import type { EmojiResolvable, RestOrArray, ToClass } from '../common'; import type { EmojiResolvable, RestOrArray, ToClass } from '../common';
import type { import type {
ChannelSelectMenuInteraction, ChannelSelectMenuInteraction,
@ -368,7 +367,7 @@ export class StringSelectOption {
*/ */
setEmoji(emoji: EmojiResolvable) { setEmoji(emoji: EmojiResolvable) {
const resolve = resolvePartialEmoji(emoji); const resolve = resolvePartialEmoji(emoji);
if (!resolve) return throwError('Invalid Emoji'); if (!resolve) throw new Error('Invalid Emoji');
this.data.emoji = resolve as APIMessageComponentEmoji; this.data.emoji = resolve as APIMessageComponentEmoji;
return this; return this;
} }

View File

@ -28,10 +28,6 @@ export * from './structures';
export * from './client'; export * from './client';
/// ///
export function throwError(msg: string): never {
throw new Error(msg);
}
/** /**
* Creates an event with the specified data and run function. * Creates an event with the specified data and run function.
* *