File size: 6,991 Bytes
183a3de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

                   _ooOoo_

                  o8888888o

                  88" . "88

                  (| -_- |)

                  O\  =  /O

               ____/`---'\____

             .'  \\|     |//  `.

            /  \\|||  :  |||//  \

           /  _||||| -:- |||||-  \

           |   | \\\  -  /// |   |

           | \_|  ''\---/''  |   |

           \  .-\__  `-`  ___/-. /

         ___`. .'  /--.--\  `. . __

      ."" '<  `.___\_<|>_/___.'  >'"".

     | | :  `- \`.;`\ _ /`;.`/ - ` : | |

     \  \ `-.   \_ __\ /__ _/   .-` /  /

======`-.____`-.___\_____/___.-`____.-'======

                   `=---='

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  PHẬT ĐỘ, CODE KHÔNG LỖI, TỐI ƯU KHÔNG BUG

            DEVELOPER: NDQ x LQT

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

import { Zalo } from "./api-zalo/index.js";

import { groupEvents } from "./automations/events-group.js";
import { messagesUser } from "./automations/event-send-msg.js";
import { undoMessageEvents } from "./automations/event-delete-msg.js";

import {
  readAdmins,
  readConfig,
  readGroupSettings,
  readCommandConfig,
  writeProphylacticConfig,
  readProphylacticConfig
} from "./utils/io-json.js";

import { logManagerBot } from "./utils/io-json.js";
import { initService } from "./service-dqt/service.js";
import { reactionEvents } from "./automations/events-reaction.js";
//import { updateMessageCache } from "./service-dqt/anti-service/anti-undo.js";
import { updateMessageCache } from "./utils/message-cache.js";
import { handleGroupEventNotify } from "./service-dqt/servises/SettingGroup.js";
import { handleWelcomeByGroup } from "./service-dqt/servises/send-msg-code.js";
import { handleAutoBlockOnJoin } from "./service-dqt/servises/block-user-join.js";

let idBot = -1;
const prophylacticConfig = readProphylacticConfig();
export let admins = readAdmins();
let config = readConfig();
let commandConfig = readCommandConfig();

const zalo = new Zalo(
  {
    cookie: config.cookie,
    imei: config.imei,
    userAgent: config.userAgent,
  },
  {
    selfListen: true, // Trả lời bản thân
    checkUpdate: false, // Cập nhật code
  }
);

export function getApi() {
  return api;
}

export function getBotId() {
  return idBot;
}

export function setBotId(id) {
  idBot = id;
}

export function getCommandConfig() {
  return commandConfig;
}

export function reloadCommandConfig() {
  commandConfig = readCommandConfig();
  return commandConfig;
}

export function getProphylacticConfig() {
  return prophylacticConfig;
}

export function getProphylacticUploadAttachment() {
  return prophylacticConfig.prophylacticUploadAttachment.enable;
}

export function setProphylacticUploadAttachment(enable, resetNum = false) {
  prophylacticConfig.prophylacticUploadAttachment.enable = enable;
  prophylacticConfig.prophylacticUploadAttachment.lastBlocked = Date.now();
  if (resetNum) prophylacticConfig.prophylacticUploadAttachment.numRequestZalo = 0;
  writeProphylacticConfig(prophylacticConfig);
}

const timeResetNumberRequestUpload = 120 * 60 * 1000;
const timeDisableProphylacticConfig = 120 * 60 * 1000;
const maxRequestUploadIntoNotProphylactic = 300;

export function checkDisableProphylacticConfig() {
  if (prophylacticConfig.prophylacticUploadAttachment.enable) {
    const currentTime = Date.now();
    const lastBlockedTime = prophylacticConfig.prophylacticUploadAttachment.lastBlocked;
    const timeDifference = currentTime - lastBlockedTime;

    if (timeDifference > timeDisableProphylacticConfig) {
      setProphylacticUploadAttachment(false, true);
    }
  }
}

export function checkConfigUploadAttachment(extFile) {
  if (["jpg", "jpeg", "png", "webp"].includes(extFile)) {
    const currentTime = Date.now();
    if (currentTime - prophylacticConfig.prophylacticUploadAttachment.lastRequestTime > timeResetNumberRequestUpload) {
      prophylacticConfig.prophylacticUploadAttachment.numRequestZalo = 0;
      prophylacticConfig.prophylacticUploadAttachment.lastRequestTime = currentTime;
    }

    prophylacticConfig.prophylacticUploadAttachment.numRequestZalo++;
    writeProphylacticConfig(prophylacticConfig);

    if (prophylacticConfig.prophylacticUploadAttachment.numRequestZalo > maxRequestUploadIntoNotProphylactic) {
      setProphylacticUploadAttachment(true);
      prophylacticConfig.prophylacticUploadAttachment.lastBlocked = currentTime;
    }
  }
}

export function isAdmin(userId, threadId, groupAdmins) {
  if (admins.includes(userId.toString())) {
    return true;
  }

  const groupSettings = readGroupSettings();
  if (threadId && groupSettings[threadId] && typeof groupSettings[threadId]["adminList"] === "object") {
    if (Object.keys(groupSettings[threadId]["adminList"]).includes(userId.toString())) {
      return true;
    }
  }

  if (groupAdmins && Array.isArray(groupAdmins) && groupAdmins.includes(userId.toString())) {
    return true;
  }

  return false;
}

const api = await zalo.login();

// Khởi động server
initService(api);

// Xử Lý Tin Nhắn Riêng Và Tin Nhắn Nhóm
api.listener.on("message", async (message) => {
  try {
    await messagesUser(api, message);
  } catch (error) {
    const detailError = `Mã Lỗi: ${error.code} - > Chú Thích Lỗi Tin Nhắn: ${error.message}\nNội Dung Lỗi: ${error.stack}`;
    console.error(detailError);
    logManagerBot(detailError);
  }
  updateMessageCache(message);
});

// Xử Lý Sự Kiện Nhóm
api.listener.on("group_event", async (event) => {
  try {
    await groupEvents(api, event);
    await handleGroupEventNotify(api, event);
    await handleWelcomeByGroup (api, event);
    await handleAutoBlockOnJoin ( api,event);
  } catch (error) {
    const detailError = `Mã Lỗi: ${error.code} - > Chú Thích Lỗi Sự Kiện Nhóm: ${error.message}\nNội Dung Lỗi: ${error.stack}`;
    console.error(detailError);
    logManagerBot(detailError);
  }
});

//Xử Lý Sự Kiện Undo Message
api.listener.on("undo", async (undo) => {
  try {
    await undoMessageEvents(api, undo);
  } catch (error) {
    const detailError = `Mã Lỗi: ${error.code} - > Chú Thích Lỗi Sự Kiện Delete Message: ${error.message}\nNội Dung Lỗi: ${error.stack}`;
    console.error(detailError);
    logManagerBot(detailError);
  }
});

//Xử Lý Sự Kiện Reaction
api.listener.on("reaction", async (reaction) => {
  try {
  //  console.log("Dữ liệu reaction nhận được:", reaction);
    await reactionEvents(api, reaction);
  } catch (error) {
    const detailError = `Mã Lỗi: ${error.code} - > Chú Thích Lỗi Sự Kiện Reaction: ${error.message}\nNội Dung Lỗi: ${error.stack}`;
    console.error(detailError);
    logManagerBot(detailError);
  }
});

api.listener.start();