rubika 1.2.3 → 1.2.5

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 (78) hide show
  1. package/README.md +1 -1
  2. package/bot/bot.ts +34 -28
  3. package/bot/contexts/inline.ts +15 -14
  4. package/bot/contexts/update.ts +20 -21
  5. package/bot/filters.ts +32 -30
  6. package/bot/index.ts +4 -2
  7. package/bot/methods/advanced/builder.ts +7 -12
  8. package/bot/methods/files/_sendFile.ts +5 -2
  9. package/bot/methods/files/uploadFile.ts +4 -1
  10. package/bot/methods/index.ts +10 -10
  11. package/bot/methods/messages/deleteMessage.ts +6 -10
  12. package/bot/methods/messages/sendContact.ts +5 -3
  13. package/bot/methods/messages/sendFile.ts +1 -1
  14. package/bot/methods/messages/sendGif.ts +23 -23
  15. package/bot/methods/messages/sendImage.ts +23 -23
  16. package/bot/methods/messages/sendLocation.ts +6 -3
  17. package/bot/methods/messages/sendMessage.ts +6 -2
  18. package/bot/methods/messages/sendMusic.ts +23 -23
  19. package/bot/methods/messages/sendPoll.ts +6 -3
  20. package/bot/methods/messages/sendSticker.ts +8 -4
  21. package/bot/methods/messages/sendVideo.ts +23 -23
  22. package/bot/methods/messages/sendVoice.ts +23 -23
  23. package/bot/methods/utilities/getUpdates.ts +1 -1
  24. package/bot/methods/utilities/handleUpdates.ts +14 -14
  25. package/bot/methods/utilities/polling.ts +57 -32
  26. package/bot/methods/utilities/run.ts +2 -2
  27. package/bot/methods/utilities/start.ts +7 -6
  28. package/bot/methods/utilities/webhook.ts +1 -2
  29. package/bot/network.ts +43 -15
  30. package/bot/types/handlers.ts +3 -3
  31. package/bot/types/interfaces.ts +44 -4
  32. package/bot/types/utils.ts +6 -1
  33. package/bot/utils/antiSpam.ts +1 -1
  34. package/client/client.ts +35 -33
  35. package/client/contexts/activities.type.ts +2 -2
  36. package/client/contexts/chat.type.ts +2 -2
  37. package/client/contexts/message.type.ts +10 -3
  38. package/client/contexts/notifications.type.ts +2 -2
  39. package/client/crypto.ts +2 -2
  40. package/client/filters.ts +31 -31
  41. package/client/index.ts +5 -1
  42. package/client/methods/advanced/builder.ts +3 -1
  43. package/client/methods/auth/registerDevice.ts +8 -6
  44. package/client/methods/channels/addChannel.ts +1 -1
  45. package/client/methods/channels/editChannelInfo.ts +3 -3
  46. package/client/methods/channels/setChannelVoiceChatSetting.ts +1 -1
  47. package/client/methods/chats/editJoinLink.ts +1 -1
  48. package/client/methods/chats/seenChats.ts +1 -1
  49. package/client/methods/extras/deleteMessagebyCount.ts +40 -38
  50. package/client/methods/extras/onEditMessages.ts +1 -1
  51. package/client/methods/extras/userIsAdmin.ts +2 -2
  52. package/client/methods/groups/editGroupInfo.ts +13 -1
  53. package/client/methods/groups/setGroupVoiceChatSetting.ts +1 -1
  54. package/client/methods/messages/sendDocument.ts +3 -2
  55. package/client/methods/messages/sendFileInline.ts +1 -1
  56. package/client/methods/messages/sendGif.ts +4 -3
  57. package/client/methods/messages/sendMessage.ts +7 -5
  58. package/client/methods/messages/sendMessageAPICall.ts +18 -0
  59. package/client/methods/messages/sendMusic.ts +3 -2
  60. package/client/methods/messages/sendPhoto.ts +3 -2
  61. package/client/methods/messages/sendText.ts +4 -2
  62. package/client/methods/messages/sendVideo.ts +3 -2
  63. package/client/methods/messages/sendVideoMessage.ts +3 -2
  64. package/client/methods/messages/sendVoice.ts +3 -2
  65. package/client/methods/settings/updateProfile.ts +1 -1
  66. package/client/methods/utilities/start.ts +14 -6
  67. package/client/methods/utilities/thumbnail.ts +5 -5
  68. package/client/network/file.ts +13 -7
  69. package/client/network/websocket.ts +32 -15
  70. package/client/types/client.type.ts +10 -10
  71. package/client/utils/session.ts +1 -1
  72. package/index.ts +1 -0
  73. package/package.json +1 -7
  74. package/utils/formater.ts +1 -1
  75. package/utils/index.ts +1 -1
  76. package/utils/logger.ts +26 -0
  77. package/utils/parser.ts +120 -156
  78. package/utils/errors.ts +0 -26
@@ -8,18 +8,19 @@ async function sendVideo(
8
8
  reply_to_message_id?: string,
9
9
  is_spoil?: boolean,
10
10
  thumb?: string | boolean,
11
- auto_delete?: number
11
+ auto_delete?: number,
12
12
  ) {
13
13
  return await this.sendMessage(
14
14
  object_guid,
15
15
  caption,
16
16
  reply_to_message_id,
17
17
  video,
18
+ undefined,
18
19
  "Video",
19
20
  is_spoil,
20
21
  thumb,
21
22
  undefined,
22
- auto_delete
23
+ auto_delete,
23
24
  );
24
25
  }
25
26
 
@@ -7,18 +7,19 @@ async function sendVideoMessage(
7
7
  reply_to_message_id?: string,
8
8
  is_spoil?: boolean,
9
9
  thumb?: string | boolean,
10
- auto_delete?: number
10
+ auto_delete?: number,
11
11
  ) {
12
12
  return await this.sendMessage(
13
13
  object_guid,
14
14
  null,
15
15
  reply_to_message_id,
16
16
  video,
17
+ undefined,
17
18
  "VideoMessage",
18
19
  is_spoil,
19
20
  thumb,
20
21
  undefined,
21
- auto_delete
22
+ auto_delete,
22
23
  );
23
24
  }
24
25
 
@@ -6,18 +6,19 @@ async function sendVoice(
6
6
  voice: string | Buffer<ArrayBufferLike>,
7
7
  caption?: string,
8
8
  reply_to_message_id?: string,
9
- auto_delete?: number
9
+ auto_delete?: number,
10
10
  ) {
11
11
  return await this.sendMessage(
12
12
  object_guid,
13
13
  caption,
14
14
  reply_to_message_id,
15
15
  voice,
16
+ undefined,
16
17
  "Voice",
17
18
  undefined,
18
19
  undefined,
19
20
  undefined,
20
- auto_delete
21
+ auto_delete,
21
22
  );
22
23
  }
23
24
 
@@ -13,7 +13,7 @@ async function updateProfile(
13
13
  last_name: string | null = null,
14
14
  bio: string | null = null,
15
15
  ) {
16
- let input: InputType = {
16
+ const input: InputType = {
17
17
  updated_parameters: [],
18
18
  };
19
19
 
@@ -16,14 +16,19 @@ async function start(this: Client): Promise<void> {
16
16
  }
17
17
 
18
18
  try {
19
- if (!this.auth) return Error("[start] Error auth is not set");
19
+ if (!this.auth)
20
+ throw this.logger.error("[start] Error auth is not set", "error");
20
21
  this.key = Buffer.from(Crypto.passphrase(this.auth), "utf8");
21
22
  this.decode_auth = Crypto.decode_auth(this.auth);
22
23
  const result = await this.getUserInfo();
24
+ console.log(result);
25
+
23
26
  this.userGuid = result.user.user_guid;
24
27
  this.initialize = true;
25
- } catch (error) {
26
- let phone_number: string = await prompt("Phone Number: ");
28
+ } catch {
29
+ let phone_number: string = await prompt(
30
+ "Phone Number ex -> (989123456789): ",
31
+ );
27
32
  let is_phone_number_true = true;
28
33
 
29
34
  while (is_phone_number_true) {
@@ -31,7 +36,7 @@ async function start(this: Client): Promise<void> {
31
36
  if (answer.toLowerCase() === "y") {
32
37
  is_phone_number_true = false;
33
38
  } else {
34
- phone_number = await prompt("Phone Number ex -> (09123456789) : ");
39
+ phone_number = await prompt("Phone Number ex -> (989123456789): ");
35
40
  }
36
41
  }
37
42
 
@@ -54,7 +59,7 @@ async function start(this: Client): Promise<void> {
54
59
  this.privateKey = privateKey;
55
60
 
56
61
  while (true) {
57
- let phone_code = await prompt("Code: ");
62
+ let phone_code = await prompt(`Code [ ${result.send_type} ]: `);
58
63
 
59
64
  let response = await this.signIn(
60
65
  phone_code,
@@ -77,7 +82,10 @@ async function start(this: Client): Promise<void> {
77
82
  private_key: this.privateKey,
78
83
  });
79
84
 
80
- await this.registerDevice();
85
+ const res = await this.registerDevice();
86
+
87
+ console.log(res);
88
+
81
89
  break;
82
90
  }
83
91
  }
@@ -8,7 +8,7 @@ const ffmpeg = optionalImport<typeof import("fluent-ffmpeg")>("fluent-ffmpeg");
8
8
  class ThumbnailGenerator {
9
9
  static async getTime(videoPath: string): Promise<number> {
10
10
  if (!ffmpeg) {
11
- return new Error(
11
+ throw new Error(
12
12
  "fluent-ffmpeg module is not installed. Some features may be disabled.",
13
13
  );
14
14
  }
@@ -28,7 +28,7 @@ class ThumbnailGenerator {
28
28
 
29
29
  static async fromVideo(videoPath: string): Promise<string> {
30
30
  if (!ffmpeg) {
31
- return new Error(
31
+ throw new Error(
32
32
  "fluent-ffmpeg module is not installed. Some features may be disabled.",
33
33
  );
34
34
  }
@@ -57,7 +57,7 @@ class ThumbnailGenerator {
57
57
  return base64Thumbnail;
58
58
  } catch (error) {
59
59
  console.error("Error generating video thumbnail:", error);
60
- return error;
60
+ throw error;
61
61
  }
62
62
  }
63
63
 
@@ -66,7 +66,7 @@ class ThumbnailGenerator {
66
66
  width: number = 320,
67
67
  ): Promise<string> {
68
68
  if (!sharp) {
69
- return new Error(
69
+ throw new Error(
70
70
  "sharp module is not installed. Some features may be disabled.",
71
71
  );
72
72
  }
@@ -76,7 +76,7 @@ class ThumbnailGenerator {
76
76
  return buffer.toString("base64");
77
77
  } catch (error) {
78
78
  console.error("Error generating image thumbnail:", error);
79
- return error;
79
+ throw error;
80
80
  }
81
81
  }
82
82
  }
@@ -8,7 +8,10 @@ export async function uploadFile(
8
8
  chunkSize: number = 1048576,
9
9
  ): Promise<any> {
10
10
  if (!fs.existsSync(filePath))
11
- return new Error("File not found in the given path");
11
+ throw network.client.logger.error(
12
+ "File not found in the given path",
13
+ "error",
14
+ );
12
15
 
13
16
  const stat = await fs.promises.stat(filePath);
14
17
  const fileSize = stat.size;
@@ -21,7 +24,7 @@ export async function uploadFile(
21
24
  let dc_id: string = result.dc_id;
22
25
  let upload_url: string = result.upload_url;
23
26
  let access_hash_send: string = result.access_hash_send;
24
- let totalParts: number = Math.ceil(fileSize / chunkSize);
27
+ const totalParts: number = Math.ceil(fileSize / chunkSize);
25
28
 
26
29
  const stream = fs.createReadStream(filePath, { highWaterMark: chunkSize });
27
30
 
@@ -76,12 +79,12 @@ export async function uploadFile(
76
79
  };
77
80
  }
78
81
  } catch (error) {
79
- console.error("Upload error:", error);
82
+ network.client.logger.error("Upload error:" + error, "error");
80
83
  await new Promise((resolve) => setTimeout(resolve, 5000));
81
84
  }
82
85
  }
83
86
 
84
- return new Error("Upload failed completely.");
87
+ throw network.client.logger.error("Upload failed completely.", "error");
85
88
  }
86
89
 
87
90
  export async function download(
@@ -99,7 +102,7 @@ export async function download(
99
102
  "user-agent": network.userAgent,
100
103
  };
101
104
 
102
- const base_url = `https://messenger${dc_id}.iranlms.ir`;
105
+ const base_url = `https://${network.client.application === "Rubika" ? "messenger" : "shstorage" + dc_id}.iranlms.ir`;
103
106
 
104
107
  const fetchChunk = async (
105
108
  start_index: number,
@@ -117,12 +120,15 @@ export async function download(
117
120
  headers,
118
121
  });
119
122
 
120
- if (res.status !== 200) return Buffer.alloc(0);
123
+ if (res.status !== 200) return await fetchChunk(start_index, last_index);
121
124
 
122
125
  const arrayBuffer = await res.arrayBuffer();
123
126
  return Buffer.from(arrayBuffer);
124
127
  } catch (err) {
125
- console.error(`[fetchChunk] Failed at ${start_index}-${last_index}`, err);
128
+ network.client.logger.error(
129
+ `[fetchChunk] Failed at ${start_index}-${last_index}` + err,
130
+ "error",
131
+ );
126
132
  return Buffer.alloc(0);
127
133
  }
128
134
  };
@@ -27,7 +27,7 @@ export async function setupWebSocket(network: Network) {
27
27
 
28
28
  network.ws.addEventListener("error", async () => {
29
29
  if (!network.reconnecting) {
30
- console.error("WebSocket error, reconnecting...");
30
+ network.client.logger.error("WebSocket error, reconnecting...", "error");
31
31
  network.reconnecting = true;
32
32
  await resetConnection(network);
33
33
  }
@@ -35,7 +35,7 @@ export async function setupWebSocket(network: Network) {
35
35
 
36
36
  network.ws.addEventListener("close", async () => {
37
37
  if (!network.reconnecting) {
38
- console.warn("WebSocket closed, reconnecting...");
38
+ network.client.logger.error("WebSocket closed, reconnecting...", "warn");
39
39
  network.reconnecting = true;
40
40
  await resetConnection(network);
41
41
  }
@@ -64,14 +64,20 @@ export async function openSocket(network: Network) {
64
64
  network.ws.send(JSON.stringify({}));
65
65
  }
66
66
  } catch (err) {
67
- console.error("Error sending heartbeat", err);
67
+ network.client.logger.error("Error sending heartbeat" + err, "error");
68
68
  }
69
69
  }, 30000);
70
70
  } else {
71
- console.warn("WebSocket is not open; cannot send handshake");
71
+ network.client.logger.error(
72
+ "WebSocket is not open; cannot send handshake",
73
+ "warn",
74
+ );
72
75
  }
73
76
  } catch (err) {
74
- console.error("Error during openSocket execution", err);
77
+ network.client.logger.error(
78
+ "Error during openSocket execution" + err,
79
+ "error",
80
+ );
75
81
  }
76
82
  }
77
83
 
@@ -83,7 +89,7 @@ async function resetConnection(network: Network) {
83
89
  try {
84
90
  await network.getUpdates();
85
91
  } catch (e) {
86
- console.error("Failed to reconnect:", e);
92
+ network.client.logger.error("Failed to reconnect:" + e, "error");
87
93
  } finally {
88
94
  network.reconnecting = false;
89
95
  }
@@ -95,11 +101,15 @@ function resetInactivityTimer(network: Network) {
95
101
 
96
102
  network.inactivityTimeout = setTimeout(
97
103
  () => {
98
- console.warn(
104
+ network.client.logger.error(
99
105
  "No updates received for 10 minutes. Reconnecting WebSocket...",
106
+ "warn",
100
107
  );
101
108
  void resetConnection(network).catch((err) => {
102
- console.error("Error during inactivity reset:", err);
109
+ network.client.logger.error(
110
+ "Error during inactivity reset:" + err,
111
+ "error",
112
+ );
103
113
  });
104
114
  },
105
115
  10 * 60 * 1000,
@@ -139,20 +149,27 @@ async function getMessage(message: string, network: Network) {
139
149
  await Promise.all(tasks);
140
150
  }
141
151
  } catch (err) {
142
- console.error("[getMessage] Failed to decrypt or process message:", err);
152
+ network.client.logger.error(
153
+ "[getMessage] Failed to decrypt or process message:" + err,
154
+ "error",
155
+ );
143
156
  }
144
157
  }
145
158
 
146
- async function handleCategory<T extends keyof ContextMapCon>(
159
+ async function handleCategory<T, K extends keyof ContextMapCon<T>>(
147
160
  type: T,
148
- handlers: Handler<ContextMapCon[T]>[],
161
+ handlers: Handler<ContextMapCon<T>[K]>[],
149
162
  updates: any[],
150
163
  network: Network,
151
164
  author_title: string,
152
165
  ) {
153
- const CtxClass = ContextConstructors[type];
166
+ // @ts-ignore
167
+ const CtxClass = ContextConstructors[type]<T>;
154
168
  if (!CtxClass) {
155
- console.warn(`[handleCategory] No constructor found for type: ${type}`);
169
+ network.client.logger.error(
170
+ `[handleCategory] No constructor found for type: ${type}`,
171
+ "warn",
172
+ );
156
173
  return;
157
174
  }
158
175
 
@@ -161,14 +178,14 @@ async function handleCategory<T extends keyof ContextMapCon>(
161
178
  update.client_guid = network.client.userGuid;
162
179
  update.message.author_title = author_title;
163
180
 
164
- const ctx = new CtxClass(network.client, update) as ContextMapCon[T];
181
+ const ctx = new CtxClass(network.client, update) as ContextMapCon<T>[K];
165
182
 
166
183
  for (const { filters, handler, prefix } of handlers) {
167
184
  const passed = await checkFilters(ctx, filters);
168
185
 
169
186
  if (passed) {
170
187
  if (type === "message" && prefix) {
171
- const text = (ctx as Message).message.text;
188
+ const text = (ctx as Message<T>).message.text;
172
189
  if (!text) continue;
173
190
 
174
191
  if (typeof prefix === "string" && text !== prefix) continue;
@@ -26,22 +26,22 @@ export interface RubPlugin {
26
26
  run: PluginFunction;
27
27
  }
28
28
 
29
- export interface ContextMap {
30
- chat: Chat;
31
- message: Message;
32
- activities: Activities;
33
- notifications: Notifications;
29
+ export interface ContextMap<T> {
30
+ chat: Chat<T>;
31
+ message: Message<T>;
32
+ activities: Activities<T>;
33
+ notifications: Notifications<T>;
34
34
  error: {
35
35
  message: string;
36
36
  client: Client;
37
37
  };
38
38
  }
39
39
 
40
- export interface ContextMapCon {
41
- chat: Chat;
42
- message: Message;
43
- activities: Activities;
44
- notifications: Notifications;
40
+ export interface ContextMapCon<T> {
41
+ chat: Chat<T>;
42
+ message: Message<T>;
43
+ activities: Activities<T>;
44
+ notifications: Notifications<T>;
45
45
  }
46
46
 
47
47
  export type Handler<T> = {
@@ -10,7 +10,7 @@ class SessionManager {
10
10
 
11
11
  this.filename = isStringInput
12
12
  ? `${input}.json`
13
- : `${Date.now()}.rubjs.json`;
13
+ : `${Date.now()}.rubika.json`;
14
14
 
15
15
  if (!isStringInput) {
16
16
  this.sessionData = input;
package/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import Bot from "./bot";
2
2
  import Client from "./client";
3
3
  import { Utils, prompt } from "./utils";
4
+ export * from "./utils";
4
5
 
5
6
  export { Bot, Client, Utils, prompt };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rubika",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "author": "Hadi Rostami",
5
5
  "main": "index.ts",
6
6
  "description": "A modern library for Rubika built with Bun.",
@@ -12,12 +12,6 @@
12
12
  ],
13
13
  "license": "MIT",
14
14
  "type": "module",
15
- "devDependencies": {
16
- "@types/bun": "^1.3.9",
17
- "@types/linkify-it": "^5.0.0",
18
- "@types/node-rsa": "^1.1.4",
19
- "@types/user-agents": "^1.0.4"
20
- },
21
15
  "dependencies": {
22
16
  "linkify-it": "^5.0.0",
23
17
  "node-rsa": "^1.1.1",
package/utils/formater.ts CHANGED
@@ -6,7 +6,7 @@ class Utils {
6
6
  static Spoiler = (text: string): string => `||${text.trim()}||`;
7
7
  static Strike = (text: string): string => `~~${text.trim()}~~`;
8
8
  static Underline = (text: string): string => `--${text.trim()}--`;
9
- static Quote = (text: string): string => `>${text.trim()}>`;
9
+ static Quote = (text: string): string => `^^${text.trim()}^^`;
10
10
  static HyperLink = (text: string, link: string): string =>
11
11
  `[${text.trim()}](${link.trim()})`;
12
12
  }
package/utils/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import checkFilters from "./checkFilter";
2
2
  import Markdown from "./parser";
3
3
  import Utils from "./formater";
4
- import Logger from "./errors";
4
+ import Logger from "./logger";
5
5
  import prompt from "./prompt";
6
6
 
7
7
  export { checkFilters, Logger, Utils, Markdown, prompt };
@@ -0,0 +1,26 @@
1
+ import { ContextMap, Handler } from "../client/types/client.type";
2
+ import checkFilters from "./checkFilter";
3
+
4
+ class Logger<T> {
5
+ constructor(
6
+ private errors: Handler<ContextMap<unknown>["error"]>[],
7
+ private bot: T,
8
+ ) {}
9
+
10
+ async error(text: string, type: "error" | "warn") {
11
+ const error = {
12
+ message: `⟮ ${type} ⟯ ----> ${text}`,
13
+ bot: this.bot,
14
+ };
15
+
16
+ for (const { filters, handler } of this.errors) {
17
+ const passed = await checkFilters(text, filters);
18
+
19
+ if (passed) {
20
+ await handler(error as any);
21
+ }
22
+ }
23
+ }
24
+ }
25
+
26
+ export default Logger;