fix: collectors

This commit is contained in:
MARCROCK22 2024-08-03 23:59:52 +00:00
parent c3ea1a394a
commit f3ba75155d
4 changed files with 12 additions and 7 deletions

View File

@ -141,7 +141,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
protected async onPacket(shardId: number, packet: GatewayDispatchPayload) { protected async onPacket(shardId: number, packet: GatewayDispatchPayload) {
Promise.allSettled([ Promise.allSettled([
this.events?.runEvent('RAW', this, packet, shardId, false), this.events?.runEvent('RAW', this, packet, shardId, false),
this.collectors.run('RAW', packet), this.collectors.run('RAW', packet, this),
]); //ignore promise ]); //ignore promise
switch (packet.t) { switch (packet.t) {
//// Cases where we must obtain the old data before updating //// Cases where we must obtain the old data before updating

View File

@ -2,6 +2,8 @@ import { randomUUID } from 'node:crypto';
import type { Awaitable, CamelCase } from '../common'; import type { Awaitable, CamelCase } from '../common';
import type { CallbackEventHandler, CustomEventsKeys, GatewayEvents } from '../events'; import type { CallbackEventHandler, CustomEventsKeys, GatewayEvents } from '../events';
import { error } from 'node:console'; import { error } from 'node:console';
import * as RawEvents from '../events/hooks';
import type { UsingClient } from '../commands';
export type AllClientEvents = CustomEventsKeys | GatewayEvents; export type AllClientEvents = CustomEventsKeys | GatewayEvents;
export type ParseClientEventName<T extends AllClientEvents> = T extends CustomEventsKeys ? T : CamelCase<T>; export type ParseClientEventName<T extends AllClientEvents> = T extends CustomEventsKeys ? T : CamelCase<T>;
@ -98,11 +100,14 @@ export class Collectors {
/**@internal */ /**@internal */
async run<T extends AllClientEvents>( async run<T extends AllClientEvents>(
name: T, name: T,
data: Awaited<Parameters<CallbackEventHandler[ParseClientEventName<T>]>[0]>, raw: Awaited<Parameters<CallbackEventHandler[ParseClientEventName<T>]>[0]>,
client: UsingClient,
) { ) {
const collectors = this.values.get(name); const collectors = this.values.get(name);
if (!collectors) return; if (!collectors) return;
const data = RawEvents[name]?.(client, raw as never) ?? raw;
for (const i of collectors) { for (const i of collectors) {
if (await i.options.filter(data as never)) { if (await i.options.filter(data as never)) {
i.idle?.refresh(); i.idle?.refresh();

View File

@ -357,7 +357,7 @@ export class WorkerClient<Ready extends boolean = boolean> extends BaseClient {
protected async onPacket(packet: GatewayDispatchPayload, shardId: number) { protected async onPacket(packet: GatewayDispatchPayload, shardId: number) {
Promise.allSettled([ Promise.allSettled([
this.events?.runEvent('RAW', this, packet, shardId, false), this.events?.runEvent('RAW', this, packet, shardId, false),
this.collectors.run('RAW', packet), this.collectors.run('RAW', packet, this),
]); //ignore promise ]); //ignore promise
switch (packet.t) { switch (packet.t) {
//// Cases where we must obtain the old data before updating //// Cases where we must obtain the old data before updating

View File

@ -111,7 +111,7 @@ export class EventHandler extends BaseHandler {
await Promise.all([ await Promise.all([
this.runEvent(args[0].t as never, args[1], args[0].d, args[2]), this.runEvent(args[0].t as never, args[1], args[0].d, args[2]),
this.client.collectors.run(args[0].t as never, args[0].d as never), this.client.collectors.run(args[0].t as never, args[0].d as never, this.client),
]); ]);
} }
@ -150,15 +150,15 @@ export class EventHandler extends BaseHandler {
async runCustom<T extends CustomEventsKeys>(name: T, ...args: Parameters<CustomEvents[T]>) { async runCustom<T extends CustomEventsKeys>(name: T, ...args: Parameters<CustomEvents[T]>) {
const Event = this.values[name]; const Event = this.values[name];
if (!Event) { if (!Event) {
return this.client.collectors.run(name, args as never); return this.client.collectors.run(name, args as never, this.client);
} }
try { try {
if (Event.data.once && Event.fired) { if (Event.data.once && Event.fired) {
return this.client.collectors.run(name, args as never); return this.client.collectors.run(name, args as never, this.client);
} }
Event.fired = true; Event.fired = true;
this.logger.debug(`executed a custom event [${name}]`, Event.data.once ? 'once' : ''); this.logger.debug(`executed a custom event [${name}]`, Event.data.once ? 'once' : '');
await Promise.all([Event.run(args, this.client), this.client.collectors.run(name, args as never)]); await Promise.all([Event.run(args, this.client), this.client.collectors.run(name, args as never, this.client)]);
} catch (e) { } catch (e) {
await this.onFail(name, e); await this.onFail(name, e);
} }