File size: 6,305 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 | import fs from "fs";
import path from "path";
import chalk from "chalk";
import { mkdir } from "fs/promises";
import { getTimeToString, getTimeNow } from "./format-util.js";
const adminFilePath = path.resolve("./assets/data/list_admin.json");
const groupSettingsPath = path.resolve("./assets/data/group_settings.json");
const configFilePath = path.resolve("./assets/config.json");
const commandFilePath = path.resolve("./assets/json-data/command.json");
export function readConfig() {
let config = {};
try {
const data = fs.readFileSync(configFilePath, "utf-8");
config = JSON.parse(data);
} catch (error) {
console.error("Lỗi đọc tệp config.json:", error);
config = {};
}
return config;
}
export function readAdmins() {
let admins = [];
try {
const data = fs.readFileSync(adminFilePath, "utf-8");
admins = JSON.parse(data);
} catch (error) {
console.error("Lỗi đọc tệp admin:", error);
admins = [];
}
return admins;
}
const logDir = path.join(process.cwd(), "logs");
export const resourceDir = path.join(process.cwd(), "assets", "resources");
export const tempDir = path.join(process.cwd(), "assets", "temp");
export const dataGifPath = path.join(process.cwd(), "assets", "resources", "gif");
const logManagerBotFilePath = path.join(logDir, "bot-manager.log");
const loggingMessageFilePath = path.join(logDir, "message.txt");
const loggingMessageJsonPath = path.join(logDir, "message.json");
const dataGamePath = path.join(process.cwd(), "assets", "json-data", "data-game.json");
export async function ensureLogFiles() {
try {
await mkdir(logDir, { recursive: true });
await mkdir(resourceDir, { recursive: true });
await mkdir(tempDir, { recursive: true });
await mkdir(dataGifPath, { recursive: true });
if (!fs.existsSync(logManagerBotFilePath)) {
fs.writeFileSync(logManagerBotFilePath, "");
}
if (!fs.existsSync(loggingMessageFilePath)) {
fs.writeFileSync(loggingMessageFilePath, "");
}
if (!fs.existsSync(loggingMessageJsonPath)) {
fs.writeFileSync(loggingMessageJsonPath, "{}");
}
if (!fs.existsSync(dataGamePath)) {
fs.writeFileSync(dataGamePath, "{}");
}
} catch (err) {
console.error("Lỗi khi tạo thư mục hoặc file log:", err);
}
}
export function mkdirRecursive(dirPath) {
if (fs.existsSync(dirPath)) return;
try {
fs.mkdirSync(dirPath, { recursive: true });
} catch (err) {
if (err.code !== "EEXIST") throw err;
}
}
export function logManagerBot(message) {
const timestamp = getTimeToString(getTimeNow());
const logEntry = `${timestamp} - ${message}\n`;
fs.appendFileSync(logManagerBotFilePath, logEntry);
}
export function logMessageToFile(data, type = "message") {
const timestamp = getTimeToString(getTimeNow());
const logData = `${data}\n`;
fs.appendFileSync(loggingMessageFilePath, logData + "\n", "utf8");
if (type === "group") {
console.log(chalk.yellowBright.bold(`[${timestamp}]\n`), chalk.yellowBright(logData));
} else {
console.log(chalk.blueBright.bold(`[${timestamp}]\n`), chalk.blueBright(logData));
}
}
export function readGroupSettings() {
try {
const data = fs.readFileSync(groupSettingsPath, "utf-8");
return JSON.parse(data);
} catch (error) {
console.error("Lỗi khi đọc file group_settings.json:", error);
return {};
}
}
export function writeGroupSettings(settings) {
try {
fs.writeFileSync(groupSettingsPath, JSON.stringify(settings, null, 2), "utf-8");
} catch (error) {
console.error("Lỗi khi ghi file group_settings.json:", error);
}
}
export function readCommandConfig() {
try {
const data = fs.readFileSync(commandFilePath, "utf-8");
return JSON.parse(data);
} catch (error) {
console.error("Lỗi khi đọc file command.json:", error);
return { commands: [] };
}
}
export function writeCommandConfig(config) {
try {
fs.writeFileSync(commandFilePath, JSON.stringify(config, null, 2));
} catch (error) {
console.error("Lỗi khi ghi file command.json:", error);
}
}
const WEB_CONFIG_PATH = path.join(process.cwd(), "assets", "web-config", "web-config.json");
export function readWebConfig() {
try {
const data = fs.readFileSync(WEB_CONFIG_PATH, "utf-8");
return JSON.parse(data);
} catch (error) {
console.error("Lỗi khi đọc file web-config.json:", error);
return {};
}
}
export function writeWebConfig(config) {
fs.writeFileSync(WEB_CONFIG_PATH, JSON.stringify(config, null, 2));
}
const MANAGER_FILE_PATH = path.join(process.cwd(), "assets", "json-data", "manager-bot.json");
export function readManagerFile() {
try {
const data = fs.readFileSync(MANAGER_FILE_PATH, "utf8");
let parsedData = JSON.parse(data);
if (!parsedData) {
parsedData = {};
}
return parsedData;
} catch (error) {
if (error.code === "ENOENT") {
return {};
}
console.error("Lỗi khi đọc file block:", error);
return {};
}
}
export function writeManagerFile(data) {
fs.writeFileSync(MANAGER_FILE_PATH, JSON.stringify(data, null, 2));
}
export function pushMessageToWebLog(io, nameType, senderName, content, avtGroup) {
if (io) {
const messageData = { nameType, senderName, content, avtGroup };
io.emit("newMessage", messageData);
}
}
const PROPHYLACTIC_CONFIG_PATH = path.join(process.cwd(), "assets", "json-data", "prophylactic.json");
export function readProphylacticConfig() {
try {
const data = fs.readFileSync(PROPHYLACTIC_CONFIG_PATH, "utf8");
const parsedData = JSON.parse(data);
return parsedData;
} catch (error) {
console.error("Lỗi khi đọc file prophylactic.json:", error);
return {
prophylacticUploadAttachment: {
enable: false,
lastBlocked: "",
numRequestZalo: 0,
},
};
}
}
export function writeProphylacticConfig(data) {
try {
fs.writeFileSync(PROPHYLACTIC_CONFIG_PATH, JSON.stringify(data, null, 2));
} catch (error) {
console.error("Lỗi khi ghi file prophylactic.json:", error);
}
}
|