@shoru/kitten 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shoru/kitten",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "description": "A powerful Node.js framework to simplify making WhatsApp Bots",
6
6
  "main": "src/index.js",
7
7
  "repository": {
@@ -5,10 +5,7 @@ export const formatter = (wa, event, eventName) => {
5
5
 
6
6
  switch (eventName) {
7
7
  case 'messages.upsert':
8
- return {
9
- ...formatMessage(wa, event),
10
- event: eventName
11
- };
8
+ return formatMessage(wa, event);
12
9
  default: return event;
13
10
  }
14
11
  }
@@ -156,7 +156,7 @@ export class PluginManager {
156
156
  static async #loadFile(filePath, parent, register = true) {
157
157
  const exec = async () => {
158
158
  const { mtimeMs } = await fs.stat(filePath);
159
- const mod = await import(`${pathToFileURL(filePath)}?v=${mtimeMs | 0}`);
159
+ const mod = await import(`${pathToFileURL(filePath)}?v=${Math.trunc(mtimeMs)}`);
160
160
  const loaded = new Map();
161
161
 
162
162
  for (const [name, value] of Object.entries(mod)) {
@@ -207,9 +207,7 @@ export class PluginManager {
207
207
 
208
208
  const prefixes = prefixOpt === false
209
209
  ? null
210
- : prefixOpt
211
- ? new Set([prefixOpt].flat())
212
- : new Set(PREFIXES);
210
+ : new Set([prefixOpt ?? PREFIXES].flat());
213
211
 
214
212
  return {
215
213
  strings,
@@ -312,7 +310,7 @@ export class PluginManager {
312
310
  #createHandler(event) {
313
311
  const bucket = PluginManager.#buckets[event];
314
312
  const sock = this.#sock;
315
- const dispatch = (ctx) => this.#dispatch(sock, ctx, bucket);
313
+ const dispatch = (ctx, event) => this.#dispatch(sock, ctx, bucket, event);
316
314
 
317
315
  const handlers = {
318
316
  'messages.upsert': ({ messages, type }) => {
@@ -329,30 +327,30 @@ export class PluginManager {
329
327
 
330
328
  'messages.update': (updates) => {
331
329
  for (const { key, update } of updates) {
332
- if (key?.remoteJid) dispatch({ event, key, update, jid: key.remoteJid });
330
+ if (key?.remoteJid) dispatch({ key, update, jid: key.remoteJid }, event);
333
331
  }
334
332
  },
335
333
 
336
334
  'messages.reaction': (reactions) => {
337
335
  for (const { key, reaction } of reactions) {
338
- if (key?.remoteJid) dispatch({ event, key, reaction, jid: key.remoteJid, emoji: reaction?.text });
336
+ if (key?.remoteJid) dispatch({ key, reaction, jid: key.remoteJid, emoji: reaction?.text }, event);
339
337
  }
340
338
  },
341
339
 
342
- 'group-participants.update': (u) => dispatch({ event, ...u }),
343
- 'connection.update': (u) => dispatch({ event, ...u }),
344
- 'creds.update': (creds) => dispatch({ event, creds }),
345
- 'call': (calls) => calls.forEach(c => dispatch({ event, ...c })),
340
+ 'group-participants.update': (u) => dispatch(u, event),
341
+ 'connection.update': (u) => dispatch(u, event),
342
+ 'creds.update': (creds) => dispatch({ creds }, event),
343
+ 'call': (calls) => calls.forEach(c => dispatch(c, event)),
346
344
  };
347
345
 
348
- return handlers[event] ?? ((data) => dispatch({ event, data }));
346
+ return handlers[event] ?? ((data) => dispatch({ data }, event));
349
347
  }
350
348
 
351
- #dispatch(sock, ctx, bucket) {
352
- if (this.#destroyed) return;
349
+ #dispatch(sock, ctx, bucket, event) {
350
+ if (this.#destroyed || !ctx) return;
353
351
 
354
352
  for (const [id, plugin] of bucket.auto) {
355
- this.#execute(id, plugin, sock, ctx, null);
353
+ this.#execute(id, plugin, sock, ctx, event);
356
354
  }
357
355
 
358
356
  if (bucket.match.size && ctx.body) {
@@ -361,15 +359,15 @@ export class PluginManager {
361
359
  if (!matchers) continue;
362
360
 
363
361
  const result = PluginManager.#test(matchers, ctx.body);
364
- if (result) this.#execute(id, plugin, sock, ctx, result);
362
+ if (result) this.#execute(id, plugin, sock, ctx, event, result);
365
363
  }
366
364
  }
367
365
  }
368
366
 
369
- async #execute(id, plugin, sock, ctx, match) {
367
+ async #execute(id, plugin, sock, ctx, event, match) {
370
368
  if (this.#destroyed) return;
371
369
  try {
372
- await plugin(sock, match ? { ...ctx, _match: match } : ctx, ctx.event);
370
+ await plugin(sock, match ? { ...ctx, _match: match } : ctx, event);
373
371
  } catch (err) {
374
372
  PluginManager.#handleError(`[Plugin:${id}]`, err);
375
373
  }