| const login = require("./fca-unofficial"); |
| const { join } = require("path"); |
| const { readdirSync, readFileSync } = require("fs-extra"); |
|
|
| const { get, set } = require("./model/restart"); |
| global.config = require("./config.json"); |
| global.cookie = readFileSync(join(process.cwd(), "cookie.txt"), "utf8"); |
|
|
| const { ADMIN, PREFIX } = global.config; |
|
|
| const readCommands = () => { |
| const commands = {}; |
| const events = {}; |
|
|
| const loadModule = (folder, type) => { |
| readdirSync(__dirname + "/scripts/" + folder).forEach((file) => { |
| const filePath = join(process.cwd(), "scripts", folder, file); |
| if (file.endsWith(".js")) { |
| const commandModule = require(filePath); |
| if ( |
| typeof commandModule.constructor === "function" && |
| typeof commandModule.on_Start === "function" |
| ) { |
| const configName = new commandModule.constructor().config.name; |
| if (type === "commands") commands[configName] = commandModule; |
| else events[configName] = commandModule; |
| } |
| } |
| }); |
| }; |
|
|
| loadModule("commands", "commands"); |
| loadModule("events", "events"); |
|
|
| console.log(`Đã load thành công ${Object.keys(commands).length} commands`); |
| console.log(`Đã load thành công ${Object.keys(events).length} events`); |
|
|
| return { commands, events }; |
| }; |
|
|
| const parseCommand = (message) => { |
| if (!message.body.startsWith(PREFIX)) return null; |
| const parts = message.body.split(" "); |
| const command = parts[0].substring(PREFIX.length); |
| const permssion = ADMIN.includes(message.senderID) ? 1 : 0; |
| const permssions = global.commands[command]?.config.permissions || []; |
| const check = permssions.includes(permssion); |
| return { name: command, permssion: check }; |
| }; |
|
|
| const connectMqtt = (api, listenerCallback) => { |
| global.Listener = api.listenMqtt(listenerCallback); |
| setTimeout(() => { |
| mqttClient.end(); |
| setTimeout(() => { |
| console.log("[ ListenMqtt ]", "Đang bắt đầu khởi tạo lại mqtt..."); |
| connectMqtt(api, listenerCallback); |
| }, 1000); |
| }, 1000 * 60 * 60 * 1); |
| }; |
|
|
|
|
| async function run_Bot(api) { |
| connectMqtt(global.api, async (err, message) => { |
| if (err) { |
| if (JSON.stringify(err).includes("601051028565049")) { |
| await passScraping(api); |
|
|
| mqttClient.end(); |
| return run_Bot(api); |
| } |
| return console.error(err); |
| } |
| message.reply = api.sendMessage; |
|
|
| const restart = get("all"); |
| if (restart?.status) { |
| setTimeout(() => { |
| message.reply("Đã khởi động lại thành công!", restart.threadID); |
| set(["status", "threadID"], [false, ""]); |
| }, 1000); |
| } |
| switch (message.type) { |
| case "message": |
| |
| const getCommand = parseCommand(message); |
| if (!getCommand) return; |
| const { name: commandName, permssion } = getCommand; |
| const command = global.commands[commandName]; |
| if (!command) |
| return message.reply( |
| "Lệnh bạn sử dụng không tồn tại!", |
| message.threadID |
| ); |
| if (!permssion) |
| return message.reply( |
| "Bạn không đủ quyền để sử dụng lệnh này!", |
| message.threadID |
| ); |
| message.body = message.body.replace(`/${commandName}`, "").trim(); |
| message.args.shift(); |
| command.on_Start({ message }); |
| break; |
| case "event": |
| for (const eventName in global.events) { |
| var event = global.events[eventName]; |
| const eventConfig = event.config; |
| if (eventConfig.logMessageType === message.logMessageType) { |
| event.on_Start({ message }); |
| } |
| } |
| break; |
| default: |
| break; |
| } |
| }); |
| } |
|
|
| (async () => { |
| login({ cookie: global.cookie }, (err, api) => { |
| if (err) return console.error(err); |
| global.api = api; |
| const { commands, events } = readCommands(); |
| global.commands = commands; |
| global.events = events; |
| run_Bot(api); |
| }); |
| })(); |
|
|
| process.on("unhandledRejection", (reason, promise) => { |
| console.error("reason:", reason); |
| }); |