mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-04 22:16:08 +00:00
feat: optimize code & implement onRunError and onStopError in collectors & use Promise.all
This commit is contained in:
parent
3850ef38a6
commit
d2157e7324
8
src/cache/resources/members.ts
vendored
8
src/cache/resources/members.ts
vendored
@ -26,11 +26,11 @@ export class Members extends GuildBasedResource {
|
||||
|
||||
override bulk(ids: string[], guild: string): ReturnCache<GuildMember[]> {
|
||||
return fakePromise(super.bulk(ids, guild)).then(members =>
|
||||
fakePromise(this.client.cache.users?.bulk(ids) ?? []).then(
|
||||
fakePromise(this.client.cache.users?.bulk(ids)).then(
|
||||
users =>
|
||||
members
|
||||
.map(rawMember => {
|
||||
const user = users.find(x => x.id === rawMember.id);
|
||||
const user = users?.find(x => x.id === rawMember.id);
|
||||
return user ? new GuildMember(this.client, rawMember, user, guild) : undefined;
|
||||
})
|
||||
.filter(Boolean) as GuildMember[],
|
||||
@ -40,11 +40,11 @@ export class Members extends GuildBasedResource {
|
||||
|
||||
override values(guild: string): ReturnCache<GuildMember[]> {
|
||||
return fakePromise(super.values(guild)).then(members =>
|
||||
fakePromise(this.client.cache.users?.values() ?? []).then(
|
||||
fakePromise(this.client.cache.users?.values()).then(
|
||||
users =>
|
||||
members
|
||||
.map(rawMember => {
|
||||
const user = users.find(x => x.id === rawMember.id);
|
||||
const user = users?.find(x => x.id === rawMember.id);
|
||||
return user ? new GuildMember(this.client, rawMember, user, rawMember.guild_id) : undefined;
|
||||
})
|
||||
.filter(Boolean) as GuildMember[],
|
||||
|
@ -2,6 +2,7 @@ import { randomUUID } from 'node:crypto';
|
||||
import type { Awaitable, CamelCase, SnakeCase } from '../common';
|
||||
import type { ClientNameEvents, GatewayEvents } from '../events';
|
||||
import type { ClientEvents } from '../events/hooks';
|
||||
import { error } from 'node:console';
|
||||
|
||||
type SnakeCaseClientNameEvents = Uppercase<SnakeCase<ClientNameEvents>>;
|
||||
|
||||
@ -11,8 +12,14 @@ type RunData<T extends SnakeCaseClientNameEvents> = {
|
||||
idle?: number;
|
||||
timeout?: number;
|
||||
onStop?: (reason: string) => unknown;
|
||||
onStopError?: (reason: string, error: unknown) => unknown;
|
||||
filter: (arg: Awaited<ClientEvents[CamelCase<Lowercase<T>>]>) => Awaitable<boolean>;
|
||||
run: (arg: Awaited<ClientEvents[CamelCase<Lowercase<T>>]>, stop: (reason?: string) => void) => unknown;
|
||||
onRunError?: (
|
||||
arg: Awaited<ClientEvents[CamelCase<Lowercase<T>>]>,
|
||||
error: unknown,
|
||||
stop: (reason?: string) => void,
|
||||
) => unknown;
|
||||
};
|
||||
idle?: NodeJS.Timeout;
|
||||
timeout?: NodeJS.Timeout;
|
||||
@ -64,7 +71,7 @@ export class Collectors {
|
||||
return options;
|
||||
}
|
||||
|
||||
private delete(name: SnakeCaseClientNameEvents, nonce: string, reason = 'unknown') {
|
||||
private async delete(name: SnakeCaseClientNameEvents, nonce: string, reason = 'unknown') {
|
||||
const collectors = this.values.get(name);
|
||||
|
||||
if (!collectors?.length) {
|
||||
@ -78,7 +85,11 @@ export class Collectors {
|
||||
clearTimeout(collector.idle);
|
||||
clearTimeout(collector.timeout);
|
||||
collectors.splice(index, 1);
|
||||
return collector.options.onStop?.(reason);
|
||||
try {
|
||||
await collector.options.onStop?.(reason);
|
||||
} catch (e) {
|
||||
await collector.options.onStopError?.(reason, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**@internal */
|
||||
@ -89,9 +100,14 @@ export class Collectors {
|
||||
for (const i of collectors) {
|
||||
if (await i.options.filter(data)) {
|
||||
i.idle?.refresh();
|
||||
await i.options.run(data, (reason = 'unknown') => {
|
||||
const stop = (reason = 'unknown') => {
|
||||
return this.delete(i.options.event, i.nonce, reason);
|
||||
});
|
||||
};
|
||||
try {
|
||||
await i.options.run(data, stop);
|
||||
} catch (e) {
|
||||
await i.options.onRunError?.(data, e, stop);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -352,9 +352,8 @@ export abstract class SubCommand extends BaseCommand {
|
||||
toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
options: (this.options ?? []).map(
|
||||
x => ({ ...x, autocomplete: 'autocomplete' in x }) as APIApplicationCommandBasicOption,
|
||||
),
|
||||
options:
|
||||
this.options?.map(x => ({ ...x, autocomplete: 'autocomplete' in x }) as APIApplicationCommandBasicOption) ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -73,8 +73,10 @@ export class EventHandler extends BaseHandler {
|
||||
break;
|
||||
}
|
||||
|
||||
await this.runEvent(args[0].t, args[1], args[0].d, args[2]);
|
||||
await this.client.collectors.run(args[0].t, args[0].d);
|
||||
await Promise.all([
|
||||
this.runEvent(args[0].t, args[1], args[0].d, args[2]),
|
||||
this.client.collectors.run(args[0].t, args[0].d),
|
||||
]);
|
||||
}
|
||||
|
||||
async runEvent(name: GatewayEvents, client: Client | WorkerClient, packet: any, shardId: number) {
|
||||
|
@ -46,7 +46,7 @@ export class BaseMessage extends DiscordBase {
|
||||
channels: data.mention_channels ?? [],
|
||||
users: [],
|
||||
};
|
||||
this.components = (data.components ?? []).map(x => new MessageActionRowComponent(x));
|
||||
this.components = data.components?.map(x => new MessageActionRowComponent(x)) ?? [];
|
||||
this.embeds = data.embeds.map(embed => new InMessageEmbed(embed));
|
||||
this.patch(data);
|
||||
}
|
||||
@ -213,6 +213,9 @@ export class InMessageEmbed {
|
||||
return this.data.title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
get type() {
|
||||
return this.data.type;
|
||||
}
|
||||
@ -234,19 +237,19 @@ export class InMessageEmbed {
|
||||
}
|
||||
|
||||
get footer() {
|
||||
return toCamelCase(this.data.footer ?? {});
|
||||
return this.data.footer ? toCamelCase(this.data.footer) : undefined;
|
||||
}
|
||||
|
||||
get image() {
|
||||
return toCamelCase(this.data.image ?? {});
|
||||
return this.data.image ? toCamelCase(this.data.image) : undefined;
|
||||
}
|
||||
|
||||
get thumbnail() {
|
||||
return toCamelCase(this.data.thumbnail ?? {});
|
||||
return this.data.thumbnail ? toCamelCase(this.data.thumbnail) : undefined;
|
||||
}
|
||||
|
||||
get video() {
|
||||
return toCamelCase(this.data.video ?? {});
|
||||
return this.data.video ? toCamelCase(this.data.video) : undefined;
|
||||
}
|
||||
|
||||
get provider() {
|
||||
@ -254,7 +257,7 @@ export class InMessageEmbed {
|
||||
}
|
||||
|
||||
get author() {
|
||||
return toCamelCase(this.data.author ?? {});
|
||||
return this.data.author ? toCamelCase(this.data.author) : undefined;
|
||||
}
|
||||
|
||||
get fields() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user