-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
147 lines (116 loc) · 4.65 KB
/
start.js
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
require('dotenv').config(); // โหลดค่าจาก .env ไฟล์
const fs = require('fs');
const { Client } = require('pg');
const { WebhookClient } = require('discord.js');
const lastCheckedUserIdFile = 'lastCheckedUserId.json';
let invalidIdsAlreadyNotified = false; // ตัวแปรเพื่อตรวจสอบว่า webhook ถูกส่งไปแล้วหรือไม่
let lastCheckedUserId;
try {
lastCheckedUserId = require(`./${lastCheckedUserIdFile}`);
} catch (err) {
console.error('Failed to load last checked user ID file:', err.message);
}
const pgClient = new Client({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
});
const webhookClient = new WebhookClient({
id: process.env.WEBHOOK_ID,
token: process.env.WEBHOOK_TOKEN,
});
pgClient.connect((err) => {
if (err) {
console.error('Failed to connect to PostgreSQL database:', err.stack);
return;
}
console.log('Connected to PostgreSQL database successfully');
// เรียกใช้ checkForSubmit() และ validateJsonIds() เพื่อให้ทำงานอย่างต่อเนื่อง
setInterval(() => {
checkForSubmit();
validateJsonIds();
}, 1000);
});
async function getTotalIdsInDatabase() {
try {
const result = await pgClient.query('SELECT COUNT(*) FROM "User"');
return result.rows[0].count;
} catch (error) {
console.error('Error fetching total IDs in database:', error);
throw error;
}
}
async function checkForSubmit() {
try {
const totalIdsInDatabase = await getTotalIdsInDatabase();
const result = await pgClient.query('SELECT id FROM "User" WHERE is_registered = TRUE');
const newSubmissions = result.rows.filter(user => !lastCheckedUserId.includes(user.id));
const currentApplicants = result.rows.length;
if (newSubmissions.length > 0) {
const peopleNotSubmitted = totalIdsInDatabase - lastCheckedUserId.length - 1;
const message = newSubmissions.map(user => {
return `
** **
** ID : ${user.id} **
Number of current applicants : ${currentApplicants}
not submitted yet: ${peopleNotSubmitted}
Total number of IDs: ${totalIdsInDatabase}
Information may be inaccurate +- 3 people.
`;
}).join('\n');
webhookClient.send({
embeds: [{
color: 0x00FF00, // Green color
title: "🎉 New Submissions",
description: message
}]
});
lastCheckedUserId.push(...newSubmissions.map(user => user.id));
fs.writeFileSync(lastCheckedUserIdFile, JSON.stringify(lastCheckedUserId, null, 2));
await validateJsonIds();
}
} catch (error) {
console.error('Error checking for submit:', error);
}
}
async function validateJsonIds() {
try {
// Load JSON data from the file
const jsonData = require(`./${lastCheckedUserIdFile}`);
// Fetch valid IDs from the PostgreSQL database
const result = await pgClient.query('SELECT id FROM "User" WHERE is_registered = TRUE');
const validIds = result.rows.map(row => row.id);
// Filter out invalid IDs from the loaded JSON data
const filteredIds = jsonData.filter(id => validIds.includes(id));
// Find invalid IDs (IDs in JSON that are not in the database)
const invalidIds = jsonData.filter(id => !validIds.includes(id));
// If there are invalid IDs, send a Discord webhook message
if (invalidIds.length > 0) {
const message = invalidIds.map(id => {
return `
** ID : ${id} **
Deleted Submissions!!
`;
}).join('\n');
// Check if webhook has been sent before sending a new one
if (!invalidIdsAlreadyNotified) {
webhookClient.send({
embeds: [{
color: 0xFF0000, // Red color
title: "🗑️ Deleted Submissions",
description: message
}]
});
invalidIdsAlreadyNotified = true; // Mark as notified
}
// Write back the filtered IDs to the JSON file
fs.writeFileSync(lastCheckedUserIdFile, JSON.stringify(filteredIds, null, 2));
} else {
invalidIdsAlreadyNotified = false; // Reset the flag
}
} catch (err) {
console.error('Error validating JSON IDs:', err.message);
}
}