File size: 8,040 Bytes
80ce16c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const fs = require("fs-extra");

module.exports = {
	config: {
		name: "jsontomongodb",
		aliases: ["jsontomongo"],
		version: "1.4",
		author: "NTKhang",
		countDown: 5,
		role: 2,
		shortDescription: {
			vi: "Đồng bộ dữ liệu từ json sang mongodb",
			en: "Synchronize data from json to mongodb"
		},
		longDescription: {
			vi: "Đồng bộ dữ liệu từ json sang mongodb",
			en: "Synchronize data from json to mongodb"
		},
		category: "owner",
		guide: {
			vi: "   {pn} <thread | user | dashboard | global | all>: Sẽ đồng bộ dữ liệu từ data json được lưu trong thư mục database/data sang mongodb\n\n   Lưu ý: Nếu dữ liệu đã tồn tại trong mongodb thì sẽ được cập nhật lại",
			en: "   {pn} <thread | user | dashboard | global | all>: Will synchronize data from json data stored in the database/data folder to mongodb\n\n   Note: If the data already exists in mongodb, it will be updated"
		}
	},

	langs: {
		vi: {
			invalidDatabase: "❌ Vui lòng chuyển database sang mongodb trong config sau đó khởi động lại bot để sử dụng lệnh này",
			missingFile: "❌ Bạn chưa sao chép dữ liệu file %1 vào thư mục database/data",
			formatInvalid: "❌ Định dạng dữ liệu không hợp lệ",
			error: "❌ Đã có lỗi xảy ra:\n%1: %2",
			successThread: "✅ Đã đồng bộ dữ liệu nhóm từ json sang mongodb thành công!",
			successUser: "✅ Đã đồng bộ dữ liệu người dùng từ json sang mongodb thành công!",
			successDashboard: "✅ Đã đồng bộ dữ liệu dashboard từ json sang mongodb thành công!",
			successGlobal: "✅ Đã đồng bộ dữ liệu global từ json sang mongodb thành công!"
		},
		en: {
			invalidDatabase: "❌ Please switch database to mongodb in config then restart the bot to use this command",
			missingFile: "❌ You haven't copied the data file %1 into the database/data folder",
			formatInvalid: "❌ Data format is invalid",
			error: "❌ An error occurred:\n%1: %2",
			successThread: "✅ Successfully synchronized thread data from json to mongodb!",
			successUser: "✅ Successfully synchronized user data from json to mongodb!",
			successDashboard: "✅ Successfully synchronized dashboard data from json to mongodb!",
			successGlobal: "✅ Successfully synchronized global data from json to mongodb!"
		}
	},

	onStart: async function ({ args, message, threadModel, userModel, dashBoardModel, globalModel, getLang }) {
		if (global.GoatBot.config.database.type !== "mongodb")
			return message.reply(getLang("invalidDatabase"));

		switch (args[0]) {
			case "thread": {
				return syncThreadData(message, threadModel, getLang);
			}
			case "user": {
				return syncUserData(message, userModel, getLang);
			}
			case "dashboard": {
				return syncDashBoardData(message, dashBoardModel, getLang);
			}
			case "global": {
				return syncGlobalData(message, globalModel, getLang);
			}
			case "all": {
				await syncThreadData(message, threadModel, getLang);
				await syncUserData(message, userModel, getLang);
				await syncDashBoardData(message, dashBoardModel, getLang);
				await syncGlobalData(message, globalModel, getLang);
				return;
			}
			default:
				return message.SyntaxError();
		}
	}
};

async function syncThreadData(message, threadModel, getLang) {
	let oldThreadsData;
	const pathThreadData = `${process.cwd()}/database/data/threadsData.json`;
	if (!fs.existsSync(pathThreadData))
		return message.reply(getLang("missingFile", pathThreadData.split("/").pop()));

	try {
		oldThreadsData = require(pathThreadData);
		delete require.cache[require.resolve(pathThreadData)];
	}
	catch (err) {
		return message.reply(getLang("formatInvalid"));
	}

	try {
		const bulkOperations = [];

		for (const thread of oldThreadsData) {
			const threadIndex = global.db.allThreadData.findIndex(item => item.threadID == thread.threadID);
			if (threadIndex === -1) {
				bulkOperations.push({
					insertOne: {
						document: thread
					}
				});
			}
			else {
				bulkOperations.push({
					updateOne: {
						filter: { threadID: thread.threadID },
						update: thread
					}
				});
			}
		}

		if (bulkOperations.length > 0) {
			await threadModel.bulkWrite(bulkOperations);
			global.db.allThreadData = await threadModel.find({}).lean();
		}

		return message.reply(getLang("successThread"));
	}
	catch (err) {
		return message.reply(getLang("error", err.name, err.message));
	}
}

async function syncUserData(message, userModel, getLang) {
	let oldUsersData;
	const pathUsersData = `${process.cwd()}/database/data/usersData.json`;
	if (!fs.existsSync(pathUsersData))
		return message.reply(getLang("missingFile", pathUsersData.split("/").pop()));

	try {
		oldUsersData = require(pathUsersData);
		delete require.cache[require.resolve(pathUsersData)];
	}
	catch (err) {
		return message.reply(getLang("formatInvalid"));
	}

	try {
		const bulkOperations = [];

		for (const user of oldUsersData) {
			const userIndex = global.db.allUserData.findIndex(item => item.userID == user.userID);
			if (userIndex === -1) {
				bulkOperations.push({
					insertOne: {
						document: user
					}
				});
			}
			else {
				bulkOperations.push({
					updateOne: {
						filter: { userID: user.userID },
						update: user
					}
				});
			}
		}

		if (bulkOperations.length > 0) {
			await userModel.bulkWrite(bulkOperations);
			global.db.allUserData = await userModel.find({}).lean();
		}

		return message.reply(getLang("successUser"));
	}
	catch (err) {
		return message.reply(getLang("error", err.name, err.message));
	}
}

async function syncDashBoardData(message, dashBoardModel, getLang) {
	let oldDashBoardData;
	const pathDashBoardData = `${process.cwd()}/database/data/dashBoardData.json`;
	if (!fs.existsSync(pathDashBoardData))
		return message.reply(getLang("missingFile", pathDashBoardData.split("/").pop()));

	try {
		oldDashBoardData = require(pathDashBoardData);
		delete require.cache[require.resolve(pathDashBoardData)];
	}
	catch (err) {
		return message.reply(getLang("formatInvalid"));
	}

	try {
		const bulkOperations = [];

		for (const dashboard of oldDashBoardData) {
			const dashboardIndex = global.db.allDashBoardData.findIndex(item => item.email == dashboard.email);
			if (dashboardIndex === -1) {
				bulkOperations.push({
					insertOne: {
						document: dashboard
					}
				});
			}
			else {
				bulkOperations.push({
					updateOne: {
						filter: { email: dashboard.email },
						update: dashboard
					}
				});
			}
		}

		if (bulkOperations.length > 0) {
			await dashBoardModel.bulkWrite(bulkOperations);
			global.db.allDashBoardData = await dashBoardModel.find({}).lean();
		}

		return message.reply(getLang("successDashboard"));
	}
	catch (err) {
		return message.reply(getLang("error", err.name, err.message));
	}
}

async function syncGlobalData(message, globalModel, getLang) {
	let oldGlobalData;
	const pathGlobalData = `${process.cwd()}/database/data/globalData.json`;
	if (!fs.existsSync(pathGlobalData))
		return message.reply(getLang("missingFile", pathGlobalData.split("/").pop()));

	try {
		oldGlobalData = require(pathGlobalData);
		delete require.cache[require.resolve(pathGlobalData)];
	}
	catch (err) {
		return message.reply(getLang("formatInvalid"));
	}

	try {
		const bulkOperations = [];

		for (const global_ of oldGlobalData) {
			const globalIndex = global.db.allGlobalData.findIndex(item => item.key == global_.key);
			if (globalIndex === -1) {
				bulkOperations.push({
					insertOne: {
						document: global_
					}
				});
			}
			else {
				bulkOperations.push({
					updateOne: {
						filter: { key: global_.key },
						update: global_
					}
				});
			}
		}

		if (bulkOperations.length > 0) {
			await globalModel.bulkWrite(bulkOperations);
			global.db.allGlobalData = await globalModel.find({}).lean();
		}

		return message.reply(getLang("successGlobal"));
	}
	catch (err) {
		return message.reply(getLang("error", err.name, err.message));
	}
}