]> git.puffer.fish Git - matthieu/gru.git/commitdiff
fix small things
authorMatthieuCoder <matthieu@matthieu-dev.xyz>
Wed, 4 Jan 2023 22:55:16 +0000 (02:55 +0400)
committerMatthieuCoder <matthieu@matthieu-dev.xyz>
Wed, 4 Jan 2023 22:55:16 +0000 (02:55 +0400)
src/index.ts
src/sys/events/client.ts
src/sys/events/transport.ts

index 2a9ea9eca6a0a0540aa5b3fac7205ee6472cb7df..26ffdf5203a4c99db3d870506d515ac6a02b5115 100644 (file)
@@ -28,8 +28,17 @@ emitter.on('messageCreate', async (message) => {
                await message.client.channels.createMessage(message.channel_id, {
                        content: `Bonjour! <@${message.author.id}>`,
                });
+       } else if (message.content === '~pid') {
+
+               await message.client.channels.createMessage(message.channel_id, {
+                       content: `Mon pid est ${process.pid}`,
+               });
        }
 });
 
+emitter.onMessageCreate(async (message) => {
+       console.log(message.content);
+});
+
 // We connect ourselves to the nova nats broker.
 (async () => emitter.start())();
index 562ff4761c40caff32bf95695f869c20a5be845d..44de1f95c6c32460987de6162b9e4fdf11c0ae0b 100644 (file)
@@ -109,16 +109,10 @@ export class Client extends undefinedClient {
        }) {
                super();
                this.rest = new REST(options.rest).setToken('_');
-               this.transport = new Transport(this, options.transport);
                this.api = new API(this.rest);
 
-               // This is safe because this event is emitted by the EventEmitter itself.
-               this.on('newListener' as any, async (event: EventName) => {
-                       await this.transport.subscribe(event);
-               });
-
                // Using a proxy to provide the 'on...' functionality
-               return new Proxy(this, {
+               let self = new Proxy(this, {
                        get(self, symbol: keyof typeof Client) {
                                const name = symbol.toString();
                                if (name.startsWith('on') && name.length > 2) {
@@ -130,7 +124,7 @@ export class Client extends undefinedClient {
                                                self.on(eventName, fn);
                                }
 
-                               if (self.api[symbol] && self[symbol as string]) {
+                               if (self.api[symbol] && !self[symbol as string]) {
                                        // eslint-disable-next-line @typescript-eslint/no-unsafe-return
                                        return self.api[symbol];
                                }
@@ -138,6 +132,15 @@ export class Client extends undefinedClient {
                                return self[symbol as string];
                        },
                });
+               
+               this.transport = new Transport(self, options.transport);
+
+               // This is safe because this event is emitted by the EventEmitter itself.
+               this.on('newListener' as any, async (event: EventName) => {
+                       await this.transport.subscribe(event);
+               });
+
+               return self;
        }
 
        public async start() {
index cd2d8c27c8bd17a7f7ab8855ac484a4aae010d5f..c3c9f7a843556f5fd9eb868380a01216642ee30e 100644 (file)
-import {Buffer} from 'node:buffer';
+import { Buffer } from "node:buffer";
 import {
-       connect,
-       type ConnectionOptions,
-       type NatsConnection,
-       type Subscription,
-} from 'nats';
-import globRegex from 'glob-regex';
+  connect,
+  type ConnectionOptions,
+  type NatsConnection,
+  type Subscription,
+} from "nats";
+import globRegex from "glob-regex";
 import {
-       type APIInteractionResponse,
-       InteractionResponseType,
-       type APIInteractionResponseCallbackData,
-       type GatewayDispatchPayload,
-       Routes,
-} from 'discord-api-types/v10';
-import {type CamelCase} from 'type-fest';
-import {type Client, type EventName, type EventsHandlerArguments} from '.';
+  type APIInteractionResponse,
+  InteractionResponseType,
+  type APIInteractionResponseCallbackData,
+  type GatewayDispatchPayload,
+  Routes,
+} from "discord-api-types/v10";
+import { type CamelCase } from "type-fest";
+import { type Client, type EventName, type EventsHandlerArguments } from ".";
 
 /**
  * Options for the nats transport layer
  */
 export type TransportOptions = {
-       additionalEvents?: Array<keyof EventsHandlerArguments>;
-       nats?: ConnectionOptions;
-       queue: string;
+  additionalEvents?: Array<keyof EventsHandlerArguments>;
+  nats?: ConnectionOptions;
+  queue: string;
 };
 
 /**
  * Transport implements all the communication to Nova using Nats
  */
 export class Transport {
-       // Nats connection
-       private nats: NatsConnection | undefined = null;
-       // Current subscriptions
-       private readonly subscriptions = new Map<string, Subscription>();
-       // Current subscribed events
-       private readonly events = new Set<EventName>();
-
-       // Creats a new Transport instance.
-       constructor(
-               private readonly emitter: Client,
-               private readonly config: Partial<TransportOptions>,
-       ) {}
-
-       /**
-        * Starts a new nats client.
-        */
-       public async start() {
-               this.nats = await connect(this.config?.nats);
-
-               await Promise.all(
-                       [...this.events].map(async (eventName) => this.subscribe(eventName)),
-               );
-
-               if (this.config.additionalEvents) {
-                       await Promise.all(
-                               this.config.additionalEvents.map(async (eventName) =>
-                                       this.subscribe(eventName),
-                               ),
-                       );
-               }
-       }
-
-       /**
-        * Subscribe to a new topic
-        * @param event Event to subscribe to
-        * @returns
-        */
-       public async subscribe(event: EventName) {
-               // If nats is not connected, we simply request to subscribe to it at startup
-               if (!this.nats) {
-                       console.log('Requesting event ' + event);
-                       this.events.add(event);
-                       return;
-               }
-
-               // Since the event names used by this library are camelCase'd we need to
-               // re-transform it to the UPPER_CASE used by nova.
-               const dashed = event.replace(/[A-Z]/g, (m) => '_' + m.toLowerCase());
-               // Construct the topic name used by nova.
-               // This **is going to change** as we implement the caching component.
-               const topic = `nova.cache.dispatch.${dashed.toUpperCase()}`;
-
-               // To avoid having multiple subscriptions covering this event
-               // we check if each of our subscriptions covers this scope.
-               const isAlreadyPresent = [...this.subscriptions.keys()].reduce(
-                       (previous, current) => {
-                               if (previous) {
-                                       return previous;
-                               }
-
-                               const regex = globRegex(current);
-
-                               return regex.test(topic);
-                       },
-                       false,
-               );
-
-               // We abord the subscriptions if it's already covered.
-               if (isAlreadyPresent) {
-                       console.warn('nats subscription already covered.');
-                       return;
-               }
-
-               // We remove all the subscriptions that are covered by out current subsciptions.
-               const regex = globRegex(topic);
-               for (const key of this.subscriptions.keys()) {
-                       if (regex.test(key)) {
-                               const subsciption = this.subscriptions.get(key);
-                               if (!subsciption) {
-                                       continue;
-                               }
-
-                               console.log(`unsubscribing from ${key}`);
-                               subsciption.unsubscribe();
-                       }
-               }
-
-               void this._subscriptionTask(topic);
-       }
-
-       // Task that monitors the subscription
-       // It also listens for a subscription end.
-       private async _subscriptionTask(topic: string) {
-               if (!this.nats) {
-                       throw new Error('nats connection is not started');
-               }
-
-               console.log(`subscribing to ${topic}`);
-               // Create the nats subscription
-               const subscription = this.nats.subscribe(topic, {
-                       queue: this.config.queue,
-               });
-               this.subscriptions.set(topic, subscription);
-               // Handle each event in the subscription stream.
-               for await (const publish of subscription) {
-                       try {
-                               // Decode the payload
-                               // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
-                               const event: GatewayDispatchPayload = JSON.parse(
-                                       Buffer.from(publish.data).toString('utf8'),
-                               );
-                               // Transform the event name to a camclCased name
-                               const camelCasedName = event.t
-                                       .toLowerCase()
-                                       .replace(/_([a-z])/g, (g) => g[1].toUpperCase()) as CamelCase<
-                                       typeof event.t
-                               >;
-
-                               // Since an interaction need a reponse,
-                               // we need to handle the case where nova is not configured
-                               // with a webhook endpoint, hence we need to use a post request
-                               // against webhook execute endpoint with the interaction data.
-                               if (event.t === 'INTERACTION_CREATE') {
-                                       const interaction = event.d;
-                                       const respond = async (respond: APIInteractionResponse) => {
-                                               if (publish.reply) {
-                                                       publish.respond(Buffer.from(JSON.stringify(respond), 'utf8'));
-                                               } else {
-                                                       await this.emitter.rest.post(
-                                                               Routes.interactionCallback(interaction.id, interaction.token),
-                                                               {
-                                                                       body: respond,
-                                                               },
-                                                       );
-                                               }
-                                       };
-
-                                       // Emit the
-                                       this.emitter.emit(
-                                               camelCasedName,
-                                               {...event.d, client: this.emitter},
-                                               respond,
-                                       );
-                               } else {
-                                       // Typescript refuses to infer this, whyyy
-                                       this.emitter.emit(camelCasedName, {
-                                               ...event.d,
-                                               client: this.emitter,
-                                       } as any);
-                               }
-                       } catch {}
-               }
-       }
+  // Nats connection
+  private nats: NatsConnection | undefined = null;
+  // Current subscriptions
+  private readonly subscriptions = new Map<string, Subscription>();
+  // Current subscribed events
+  private readonly events = new Set<EventName>();
+
+  // Creats a new Transport instance.
+  constructor(
+    private readonly emitter: Client,
+    private readonly config: Partial<TransportOptions>
+  ) {}
+
+  /**
+   * Starts a new nats client.
+   */
+  public async start() {
+    this.nats = await connect(this.config?.nats);
+
+    await Promise.all(
+      [...this.events].map(async (eventName) => this.subscribe(eventName))
+    );
+
+    if (this.config.additionalEvents) {
+      await Promise.all(
+        this.config.additionalEvents.map(async (eventName) =>
+          this.subscribe(eventName)
+        )
+      );
+    }
+  }
+
+  /**
+   * Subscribe to a new topic
+   * @param event Event to subscribe to
+   * @returns
+   */
+  public async subscribe(event: EventName) {
+    // If nats is not connected, we simply request to subscribe to it at startup
+    if (!this.nats) {
+      console.log("Requesting event " + event);
+      this.events.add(event);
+      return;
+    }
+
+    // Since the event names used by this library are camelCase'd we need to
+    // re-transform it to the UPPER_CASE used by nova.
+    const dashed = event.replace(/[A-Z]/g, (m) => "_" + m.toLowerCase());
+    // Construct the topic name used by nova.
+    // This **is going to change** as we implement the caching component.
+    const topic = `nova.cache.dispatch.${dashed.toUpperCase()}`;
+
+    // To avoid having multiple subscriptions covering this event
+    // we check if each of our subscriptions covers this scope.
+    const isAlreadyPresent = [...this.subscriptions.keys()].reduce(
+      (previous, current) => {
+        if (previous) {
+          return previous;
+        }
+
+        const regex = globRegex(current);
+
+        return regex.test(topic);
+      },
+      false
+    );
+
+    // We abord the subscriptions if it's already covered.
+    if (isAlreadyPresent) {
+      console.warn("nats subscription already covered.");
+      return;
+    }
+
+    // We remove all the subscriptions that are covered by out current subsciptions.
+    const regex = globRegex(topic);
+    for (const key of this.subscriptions.keys()) {
+      if (regex.test(key)) {
+        const subsciption = this.subscriptions.get(key);
+        if (!subsciption) {
+          continue;
+        }
+
+        console.log(`unsubscribing from ${key}`);
+        subsciption.unsubscribe();
+      }
+    }
+
+    void this._subscriptionTask(topic);
+  }
+
+  // Task that monitors the subscription
+  // It also listens for a subscription end.
+  private async _subscriptionTask(topic: string) {
+    if (!this.nats) {
+      throw new Error("nats connection is not started");
+    }
+
+    console.log(`subscribing to ${topic}`);
+    // Create the nats subscription
+    const subscription = this.nats.subscribe(topic, {
+      queue: this.config.queue || "nova_consumer",
+    });
+    this.subscriptions.set(topic, subscription);
+    // Handle each event in the subscription stream.
+    for await (const publish of subscription) {
+      try {
+        // Decode the payload
+        // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
+        const event: GatewayDispatchPayload = JSON.parse(
+          Buffer.from(publish.data).toString("utf8")
+        );
+        // Transform the event name to a camclCased name
+        const camelCasedName = event.t
+          .toLowerCase()
+          .replace(/_([a-z])/g, (g) => g[1].toUpperCase()) as CamelCase<
+          typeof event.t
+        >;
+
+        // Since an interaction need a reponse,
+        // we need to handle the case where nova is not configured
+        // with a webhook endpoint, hence we need to use a post request
+        // against webhook execute endpoint with the interaction data.
+        if (event.t === "INTERACTION_CREATE") {
+          const interaction = event.d;
+          const respond = async (respond: APIInteractionResponse) => {
+            if (publish.reply) {
+              publish.respond(Buffer.from(JSON.stringify(respond), "utf8"));
+            } else {
+              await this.emitter.rest.post(
+                Routes.interactionCallback(interaction.id, interaction.token),
+                {
+                  body: respond,
+                }
+              );
+            }
+          };
+
+          // Emit the
+          this.emitter.emit(
+            camelCasedName,
+            { ...event.d, client: this.emitter },
+            respond
+          );
+        } else {
+          // Typescript refuses to infer this, whyyy
+          this.emitter.emit(camelCasedName, {
+            ...event.d,
+            client: this.emitter,
+          } as any);
+        }
+      } catch {}
+    }
+  }
 }