ShadowVNs commited on
Commit
4efb4d5
·
verified ·
1 Parent(s): d8568e7

Upload A.js

Browse files
Files changed (1) hide show
  1. A.js +132 -0
A.js ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const login = require("./fca-unofficial");
2
+ const { join } = require("path");
3
+ const { readdirSync, readFileSync } = require("fs-extra");
4
+
5
+ const { get, set } = require("./model/restart");
6
+ global.config = require("./config.json");
7
+ global.cookie = readFileSync(join(process.cwd(), "cookie.txt"), "utf8");
8
+
9
+ const { ADMIN, PREFIX } = global.config;
10
+
11
+ const readCommands = () => {
12
+ const commands = {};
13
+ const events = {};
14
+
15
+ const loadModule = (folder, type) => {
16
+ readdirSync(__dirname + "/scripts/" + folder).forEach((file) => {
17
+ const filePath = join(process.cwd(), "scripts", folder, file);
18
+ if (file.endsWith(".js")) {
19
+ const commandModule = require(filePath);
20
+ if (
21
+ typeof commandModule.constructor === "function" &&
22
+ typeof commandModule.on_Start === "function"
23
+ ) {
24
+ const configName = new commandModule.constructor().config.name;
25
+ if (type === "commands") commands[configName] = commandModule;
26
+ else events[configName] = commandModule;
27
+ }
28
+ }
29
+ });
30
+ };
31
+
32
+ loadModule("commands", "commands");
33
+ loadModule("events", "events");
34
+
35
+ console.log(`Đã load thành công ${Object.keys(commands).length} commands`);
36
+ console.log(`Đã load thành công ${Object.keys(events).length} events`);
37
+
38
+ return { commands, events };
39
+ };
40
+
41
+ const parseCommand = (message) => {
42
+ if (!message.body.startsWith(PREFIX)) return null;
43
+ const parts = message.body.split(" ");
44
+ const command = parts[0].substring(PREFIX.length);
45
+ const permssion = ADMIN.includes(message.senderID) ? 1 : 0;
46
+ const permssions = global.commands[command]?.config.permissions || [];
47
+ const check = permssions.includes(permssion);
48
+ return { name: command, permssion: check };
49
+ };
50
+
51
+ const connectMqtt = (api, listenerCallback) => {
52
+ global.Listener = api.listenMqtt(listenerCallback);
53
+ setTimeout(() => {
54
+ mqttClient.end();
55
+ setTimeout(() => {
56
+ console.log("[ ListenMqtt ]", "Đang bắt đầu khởi tạo lại mqtt...");
57
+ connectMqtt(api, listenerCallback);
58
+ }, 1000);
59
+ }, 1000 * 60 * 60 * 1);
60
+ };
61
+
62
+
63
+ async function run_Bot(api) {
64
+ connectMqtt(global.api, async (err, message) => {
65
+ if (err) {
66
+ if (JSON.stringify(err).includes("601051028565049")) {
67
+ await passScraping(api);
68
+
69
+ mqttClient.end();
70
+ return run_Bot(api);
71
+ }
72
+ return console.error(err);
73
+ }
74
+ message.reply = api.sendMessage;
75
+
76
+ const restart = get("all");
77
+ if (restart?.status) {
78
+ setTimeout(() => {
79
+ message.reply("Đã khởi động lại thành công!", restart.threadID);
80
+ set(["status", "threadID"], [false, ""]);
81
+ }, 1000);
82
+ }
83
+ switch (message.type) {
84
+ case "message":
85
+ // message.reply("xin chào", message.threadID)
86
+ const getCommand = parseCommand(message);
87
+ if (!getCommand) return;
88
+ const { name: commandName, permssion } = getCommand;
89
+ const command = global.commands[commandName];
90
+ if (!command)
91
+ return message.reply(
92
+ "Lệnh bạn sử dụng không tồn tại!",
93
+ message.threadID
94
+ );
95
+ if (!permssion)
96
+ return message.reply(
97
+ "Bạn không đủ quyền để sử dụng lệnh này!",
98
+ message.threadID
99
+ );
100
+ message.body = message.body.replace(`/${commandName}`, "").trim();
101
+ message.args.shift();
102
+ command.on_Start({ message });
103
+ break;
104
+ case "event":
105
+ for (const eventName in global.events) {
106
+ var event = global.events[eventName];
107
+ const eventConfig = event.config;
108
+ if (eventConfig.logMessageType === message.logMessageType) {
109
+ event.on_Start({ message });
110
+ }
111
+ }
112
+ break;
113
+ default:
114
+ break;
115
+ }
116
+ });
117
+ }
118
+
119
+ (async () => {
120
+ login({ cookie: global.cookie }, (err, api) => {
121
+ if (err) return console.error(err);
122
+ global.api = api;
123
+ const { commands, events } = readCommands();
124
+ global.commands = commands;
125
+ global.events = events;
126
+ run_Bot(api);
127
+ });
128
+ })();
129
+
130
+ process.on("unhandledRejection", (reason, promise) => {
131
+ console.error("reason:", reason);
132
+ });