bb-fca 2.0.9 → 2.0.10

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.
Files changed (81) hide show
  1. package/dist/deltas/apis/messaging/editMessage.js +13 -24
  2. package/dist/deltas/apis/messaging/editMessage.js.map +1 -1
  3. package/dist/deltas/apis/messaging/emoji.js +34 -88
  4. package/dist/deltas/apis/messaging/emoji.js.map +1 -1
  5. package/dist/deltas/apis/messaging/gcmember.js +48 -101
  6. package/dist/deltas/apis/messaging/gcmember.js.map +1 -1
  7. package/dist/deltas/apis/messaging/gcname.js +28 -79
  8. package/dist/deltas/apis/messaging/gcname.js.map +1 -1
  9. package/dist/deltas/apis/messaging/gcrule.js +47 -105
  10. package/dist/deltas/apis/messaging/gcrule.js.map +1 -1
  11. package/dist/deltas/apis/messaging/markAsDelivered.js +10 -12
  12. package/dist/deltas/apis/messaging/markAsDelivered.js.map +1 -1
  13. package/dist/deltas/apis/messaging/markAsRead.js +25 -60
  14. package/dist/deltas/apis/messaging/markAsRead.js.map +1 -1
  15. package/dist/deltas/apis/messaging/markAsReadAll.js +9 -11
  16. package/dist/deltas/apis/messaging/markAsReadAll.js.map +1 -1
  17. package/dist/deltas/apis/messaging/markAsSeen.js +10 -37
  18. package/dist/deltas/apis/messaging/markAsSeen.js.map +1 -1
  19. package/dist/deltas/apis/messaging/nickname.js +38 -98
  20. package/dist/deltas/apis/messaging/nickname.js.map +1 -1
  21. package/dist/deltas/apis/messaging/notes.js +52 -90
  22. package/dist/deltas/apis/messaging/notes.js.map +1 -1
  23. package/dist/deltas/apis/messaging/resolvePhotoUrl.js +17 -42
  24. package/dist/deltas/apis/messaging/resolvePhotoUrl.js.map +1 -1
  25. package/dist/deltas/apis/messaging/sendMessage.js +14 -15
  26. package/dist/deltas/apis/messaging/sendMessage.js.map +1 -1
  27. package/dist/deltas/apis/messaging/sendTypingIndicator.js +7 -13
  28. package/dist/deltas/apis/messaging/sendTypingIndicator.js.map +1 -1
  29. package/dist/deltas/apis/messaging/setMessageReaction.js +24 -14
  30. package/dist/deltas/apis/messaging/setMessageReaction.js.map +1 -1
  31. package/dist/deltas/apis/messaging/shareContact.js +14 -12
  32. package/dist/deltas/apis/messaging/shareContact.js.map +1 -1
  33. package/dist/deltas/apis/messaging/stickers.js +4 -5
  34. package/dist/deltas/apis/messaging/stickers.js.map +1 -1
  35. package/dist/deltas/apis/messaging/theme.js +142 -213
  36. package/dist/deltas/apis/messaging/theme.js.map +1 -1
  37. package/dist/deltas/apis/messaging/unsendMessage.js +14 -7
  38. package/dist/deltas/apis/messaging/unsendMessage.js.map +1 -1
  39. package/dist/deltas/apis/posting/group.js +33 -11
  40. package/dist/deltas/apis/posting/group.js.map +1 -1
  41. package/dist/types/deltas/apis/messaging/editMessage.d.ts +4 -5
  42. package/dist/types/deltas/apis/messaging/emoji.d.ts +7 -1
  43. package/dist/types/deltas/apis/messaging/gcmember.d.ts +7 -1
  44. package/dist/types/deltas/apis/messaging/gcname.d.ts +9 -1
  45. package/dist/types/deltas/apis/messaging/gcrule.d.ts +7 -1
  46. package/dist/types/deltas/apis/messaging/markAsDelivered.d.ts +1 -2
  47. package/dist/types/deltas/apis/messaging/markAsRead.d.ts +1 -1
  48. package/dist/types/deltas/apis/messaging/markAsReadAll.d.ts +1 -2
  49. package/dist/types/deltas/apis/messaging/markAsSeen.d.ts +1 -1
  50. package/dist/types/deltas/apis/messaging/nickname.d.ts +7 -1
  51. package/dist/types/deltas/apis/messaging/notes.d.ts +13 -11
  52. package/dist/types/deltas/apis/messaging/resolvePhotoUrl.d.ts +4 -6
  53. package/dist/types/deltas/apis/messaging/sendMessage.d.ts +1 -1
  54. package/dist/types/deltas/apis/messaging/sendTypingIndicator.d.ts +1 -1
  55. package/dist/types/deltas/apis/messaging/setMessageReaction.d.ts +6 -1
  56. package/dist/types/deltas/apis/messaging/shareContact.d.ts +1 -2
  57. package/dist/types/deltas/apis/messaging/stickers.d.ts +1 -1
  58. package/dist/types/deltas/apis/messaging/theme.d.ts +8 -1
  59. package/dist/types/deltas/apis/messaging/unsendMessage.d.ts +6 -1
  60. package/package.json +1 -1
  61. package/src/deltas/apis/messaging/editMessage.ts +16 -26
  62. package/src/deltas/apis/messaging/emoji.ts +45 -97
  63. package/src/deltas/apis/messaging/gcmember.ts +68 -113
  64. package/src/deltas/apis/messaging/gcname.ts +42 -91
  65. package/src/deltas/apis/messaging/gcrule.ts +61 -111
  66. package/src/deltas/apis/messaging/markAsDelivered.ts +19 -14
  67. package/src/deltas/apis/messaging/markAsRead.ts +45 -72
  68. package/src/deltas/apis/messaging/markAsReadAll.ts +17 -17
  69. package/src/deltas/apis/messaging/markAsSeen.ts +17 -41
  70. package/src/deltas/apis/messaging/nickname.ts +50 -116
  71. package/src/deltas/apis/messaging/notes.ts +59 -95
  72. package/src/deltas/apis/messaging/resolvePhotoUrl.ts +27 -50
  73. package/src/deltas/apis/messaging/sendMessage.ts +28 -26
  74. package/src/deltas/apis/messaging/sendTypingIndicator.ts +13 -12
  75. package/src/deltas/apis/messaging/setMessageReaction.ts +45 -20
  76. package/src/deltas/apis/messaging/shareContact.ts +25 -15
  77. package/src/deltas/apis/messaging/stickers.ts +4 -4
  78. package/src/deltas/apis/messaging/theme.ts +172 -259
  79. package/src/deltas/apis/messaging/unsendMessage.ts +23 -7
  80. package/src/deltas/apis/posting/group.ts +36 -11
  81. package/request.txt +0 -60
@@ -1 +1,9 @@
1
- export default function (defaultFuncs: any, api: any, ctx: any): (newName: any, threadID: any, callback: any, initiatorID: any) => any;
1
+ /**
2
+ * Made by Choru Official
3
+ * Sets the name of a group chat thread via MQTT.
4
+ *
5
+ * @param {Object} defaultFuncs
6
+ * @param {Object} api
7
+ * @param {Object} ctx
8
+ */
9
+ export default function gcname(defaultFuncs: any, api: any, ctx: any): Function;
@@ -1 +1,7 @@
1
- export default function (defaultFuncs: any, api: any, ctx: any): (action: any, userID: any, threadID: any, callback: any) => Promise<any>;
1
+ /**
2
+ * @module gcrule
3
+ * @param {Object} defaultFuncs
4
+ * @param {Object} api
5
+ * @param {Object} ctx
6
+ */
7
+ export default function gcrule(defaultFuncs: any, api: any, ctx: any): Function;
@@ -2,6 +2,5 @@
2
2
  * @param {Object} defaultFuncs
3
3
  * @param {Object} api
4
4
  * @param {Object} ctx
5
- * @returns {function(threadID: string, messageID: string): Promise<void>}
6
5
  */
7
- export default function (defaultFuncs: any, api: any, ctx: any): (threadID: any, messageID: any) => Promise<void>;
6
+ export default function markAsDelivered(defaultFuncs: any, api: any, ctx: any): Function;
@@ -3,4 +3,4 @@
3
3
  * @param {Object} api
4
4
  * @param {Object} ctx
5
5
  */
6
- export default function (defaultFuncs: any, api: any, ctx: any): (threadID: any, read: any, callback: any) => Promise<any>;
6
+ export default function markAsRead(defaultFuncs: any, api: any, ctx: any): Function;
@@ -2,6 +2,5 @@
2
2
  * @param {Object} defaultFuncs
3
3
  * @param {Object} api
4
4
  * @param {Object} ctx
5
- * @returns {function(): Promise<void>}
6
5
  */
7
- export default function (defaultFuncs: any, api: any, ctx: any): () => Promise<void>;
6
+ export default function markAsReadAll(defaultFuncs: any, api: any, ctx: any): Function;
@@ -3,4 +3,4 @@
3
3
  * @param {Object} api
4
4
  * @param {Object} ctx
5
5
  */
6
- export default function (defaultFuncs: any, api: any, ctx: any): (seen_timestamp: any, callback: any) => Promise<any>;
6
+ export default function markAsSeen(defaultFuncs: any, api: any, ctx: any): Function;
@@ -1 +1,7 @@
1
- export default function (defaultFuncs: any, api: any, ctx: any): (nickname: any, threadID: any, participantID: any, callback: any, initiatorID: any) => any;
1
+ /**
2
+ * @module nickname
3
+ * @param {Object} defaultFuncs
4
+ * @param {Object} api
5
+ * @param {Object} ctx
6
+ */
7
+ export default function nickname(defaultFuncs: any, api: any, ctx: any): Function;
@@ -1,14 +1,16 @@
1
1
  /**
2
- * @ChoruOfficial
3
- * @description A module for interacting with Facebook Messenger Notes. This is not for creating notes on a user's profile page, but rather the temporary status-like notes in Messenger.
4
- * @param {Object} defaultFuncs The default functions provided by the API wrapper.
5
- * @param {Object} api The full API object.
6
- * @param {Object} ctx The context object containing the user's session state (e.g., userID, jar).
7
- * @returns {Object} An object containing methods to create, delete, recreate, and check notes.
2
+ * @module notes
3
+ * @description Interacts with Facebook Messenger Notes (temporary status-like notes).
4
+ * @param {Object} defaultFuncs
5
+ * @param {Object} api
6
+ * @param {Object} ctx
8
7
  */
9
- export default function (defaultFuncs: any, api: any, ctx: any): {
10
- create: (text: any, privacy?: string, callback?: any) => void;
11
- delete: (noteID: any, callback: any) => void;
12
- recreate: (oldNoteID: any, newText: any, callback: any) => void;
13
- check: (callback: any) => void;
8
+ export default function notes(defaultFuncs: any, api: any, ctx: any): {
9
+ create: (text: string, privacy?: string) => Promise<any>;
10
+ delete: (noteID: string) => Promise<any>;
11
+ recreate: (oldNoteID: string, newText: string) => Promise<{
12
+ deleted: any;
13
+ created: any;
14
+ }>;
15
+ check: () => Promise<any>;
14
16
  };
@@ -1,9 +1,7 @@
1
1
  /**
2
2
  * @module resolvePhotoUrl
3
- * @description Fetches the direct URL of a Facebook photo using its photo ID.
4
- * @param {Object} defaultFuncs - An object containing default request functions.
5
- * @param {Object} api - Facebook API object (unused here but kept for compatibility).
6
- * @param {Object} ctx - Context object containing cookies (jar) and other session info.
7
- * @returns {Function} resolvePhotoUrl - A function that takes a photo ID and optional callback, and returns a Promise resolving to the photo URL.
3
+ * @param {Object} defaultFuncs
4
+ * @param {Object} api
5
+ * @param {Object} ctx
8
6
  */
9
- export default function (defaultFuncs: any, api: any, ctx: any): (photoID: any, callback: any) => Promise<any>;
7
+ export default function resolvePhotoUrl(defaultFuncs: any, api: any, ctx: any): Function;
@@ -1 +1 @@
1
- export default function (defaultFuncs: any, api: any, ctx: any): (msg: any, threadID: any, replyToMessage: any, isSingleUser?: boolean) => Promise<any>;
1
+ export default function sendMessage(defaultFuncs: any, api: any, ctx: any): Function;
@@ -3,4 +3,4 @@
3
3
  * @param {Object} api
4
4
  * @param {Object} ctx
5
5
  */
6
- export default function (defaultFuncs: any, api: any, ctx: any): (sendTyping: any, threadID: any, callback: any) => Promise<void>;
6
+ export default function sendTypingIndicator(defaultFuncs: any, api: any, ctx: any): Function;
@@ -1 +1,6 @@
1
- export default function (defaultFuncs: any, api: any, ctx: any): (reaction: any, messageID: any) => Promise<void>;
1
+ /**
2
+ * @param {Object} defaultFuncs
3
+ * @param {Object} api
4
+ * @param {Object} ctx
5
+ */
6
+ export default function setMessageReaction(defaultFuncs: any, api: any, ctx: any): Function;
@@ -3,6 +3,5 @@
3
3
  * @param {Object} defaultFuncs - The default functions provided by the API.
4
4
  * @param {Object} api - The full API object.
5
5
  * @param {Object} ctx - The context object.
6
- * @returns {function(text: string, senderID: string, threadID: string, callback: Function): void} - A function to share a contact.
7
6
  */
8
- export default function (defaultFuncs: any, api: any, ctx: any): (text: any, senderID: any, threadID: any, callback: any) => void;
7
+ export default function shareContact(defaultFuncs: any, api: any, ctx: any): Function;
@@ -3,7 +3,7 @@
3
3
  * Provides access to Facebook's GraphQL-based sticker endpoints.
4
4
  * Made by @ChoruOfficial
5
5
  */
6
- export default function (defaultFuncs: any, api: any, ctx: any): {
6
+ export default function stickers(defaultFuncs: any, api: any, ctx: any): {
7
7
  /**
8
8
  * Search for stickers by keyword
9
9
  * @param {string} query - Search term
@@ -1 +1,8 @@
1
- export default function (defaultFuncs: any, api: any, ctx: any): (themeName: any, threadID: any, callback: any, initiatorID: any) => Promise<any>;
1
+ /**
2
+ * @module theme
3
+ * @description Manages or sets the custom theme for a Facebook thread. Made by Choru Official.
4
+ * @param {Object} defaultFuncs
5
+ * @param {Object} api
6
+ * @param {Object} ctx
7
+ */
8
+ export default function theme(defaultFuncs: any, api: any, ctx: any): Function;
@@ -1 +1,6 @@
1
- export default function (defaultFuncs: any, api: any, ctx: any): (messageID: any) => Promise<any>;
1
+ /**
2
+ * @param {Object} defaultFuncs
3
+ * @param {Object} api
4
+ * @param {Object} ctx
5
+ */
6
+ export default function unsendMessage(defaultFuncs: any, api: any, ctx: any): Function;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bb-fca",
3
- "version": "2.0.9",
3
+ "version": "2.0.10",
4
4
  "description": "BB-FCA is a powerful and user-friendly Facebook Chat API wrapper for Node.js, designed to simplify the process of creating chatbots and automating interactions on Facebook Messenger. With BB-FCA, developers can easily send messages, manage conversations, and interact with the Facebook Messenger platform using a simple and intuitive API.",
5
5
  "main": "dist/core/client.js",
6
6
  "types": "dist/index.d.ts",
@@ -2,27 +2,25 @@
2
2
  * @author RFS-ADRENO
3
3
  * @rewrittenBy Isai Ivanov
4
4
  */
5
- //fixed march 30
6
5
  import utils = require('../../../utils');
7
6
 
8
- function canBeCalled(func) {
9
- try {
10
- Reflect.apply(func, null, []);
11
- return true;
12
- } catch (error) {
13
- return false;
14
- }
15
- }
16
-
17
7
  /**
18
- * A function for editing bot's messages.
19
- * @param {string} text - The text with which the bot will edit its messages.
20
- * @param {string} messageID - The message ID of the message the bot will edit.
21
- * @param {Object} callback - Callback for the function.
8
+ * @param {Object} defaultFuncs
9
+ * @param {Object} api
10
+ * @param {Object} ctx
22
11
  */
23
-
24
- export default function(defaultFuncs: any, api: any, ctx: any) {
25
- return function editMessage(text, messageID, callback) {
12
+ export default function editMessage(
13
+ defaultFuncs: any,
14
+ api: any,
15
+ ctx: any,
16
+ ): Function {
17
+ /**
18
+ * Edits a message previously sent by the bot via MQTT.
19
+ * @param {string} text - The new text for the message.
20
+ * @param {string} messageID - The ID of the message to edit.
21
+ * @returns {void}
22
+ */
23
+ return function editMessage(text: string, messageID: string): void {
26
24
  if (!ctx.mqttClient) {
27
25
  throw new Error('Not connected to MQTT');
28
26
  }
@@ -30,10 +28,7 @@ export default function(defaultFuncs: any, api: any, ctx: any) {
30
28
  ctx.wsReqNumber += 1;
31
29
  ctx.wsTaskNumber += 1;
32
30
 
33
- const queryPayload = {
34
- message_id: messageID,
35
- text,
36
- };
31
+ const queryPayload = { message_id: messageID, text };
37
32
 
38
33
  const query = {
39
34
  failure_count: null,
@@ -54,13 +49,8 @@ export default function(defaultFuncs: any, api: any, ctx: any) {
54
49
  request_id: ctx.wsReqNumber,
55
50
  type: 3,
56
51
  };
57
-
58
52
  context.payload = JSON.stringify(context.payload);
59
53
 
60
- // if (canBeCalled(callback)) {
61
- // ctx.reqCallbacks[ctx.wsReqNumber] = callback;
62
- // }
63
-
64
54
  ctx.mqttClient.publish('/ls_req', JSON.stringify(context), {
65
55
  qos: 1,
66
56
  retain: false,
@@ -1,88 +1,40 @@
1
1
  import utils = require('../../../utils');
2
2
 
3
- export default function(defaultFuncs: any, api: any, ctx: any) {
3
+ /**
4
+ * @module emoji
5
+ * @param {Object} defaultFuncs
6
+ * @param {Object} api
7
+ * @param {Object} ctx
8
+ */
9
+ export default function emoji(
10
+ defaultFuncs: any,
11
+ api: any,
12
+ ctx: any,
13
+ ): Function {
4
14
  /**
5
- * Made by Choru Official
6
- * Mqtt
7
- * Sets the custom emoji for a specific Facebook thread via MQTT.
8
- *
9
- * @param {string} emoji The emoji character to set as the custom emoji (e.g., "👍", "❤️").
10
- * @param {string} threadID The ID of the thread where the emoji will be set.
11
- * @param {Function} [callback] Optional callback function to be invoked upon completion.
12
- * @param {string} [initiatorID] The ID of the user who initiated the emoji change (e.g., from event.senderID).
13
- * @returns {Promise<object>} A promise that resolves with a structured event object on success or rejects on error.
15
+ * Made by Choru Official — sets the custom emoji for a thread via MQTT.
16
+ * @param {string} emoji - The emoji character to set (e.g., "👍", "❤️").
17
+ * @param {string} threadID - The ID of the thread.
18
+ * @param {string} [initiatorID] - The ID of the initiating user.
19
+ * @returns {Promise<object>}
14
20
  */
15
- return function emoji(emoji, threadID, callback, initiatorID) {
16
- let _callback;
17
- let _initiatorID;
21
+ return async function emoji(
22
+ emojiChar: string,
23
+ threadID: string,
24
+ initiatorID?: string,
25
+ ): Promise<any> {
26
+ const _initiatorID = initiatorID || ctx.userID;
18
27
 
19
- let _resolvePromise;
20
- let _rejectPromise;
21
- const returnPromise = new Promise<any>((resolve, reject) => {
22
- _resolvePromise = resolve;
23
- _rejectPromise = reject;
24
- });
25
-
26
- if (
27
- utils.getType(callback) === 'Function' ||
28
- utils.getType(callback) === 'AsyncFunction'
29
- ) {
30
- _callback = callback;
31
- _initiatorID = initiatorID;
32
- } else if (
33
- utils.getType(threadID) === 'Function' ||
34
- utils.getType(threadID) === 'AsyncFunction'
35
- ) {
36
- _callback = threadID;
37
- threadID = null;
38
- _initiatorID = callback;
39
- } else if (utils.getType(callback) === 'string') {
40
- _initiatorID = callback;
41
- _callback = undefined;
42
- } else {
43
- _callback = undefined;
44
- _initiatorID = undefined;
45
- }
46
-
47
- if (!_callback) {
48
- _callback = function(__err, __data) {
49
- if (__err) _rejectPromise(__err);
50
- else _resolvePromise(__data);
51
- };
52
- } else {
53
- const originalCallback = _callback;
54
- _callback = function(__err, __data) {
55
- if (__err) {
56
- originalCallback(__err);
57
- _rejectPromise(__err);
58
- } else {
59
- originalCallback(null, __data);
60
- _resolvePromise(__data);
61
- }
62
- };
63
- }
64
-
65
- _initiatorID = _initiatorID || ctx.userID;
66
-
67
- threadID = threadID || ctx.threadID;
68
-
69
- if (!threadID) {
70
- return _callback(new Error('threadID is required to set an emoji.'));
71
- }
72
- if (!emoji) {
73
- return _callback(new Error('An emoji character is required.'));
74
- }
75
-
76
- if (!ctx.mqttClient) {
77
- return _callback(new Error('Not connected to MQTT'));
78
- }
28
+ if (!threadID) throw new Error('threadID is required to set an emoji.');
29
+ if (!emojiChar) throw new Error('An emoji character is required.');
30
+ if (!ctx.mqttClient) throw new Error('Not connected to MQTT');
79
31
 
80
32
  ctx.wsReqNumber += 1;
81
33
  ctx.wsTaskNumber += 1;
82
34
 
83
35
  const queryPayload = {
84
36
  thread_key: threadID.toString(),
85
- custom_emoji: emoji,
37
+ custom_emoji: emojiChar,
86
38
  avatar_sticker_instruction_key_id: null,
87
39
  sync_group: 1,
88
40
  };
@@ -98,7 +50,7 @@ export default function(defaultFuncs: any, api: any, ctx: any) {
98
50
  const context: any = {
99
51
  app_id: ctx.appID,
100
52
  payload: {
101
- epoch_id: parseInt(utils.generateOfflineThreadingID()),
53
+ epoch_id: Number.parseInt(utils.generateOfflineThreadingID()),
102
54
  tasks: [query],
103
55
  version_id: '24631415369801570',
104
56
  },
@@ -107,29 +59,25 @@ export default function(defaultFuncs: any, api: any, ctx: any) {
107
59
  };
108
60
  context.payload = JSON.stringify(context.payload);
109
61
 
110
- ctx.mqttClient.publish(
111
- '/ls_req',
112
- JSON.stringify(context),
113
- { qos: 1, retain: false },
114
- (err) => {
115
- if (err) {
116
- return _callback(
117
- new Error(`MQTT publish failed for emoji: ${err.message || err}`),
118
- );
119
- }
120
-
121
- const emojiChangeEvent = {
122
- type: 'thread_emoji_update',
123
- threadID: threadID,
124
- newEmoji: emoji,
125
- senderID: _initiatorID,
126
- BotID: ctx.userID,
127
- timestamp: Date.now(),
128
- };
129
- _callback(null, emojiChangeEvent);
130
- },
62
+ await new Promise<void>((resolve, reject) =>
63
+ ctx.mqttClient.publish(
64
+ '/ls_req',
65
+ JSON.stringify(context),
66
+ { qos: 1, retain: false },
67
+ (err) =>
68
+ err
69
+ ? reject(new Error(`MQTT publish failed for emoji: ${err.message || err}`))
70
+ : resolve(),
71
+ ),
131
72
  );
132
73
 
133
- return returnPromise;
74
+ return {
75
+ type: 'thread_emoji_update',
76
+ threadID,
77
+ newEmoji: emojiChar,
78
+ senderID: _initiatorID,
79
+ BotID: ctx.userID,
80
+ timestamp: Date.now(),
81
+ };
134
82
  };
135
83
  }
@@ -1,106 +1,71 @@
1
1
  import utils = require('../../../utils');
2
2
 
3
- export default function(defaultFuncs: any, api: any, ctx: any) {
3
+ /**
4
+ * @module gcmember
5
+ * @param {Object} defaultFuncs
6
+ * @param {Object} api
7
+ * @param {Object} ctx
8
+ */
9
+ export default function gcmember(
10
+ defaultFuncs: any,
11
+ api: any,
12
+ ctx: any,
13
+ ): Function {
4
14
  /**
5
- * Made by ChoruOfficial
6
- * Mqtt
7
- * Adds or removes members from a group chat with pre-checking.
8
- *
9
- * @param {"add" | "remove"} action The action to perform.
10
- * @param {string|string[]} userIDs The user ID or array of user IDs.
11
- * @param {string} threadID The ID of the group chat.
12
- * @param {Function} [callback] Optional callback function.
13
- * @returns {Promise<object>} A promise that resolves with information about the action.
15
+ * Made by ChoruOfficial — Adds or removes members from a group chat via MQTT.
16
+ * @param {"add" | "remove"} action - The action to perform.
17
+ * @param {string | string[]} userIDs - The user ID or array of user IDs.
18
+ * @param {string} threadID - The ID of the group chat.
19
+ * @returns {Promise<object>}
14
20
  */
15
- return async function gcmember(action, userIDs, threadID, callback) {
16
- let _callback;
17
- if (typeof threadID === 'function') {
18
- _callback = threadID;
19
- threadID = null;
20
- } else if (typeof callback === 'function') {
21
- _callback = callback;
22
- }
23
-
24
- let resolvePromise, rejectPromise;
25
- const returnPromise = new Promise<any>((resolve, reject) => {
26
- resolvePromise = resolve;
27
- rejectPromise = reject;
28
- });
21
+ return async function gcmember(
22
+ action: 'add' | 'remove',
23
+ userIDs: string | string[],
24
+ threadID: string,
25
+ ): Promise<any> {
26
+ const validActions = ['add', 'remove'];
27
+ const normalizedAction = (action || '').toLowerCase() as 'add' | 'remove';
29
28
 
30
- if (typeof _callback != 'function') {
31
- _callback = (err, data) => {
32
- // Note: We will now rarely use the 'err' parameter for validation errors
33
- if (err) return rejectPromise(err);
34
- resolvePromise(data);
35
- };
29
+ if (!validActions.includes(normalizedAction)) {
30
+ return { type: 'error_gc', error: `Invalid action. Must be one of: ${validActions.join(', ')}` };
31
+ }
32
+ if (!userIDs || (Array.isArray(userIDs) && userIDs.length === 0)) {
33
+ return { type: 'error_gc', error: 'userIDs is required.' };
34
+ }
35
+ if (!threadID) {
36
+ return { type: 'error_gc', error: 'threadID is required.' };
37
+ }
38
+ if (!ctx.mqttClient) {
39
+ return { type: 'error_gc', error: 'Not connected to MQTT' };
36
40
  }
37
41
 
38
42
  try {
39
- const validActions = ['add', 'remove'];
40
- action = action ? action.toLowerCase() : '';
41
-
42
- // --- ERROR CHECKS NOW RETURN AN OBJECT INSTEAD OF THROWING ---
43
- if (!validActions.includes(action)) {
44
- return _callback(null, {
45
- type: 'error_gc',
46
- error: `Invalid action. Must be one of: ${validActions.join(', ')}`,
47
- });
48
- }
49
- if (!userIDs || userIDs.length === 0) {
50
- return _callback(null, {
51
- type: 'error_gc',
52
- error: 'userIDs is required.',
53
- });
54
- }
55
- if (!threadID) {
56
- return _callback(null, {
57
- type: 'error_gc',
58
- error: 'threadID is required.',
59
- });
60
- }
61
- if (!ctx.mqttClient) {
62
- return _callback(null, {
63
- type: 'error_gc',
64
- error: 'Not connected to MQTT',
65
- });
66
- }
67
-
68
43
  const threadInfo = await api.getThreadInfo(threadID);
69
44
  if (!threadInfo) {
70
- return _callback(null, {
71
- type: 'error_gc',
72
- error: 'Could not retrieve thread information.',
73
- });
45
+ return { type: 'error_gc', error: 'Could not retrieve thread information.' };
74
46
  }
75
47
  if (threadInfo.isGroup === false) {
76
- return _callback(null, {
77
- type: 'error_gc',
78
- error: 'This feature is only for group chats, not private messages.',
79
- });
48
+ return { type: 'error_gc', error: 'This feature is only for group chats, not private messages.' };
80
49
  }
81
50
 
82
- const currentMembers = threadInfo.participantIDs;
51
+ const currentMembers: string[] = threadInfo.participantIDs;
83
52
  const usersToModify = Array.isArray(userIDs) ? userIDs : [userIDs];
84
- let queryPayload, query;
85
- let finalUsers = usersToModify;
86
53
 
87
54
  ctx.wsReqNumber = (ctx.wsReqNumber || 0) + 1;
88
55
  ctx.wsTaskNumber = (ctx.wsTaskNumber || 0) + 1;
89
56
 
90
- if (action === 'add') {
91
- const usersToAdd = usersToModify.filter(
92
- (id) => !currentMembers.includes(id),
93
- );
57
+ let query: any;
58
+ let finalUsers: string[];
59
+
60
+ if (normalizedAction === 'add') {
61
+ const usersToAdd = usersToModify.filter((id) => !currentMembers.includes(id));
94
62
  if (usersToAdd.length === 0) {
95
- return _callback(null, {
96
- type: 'error_gc',
97
- error: 'All specified users are already in the group.',
98
- });
63
+ return { type: 'error_gc', error: 'All specified users are already in the group.' };
99
64
  }
100
65
  finalUsers = usersToAdd;
101
- queryPayload = {
102
- thread_key: parseInt(threadID),
103
- contact_ids: finalUsers.map((id) => parseInt(id)),
66
+ const queryPayload = {
67
+ thread_key: Number.parseInt(threadID),
68
+ contact_ids: finalUsers.map((id) => Number.parseInt(id)),
104
69
  sync_group: 1,
105
70
  };
106
71
  query = {
@@ -110,16 +75,12 @@ export default function(defaultFuncs: any, api: any, ctx: any) {
110
75
  task_id: ctx.wsTaskNumber,
111
76
  };
112
77
  } else {
113
- // action is 'remove'
114
78
  const userToRemove = usersToModify[0];
115
79
  if (!currentMembers.includes(userToRemove)) {
116
- return _callback(null, {
117
- type: 'error_gc',
118
- error: `User with ID ${userToRemove} is not in this group chat.`,
119
- });
80
+ return { type: 'error_gc', error: `User with ID ${userToRemove} is not in this group chat.` };
120
81
  }
121
82
  finalUsers = [userToRemove];
122
- queryPayload = {
83
+ const queryPayload = {
123
84
  thread_id: threadID,
124
85
  contact_id: userToRemove,
125
86
  sync_group: 1,
@@ -135,7 +96,7 @@ export default function(defaultFuncs: any, api: any, ctx: any) {
135
96
  const context: any = {
136
97
  app_id: ctx.appID,
137
98
  payload: {
138
- epoch_id: parseInt(utils.generateOfflineThreadingID()),
99
+ epoch_id: Number.parseInt(utils.generateOfflineThreadingID()),
139
100
  tasks: [query],
140
101
  version_id: '24631415369801570',
141
102
  },
@@ -144,32 +105,26 @@ export default function(defaultFuncs: any, api: any, ctx: any) {
144
105
  };
145
106
  context.payload = JSON.stringify(context.payload);
146
107
 
147
- ctx.mqttClient.publish(
148
- '/ls_req',
149
- JSON.stringify(context),
150
- { qos: 1, retain: false },
151
- (err) => {
152
- if (err) return _callback(err); // For network errors, we still reject
153
-
154
- const gcmemberInfo = {
155
- type: 'gc_member_update',
156
- threadID: threadID,
157
- userIDs: finalUsers,
158
- action: action,
159
- senderID: ctx.userID,
160
- BotID: ctx.userID,
161
- timestamp: Date.now(),
162
- };
163
- return _callback(null, gcmemberInfo);
164
- },
108
+ await new Promise<void>((resolve, reject) =>
109
+ ctx.mqttClient.publish(
110
+ '/ls_req',
111
+ JSON.stringify(context),
112
+ { qos: 1, retain: false },
113
+ (err) => (err ? reject(err) : resolve()),
114
+ ),
165
115
  );
166
- } catch (err) {
167
- return _callback(null, {
168
- type: 'error_gc',
169
- error: err.message || 'An unknown error occurred.',
170
- });
171
- }
172
116
 
173
- return returnPromise;
117
+ return {
118
+ type: 'gc_member_update',
119
+ threadID,
120
+ userIDs: finalUsers,
121
+ action: normalizedAction,
122
+ senderID: ctx.userID,
123
+ BotID: ctx.userID,
124
+ timestamp: Date.now(),
125
+ };
126
+ } catch (err: any) {
127
+ return { type: 'error_gc', error: err.message || 'An unknown error occurred.' };
128
+ }
174
129
  };
175
130
  }