fix: use fakePromise

This commit is contained in:
MARCROCK22 2024-05-31 00:39:49 +00:00
parent 482de42b28
commit 52ee517fdc

View File

@ -1,64 +1,68 @@
import type { APIMessage, APIUser } from 'discord-api-types/v10'; import type { APIMessage, APIUser } from 'discord-api-types/v10';
import { GuildRelatedResource } from './default/guild-related'; import { GuildRelatedResource } from './default/guild-related';
import type { MessageData, ReturnCache } from '../..'; import type { MessageData, ReturnCache } from '../..';
import { fakePromise } from '../../common'; import { fakePromise } from '../../common';
import { Message } from '../../structures'; import { Message } from '../../structures';
export class Messages extends GuildRelatedResource { export class Messages extends GuildRelatedResource {
namespace = 'message'; namespace = 'message';
//@ts-expect-error //@ts-expect-error
filter(data: MessageData, id: string, channel_id?: string) { filter(data: MessageData, id: string, channel_id?: string) {
return true; return true;
} }
override parse(data: any, _key: string, _channel_id: string) { override parse(data: any, _key: string, _channel_id: string) {
const { author, member, ...rest } = data; const { author, member, ...rest } = data;
if (author?.id) rest.user_id = author.id; if (author?.id) rest.user_id = author.id;
return rest; return rest;
} }
override get(id: string): ReturnCache<Message | undefined> { override get(id: string): ReturnCache<Message | undefined> {
return fakePromise(super.get(id) as APIMessageResource | undefined).then(rawMessage => { return fakePromise(super.get(id) as APIMessageResource | undefined).then(rawMessage => {
const user = return this.cache.users && rawMessage?.user_id
this.cache.users && rawMessage?.user_id ? fakePromise(this.cache.adapter.get(this.cache.users.hashId(rawMessage.user_id)) as APIUser | undefined).then(
? (this.cache.adapter.get(this.cache.users.hashId(rawMessage.user_id)) as APIUser | undefined) user => {
: undefined; return user ? new Message(this.client, { ...rawMessage!, author: user }) : undefined;
return user ? new Message(this.client, { ...rawMessage!, author: user }) : undefined; },
}); )
} : undefined;
});
override bulk(ids: string[]): ReturnCache<Message[]> { }
return fakePromise(super.bulk(ids) as APIMessageResource[]).then(
messages => override bulk(ids: string[]): ReturnCache<Message[]> {
messages return fakePromise(super.bulk(ids) as APIMessageResource[]).then(
.map(rawMessage => { messages =>
const user = messages
this.cache.users && rawMessage.user_id .map(rawMessage => {
? (this.cache.adapter.get(this.cache.users.hashId(rawMessage.user_id)) as APIUser | undefined) return this.cache.users && rawMessage?.user_id
: undefined; ? fakePromise(
return user ? new Message(this.client, { ...rawMessage, author: user }) : undefined; this.cache.adapter.get(this.cache.users.hashId(rawMessage.user_id)) as APIUser | undefined,
}) ).then(user => {
.filter(Boolean) as Message[], return user ? new Message(this.client, { ...rawMessage!, author: user }) : undefined;
); })
} : undefined;
})
override values(guild: string): ReturnCache<Message[]> { .filter(Boolean) as Message[],
return fakePromise(super.values(guild) as APIMessageResource[]).then(messages => { );
const hashes: (string | undefined)[] = this.cache.users }
? messages.map(x => (x.user_id ? this.cache.users!.hashId(x.user_id) : undefined))
: []; override values(guild: string): ReturnCache<Message[]> {
return fakePromise(this.cache.adapter.get(hashes.filter(Boolean) as string[]) as APIUser[]).then(users => { return fakePromise(super.values(guild) as APIMessageResource[]).then(messages => {
return messages const hashes: (string | undefined)[] = this.cache.users
.map(message => { ? messages.map(x => (x.user_id ? this.cache.users!.hashId(x.user_id) : undefined))
const user = users.find(user => user.id === message.user_id); : [];
return user ? new Message(this.client, { ...message, author: user }) : undefined; return fakePromise(this.cache.adapter.get(hashes.filter(Boolean) as string[]) as APIUser[]).then(users => {
}) return messages
.filter(Boolean) as Message[]; .map(message => {
}); const user = users.find(user => user.id === message.user_id);
}); return user ? new Message(this.client, { ...message, author: user }) : undefined;
} })
} .filter(Boolean) as Message[];
});
export type APIMessageResource = Omit<APIMessage, 'author'> & { user_id?: string }; });
}
}
export type APIMessageResource = Omit<APIMessage, 'author'> & { user_id?: string };