rubika 1.2.3 → 1.2.4

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 (77) hide show
  1. package/bot/bot.ts +34 -28
  2. package/bot/contexts/inline.ts +15 -14
  3. package/bot/contexts/update.ts +20 -21
  4. package/bot/filters.ts +32 -30
  5. package/bot/index.ts +4 -2
  6. package/bot/methods/advanced/builder.ts +7 -12
  7. package/bot/methods/files/_sendFile.ts +5 -2
  8. package/bot/methods/files/uploadFile.ts +4 -1
  9. package/bot/methods/index.ts +10 -10
  10. package/bot/methods/messages/deleteMessage.ts +6 -10
  11. package/bot/methods/messages/sendContact.ts +5 -3
  12. package/bot/methods/messages/sendFile.ts +1 -1
  13. package/bot/methods/messages/sendGif.ts +23 -23
  14. package/bot/methods/messages/sendImage.ts +23 -23
  15. package/bot/methods/messages/sendLocation.ts +6 -3
  16. package/bot/methods/messages/sendMessage.ts +6 -2
  17. package/bot/methods/messages/sendMusic.ts +23 -23
  18. package/bot/methods/messages/sendPoll.ts +6 -3
  19. package/bot/methods/messages/sendSticker.ts +8 -4
  20. package/bot/methods/messages/sendVideo.ts +23 -23
  21. package/bot/methods/messages/sendVoice.ts +23 -23
  22. package/bot/methods/utilities/getUpdates.ts +1 -1
  23. package/bot/methods/utilities/handleUpdates.ts +14 -14
  24. package/bot/methods/utilities/polling.ts +57 -32
  25. package/bot/methods/utilities/run.ts +2 -2
  26. package/bot/methods/utilities/start.ts +5 -2
  27. package/bot/methods/utilities/webhook.ts +1 -2
  28. package/bot/network.ts +43 -15
  29. package/bot/types/handlers.ts +3 -3
  30. package/bot/types/interfaces.ts +44 -4
  31. package/bot/types/utils.ts +6 -1
  32. package/bot/utils/antiSpam.ts +1 -1
  33. package/client/client.ts +35 -33
  34. package/client/contexts/activities.type.ts +2 -2
  35. package/client/contexts/chat.type.ts +2 -2
  36. package/client/contexts/message.type.ts +10 -3
  37. package/client/contexts/notifications.type.ts +2 -2
  38. package/client/crypto.ts +2 -2
  39. package/client/filters.ts +31 -31
  40. package/client/index.ts +5 -1
  41. package/client/methods/advanced/builder.ts +3 -1
  42. package/client/methods/auth/registerDevice.ts +8 -6
  43. package/client/methods/channels/addChannel.ts +1 -1
  44. package/client/methods/channels/editChannelInfo.ts +3 -3
  45. package/client/methods/channels/setChannelVoiceChatSetting.ts +1 -1
  46. package/client/methods/chats/editJoinLink.ts +1 -1
  47. package/client/methods/chats/seenChats.ts +1 -1
  48. package/client/methods/extras/deleteMessagebyCount.ts +40 -38
  49. package/client/methods/extras/onEditMessages.ts +1 -1
  50. package/client/methods/extras/userIsAdmin.ts +2 -2
  51. package/client/methods/groups/editGroupInfo.ts +13 -1
  52. package/client/methods/groups/setGroupVoiceChatSetting.ts +1 -1
  53. package/client/methods/messages/sendDocument.ts +3 -2
  54. package/client/methods/messages/sendFileInline.ts +1 -1
  55. package/client/methods/messages/sendGif.ts +4 -3
  56. package/client/methods/messages/sendMessage.ts +7 -5
  57. package/client/methods/messages/sendMessageAPICall.ts +18 -0
  58. package/client/methods/messages/sendMusic.ts +3 -2
  59. package/client/methods/messages/sendPhoto.ts +3 -2
  60. package/client/methods/messages/sendText.ts +4 -2
  61. package/client/methods/messages/sendVideo.ts +3 -2
  62. package/client/methods/messages/sendVideoMessage.ts +3 -2
  63. package/client/methods/messages/sendVoice.ts +3 -2
  64. package/client/methods/settings/updateProfile.ts +1 -1
  65. package/client/methods/utilities/start.ts +7 -4
  66. package/client/methods/utilities/thumbnail.ts +5 -5
  67. package/client/network/file.ts +13 -7
  68. package/client/network/websocket.ts +32 -15
  69. package/client/types/client.type.ts +10 -10
  70. package/client/utils/session.ts +1 -1
  71. package/index.ts +1 -0
  72. package/package.json +1 -7
  73. package/utils/formater.ts +1 -1
  74. package/utils/index.ts +2 -1
  75. package/utils/logger.ts +28 -0
  76. package/utils/parser.ts +120 -156
  77. package/utils/errors.ts +0 -26
package/utils/parser.ts CHANGED
@@ -1,173 +1,137 @@
1
- import { MarkdownType, MetaDataPart, MetadataResult } from "../bot/types/utils";
2
-
3
- const MENTION_PREFIX_TYPES: Record<string, string> = {
4
- u: "User",
5
- g: "Group",
6
- c: "Channel",
7
- b: "Bot",
1
+ type MarkdownEntity = {
2
+ type: string;
3
+ from_index: number;
4
+ length: number;
5
+ language?: string;
6
+ mention_text_user_id?: string;
7
+ mention_text_object_guid?: string;
8
+ mention_text_object_type?: string;
9
+ link?: { type: string; hyperlink_data: { url: string } };
8
10
  };
9
11
 
10
- function buildUtf16PrefixLengths(text: string): number[] {
11
- const prefixLengths: number[] = [0];
12
- let total = 0;
13
- for (const char of text) {
14
- total += char.codePointAt(0)! > 0xffff ? 2 : 1;
15
- prefixLengths.push(total);
16
- }
17
- return prefixLengths;
18
- }
19
-
20
- const MARKDOWN_RE =
21
- /(?:^(?:> ?[^\n]*\n?)+)|```([\s\S]*?)```|\*\*([^\n*]+?)\*\*|`([^\n`]+?)`|__([^\n_]+?)__|--([^\n-]+?)--|~~([^\n~]+?)~~|\|\|([^\n|]+?)\|\||\[([^\]]+?)\]\((\S+)\)/gms;
22
-
23
- const MARKDOWN_TYPE_SEQUENCE: [string, [MarkdownType, number | null]][] = [
24
- [">", ["Quote", null]],
25
- ["```", ["Pre", 1]],
26
- ["**", ["Bold", 2]],
27
- ["`", ["Mono", 3]],
28
- ["__", ["Italic", 4]],
29
- ["--", ["Underline", 5]],
30
- ["~~", ["Strike", 6]],
31
- ["||", ["Spoiler", 7]],
32
- ["[", ["Link", 8]],
33
- ];
34
-
35
- export default class Markdown {
36
- static toMetadata(text: string): MetadataResult {
37
- const metaDataParts: MetaDataPart[] = [];
38
- let currentText = text;
12
+ class Markdown {
13
+ static markdownRegExp =
14
+ /(^|\s|\n)(````?)([\s\S]+?)(````?)([\s\n.,:?!;]|$)|(^|\s)(`|\*\*|__|~~|--|\|\||\^\^)([^\n]+?)\7([\s.,:?!;]|$)|@([a-zA-Z0-9]+)\s*\((.+?)\)|\[(.+?)\]\((.+?)\)/m;
15
+
16
+ static markdownEntities: Record<string, string> = {
17
+ "`": "Mono",
18
+ "**": "Bold",
19
+ __: "Italic",
20
+ "||": "Spoiler",
21
+ "~~": "Strike",
22
+ "--": "Underline",
23
+ "^^": "Quote",
24
+ };
25
+
26
+ static toMetadata(text: string): {
27
+ text: string;
28
+ metadata?: { meta_data_parts: MarkdownEntity[] };
29
+ } {
30
+ const entities: MarkdownEntity[] = [];
31
+ let match: RegExpMatchArray | null;
32
+ let remainingText = text;
33
+ const parsedTextParts: string[] = [];
39
34
  let offset = 0;
40
- let charOffset = 0;
41
- const utf16Prefix = buildUtf16PrefixLengths(text);
42
-
43
- let match: RegExpExecArray | null;
44
- const globalRegex = new RegExp(MARKDOWN_RE, "gms");
45
-
46
- while ((match = globalRegex.exec(text)) !== null) {
47
- const group = match[0];
48
- const start = match.index;
49
- const end = start + group.length;
50
- const adjustedStart = utf16Prefix[start] - offset;
51
- const adjustedCharStart = start - charOffset;
52
-
53
- for (const [prefix, [mdType, groupIdx]] of MARKDOWN_TYPE_SEQUENCE) {
54
- if (group.startsWith(prefix)) {
55
- let content = "";
56
- let contentLength = 0;
57
- let charContentLength = 0;
58
-
59
- if (mdType === "Quote") {
60
- const quoteLines = group.split("\n");
61
- const contentLines = quoteLines.map((line) => {
62
- if (line.startsWith("> ")) return line.slice(2);
63
- if (line.startsWith(">")) return line.slice(1);
64
- return line;
65
- });
66
- content = contentLines.join("\n");
67
- charContentLength = content.length;
68
- contentLength = Markdown.getUtf16Length(content);
69
-
70
- const innerMeta = this.toMetadata(content);
71
- content = innerMeta.text;
72
- contentLength = Markdown.getUtf16Length(content);
73
- charContentLength = content.length;
74
-
75
- if (innerMeta.metadata) {
76
- for (const part of innerMeta.metadata.meta_data_parts) {
77
- part.from_index += adjustedStart;
78
- metaDataParts.push(part);
79
- }
80
- }
81
- } else {
82
- if (groupIdx !== null && match[groupIdx] !== undefined) {
83
- content = match[groupIdx] || "";
84
- const groupStart = match.index + group.indexOf(content);
85
- const groupEnd = groupStart + content.length;
86
- contentLength = utf16Prefix[groupEnd] - utf16Prefix[groupStart];
87
- charContentLength = content.length;
88
-
89
- if (mdType !== "Pre" && mdType !== "Link") {
90
- const innerMeta = this.toMetadata(content);
91
- content = innerMeta.text;
92
- contentLength = Markdown.getUtf16Length(content);
93
- charContentLength = content.length;
94
-
95
- if (innerMeta.metadata) {
96
- for (const part of innerMeta.metadata.meta_data_parts) {
97
- part.from_index += adjustedStart;
98
- metaDataParts.push(part);
99
- }
100
- }
101
- }
102
- } else {
103
- content = "";
104
- contentLength = 0;
105
- charContentLength = 0;
106
- }
35
+
36
+ while ((match = remainingText.match(this.markdownRegExp))) {
37
+ const matchIndex = offset + (match.index || 0);
38
+ parsedTextParts.push(remainingText.substring(0, match.index!));
39
+ let matchedContent = match[3] || match[8] || match[11] || match[13];
40
+
41
+ if (matchedContent) {
42
+ offset -= matchedContent.length;
43
+ matchedContent = matchedContent.trim();
44
+ offset += matchedContent.length;
45
+
46
+ if (/^`*$/.test(matchedContent)) {
47
+ parsedTextParts.push(match[0]);
48
+ } else if (match[3]) {
49
+ if (match[5] === "\n") {
50
+ match[5] = "";
51
+ offset -= 1;
52
+ }
53
+ parsedTextParts.push(match[1] + matchedContent + match[5]);
54
+ entities.push({
55
+ type: "Pre",
56
+ language: "",
57
+ from_index: matchIndex + match[1].length,
58
+ length: matchedContent.length,
59
+ });
60
+ offset -= match[2].length + match[4].length;
61
+ } else if (match[7]) {
62
+ parsedTextParts.push(match[6] + matchedContent + match[9]);
63
+ entities.push({
64
+ type: this.markdownEntities[match[7]],
65
+ from_index: matchIndex + match[6].length,
66
+ length: matchedContent.length,
67
+ });
68
+ offset -= 2 * match[7].length;
69
+ } else if (match[11]) {
70
+ parsedTextParts.push(matchedContent);
71
+ entities.push({
72
+ type: "MentionText",
73
+ mention_text_object_guid: match[10],
74
+ from_index: matchIndex,
75
+ length: matchedContent.length,
76
+ mention_text_object_type: "User",
77
+ });
78
+ offset -= match[0].length - matchedContent.length;
79
+ } else if (match[12]) {
80
+ const [label, url] = [match[12], match[13]];
81
+
82
+ let mentionType: string | undefined;
83
+ if (url.length === 32) {
84
+ if (url.startsWith("u")) mentionType = "User";
85
+ else if (url.startsWith("g")) mentionType = "Group";
86
+ else if (url.startsWith("c")) mentionType = "Channel";
107
87
  }
108
88
 
109
- const metaDataPart: MetaDataPart = {
110
- type: mdType,
111
- from_index: adjustedStart,
112
- length: contentLength,
89
+ parsedTextParts.push(label);
90
+ const metaDataPart: MarkdownEntity = {
91
+ type: mentionType ? "MentionText" : "Link",
92
+ from_index: matchIndex,
93
+ length: label.length,
113
94
  };
114
95
 
115
- // تنظیمات بر اساس نوع
116
- if (mdType === "Pre") {
117
- const lines = content.split("\n", 2);
118
- const language =
119
- lines.length > 1 && lines[0].trim() ? lines[0].trim() : "";
120
- metaDataPart.language = language;
121
- if (language) {
122
- content = lines.slice(1).join("\n");
123
- contentLength = Markdown.getUtf16Length(content);
124
- charContentLength = content.length;
125
- }
126
- } else if (mdType === "Link") {
127
- const url = match[9];
128
- const mentionType = MENTION_PREFIX_TYPES[url?.[0]] || "hyperlink";
129
-
130
- if (mentionType === "hyperlink") {
131
- metaDataPart.link_url = url;
132
- metaDataPart.link = {
133
- type: mentionType,
134
- hyperlink_data: { url },
135
- };
136
- } else {
137
- metaDataPart.type = "MentionText";
138
- metaDataPart.mention_text_object_guid = url;
139
- metaDataPart.mention_text_user_id = url;
140
- metaDataPart.mention_text_object_type = mentionType;
141
- }
96
+ if (mentionType) {
97
+ metaDataPart.mention_text_object_guid = url;
98
+ metaDataPart.mention_text_user_id = url;
99
+ metaDataPart.mention_text_object_type = mentionType;
100
+ } else {
101
+ metaDataPart.link = { type: "hyperlink", hyperlink_data: { url } };
142
102
  }
143
103
 
144
- metaDataParts.push(metaDataPart);
145
- currentText =
146
- currentText.slice(0, adjustedCharStart) +
147
- content +
148
- currentText.slice(end - charOffset);
149
-
150
- const markupLength = utf16Prefix[end] - utf16Prefix[start];
151
- const charMarkupLength = end - start;
152
- offset += markupLength - contentLength;
153
- charOffset += charMarkupLength - charContentLength;
154
-
155
- break;
104
+ entities.push(metaDataPart);
105
+ offset -= match[0].length - label.length;
156
106
  }
107
+
108
+ remainingText = remainingText.substring(match.index! + match[0].length);
109
+ offset += match.index! + match[0].length;
157
110
  }
158
111
  }
159
112
 
160
- return {
161
- text: currentText.trim(),
162
- ...(metaDataParts.length > 0 && {
163
- metadata: { meta_data_parts: metaDataParts },
164
- }),
113
+ parsedTextParts.push(remainingText);
114
+ let resultText = parsedTextParts.join("");
115
+ if (!resultText.replace(/\s+/g, "").length) {
116
+ resultText = text;
117
+ entities.splice(0, entities.length);
118
+ }
119
+ if (!entities.length) {
120
+ resultText = resultText.trim();
121
+ }
122
+
123
+ const returnData: {
124
+ text: string;
125
+ metadata?: { meta_data_parts: MarkdownEntity[] };
126
+ } = {
127
+ text: resultText,
165
128
  };
166
- }
167
129
 
168
- static getUtf16Length(str: string): number {
169
- return str.split("").reduce((len, char) => {
170
- return len + (char.codePointAt(0)! > 0xffff ? 2 : 1);
171
- }, 0);
130
+ if (entities.length > 0)
131
+ returnData.metadata = { meta_data_parts: entities };
132
+
133
+ return returnData;
172
134
  }
173
135
  }
136
+
137
+ export default Markdown;
package/utils/errors.ts DELETED
@@ -1,26 +0,0 @@
1
- import checkFilters from "./checkFilter";
2
-
3
- class Logger<T> {
4
- constructor(
5
- private errors: any,
6
- private bot: T,
7
- ) {}
8
-
9
- async error(text: string, type: "error" | "warn") {
10
- const time = new Date();
11
- for (let { filters, handler } of this.errors) {
12
- const error = {
13
- message: `${time.toISOString()} | ⟮ ${type} ⟯ ----> ${text}\n`,
14
- bot: this.bot,
15
- };
16
-
17
- const passed = await checkFilters(text, filters);
18
-
19
- if (passed) {
20
- await handler(error);
21
- }
22
- }
23
- }
24
- }
25
-
26
- export default Logger;