-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
291 lines (256 loc) · 8.55 KB
/
index.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const { Client, Intents } = require('discord.js');
const { exec } = require('child_process');
const fs = require('fs');
const { createCanvas } = require('canvas');
const Chart = require('chart.js');
const { DateTime } = require('luxon');
const path = require('path');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const DISCORD_TOKEN = 'Discord-Token';
const CHANNEL_HOUR_ID = 'Channel-Hour';
const CHANNEL_END_OF_DAY_ID = 'Channel-End-Day';
const HOURLY_DATA_FILE = 'hourlydata.json';
const CHARTS_DIR = './charts';
client.once('ready', async () => {
console.log('Bot is ready');
const channelHour = await client.channels.fetch(CHANNEL_HOUR_ID);
const channelEndOfDay = await client.channels.fetch(CHANNEL_END_OF_DAY_ID);
if (channelHour && channelEndOfDay) {
updateHourlyEmbed(channelHour);
setInterval(() => updateHourlyEmbed(channelHour), 30000);
scheduleEndOfDayLog(channelEndOfDay);
} else {
console.error('Channel not found');
}
});
client.login(DISCORD_TOKEN);
async function updateHourlyEmbed(channel) {
const hourlyData = await getVnstatData();
if (hourlyData.length > 0) {
saveHourlyData(hourlyData);
const chartPath = await generateChart(hourlyData);
await sendOrUpdateDiscordEmbed(channel, hourlyData, chartPath);
} else {
console.error("No data available for the current day.");
}
}
async function scheduleEndOfDayLog(channel) {
const now = new Date();
const midnight = new Date(now);
midnight.setHours(24, 0, 0, 0);
const timeUntilMidnight = midnight - now;
setTimeout(async () => {
const today = new Date().toISOString().split('T')[0];
const hourlyData = loadHourlyData(today);
if (hourlyData.length > 0) {
const chartPath = await generateChart(hourlyData);
await sendEndOfDayLog(channel, hourlyData, chartPath);
} else {
console.error("No data available for the current day.");
}
scheduleEndOfDayLog(channel);
resetHourlyData(today);
}, timeUntilMidnight);
}
async function sendEndOfDayLog(channel, hourlyData, chartPath) {
if (hourlyData.length === 0) {
console.error("No data available for the end of day log.");
return;
}
const maxUsage = hourlyData.reduce((max, item) => parseFloat(item.Total_Data) > parseFloat(max.Total_Data) ? item : max, hourlyData[0]);
const mostUsageTime = `${maxUsage.Time} with ${maxUsage.Total_Data} GB`;
const totalUsage = hourlyData.reduce((sum, item) => sum + parseFloat(item.Total_Data), 0).toFixed(2);
const today = new Date().toISOString().split('T')[0];
const embed = {
title: `📅 End of Day Usage | ${today}`,
description: `🔋 **Most Usage Time**: (${mostUsageTime})\n📊 **Total Usage Today**: ${totalUsage} GB`,
color: '#ffffff',
image: {
url: `attachment://${chartPath}`
},
timestamp: new Date()
};
const files = [{
attachment: chartPath,
name: 'chart.png'
}];
await channel.send({ embeds: [embed], files });
}
function parseVnstatData(data) {
const lines = data.split('\n');
const hourlyData = [];
const iranTime = DateTime.now().setZone('Asia/Tehran');
const today = iranTime.toISODate();
let parsingTodayData = false;
lines.forEach(line => {
const dateMatch = line.match(/^\s*(\d{4}-\d{2}-\d{2})/);
if (dateMatch) {
const currentDate = dateMatch[1];
parsingTodayData = currentDate === today;
} else if (parsingTodayData) {
const match = line.match(/^\s*(\d{2}:\d{2})\s+(\d+\.\d+|\d+)\s+([KMG]i?B)\s+\|\s+(\d+\.\d+|\d+)\s+([KMG]i?B)\s+\|\s+(\d+\.\d+|\d+)\s+([KMG]i?B)/);
if (match) {
const hour = match[1];
const totalValue = parseFloat(match[6]);
const totalUnit = match[7];
const totalGB = convertToGB(totalValue, totalUnit);
hourlyData.push({
Date: today,
Time: hour,
Total_Data: totalGB.toFixed(2)
});
}
}
});
return hourlyData;
}
function convertToGB(value, unit) {
const units = {
B: 1 / (1024 ** 3),
KiB: 1 / (1024 ** 2),
MiB: 1 / 1024,
GiB: 1,
KB: 1 / (1000 ** 3),
MB: 1 / (1000 ** 2),
GB: 1,
};
return value * (units[unit] || 1);
}
async function generateChart(hourlyData) {
const canvas = createCanvas(600, 400);
const ctx = canvas.getContext('2d');
const labels = hourlyData.map(item => item.Time);
const data = hourlyData.map(item => parseFloat(item.Total_Data));
const backgroundColorPlugin = {
id: 'customCanvasBackgroundColor',
beforeDraw: (chart) => {
const ctx = chart.ctx;
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect(0, 0, chart.width, chart.height);
}
};
new Chart(ctx, {
type: 'line',
plugins: [backgroundColorPlugin],
data: {
labels: labels,
datasets: [{
label: 'Total Data (GB)',
data: data,
borderColor: 'rgba(33, 145, 81, 1)',
backgroundColor: 'rgba(33, 145, 81, 0.2)',
fill: true,
tension: 0.1,
pointStyle: 'circle',
pointBackgroundColor: 'rgba(33, 145, 81, 1)',
pointRadius: 5,
pointBorderWidth: 3
}]
},
options: {
scales: {
x: {
title: {
display: true,
text: 'Time',
color: '#ffffff'
},
ticks: {
color: '#ffffff'
}
},
y: {
title: {
display: true,
text: 'Total Data (GB)',
color: '#ffffff'
},
ticks: {
color: '#ffffff',
stepSize: 1,
beginAtZero: true
}
}
},
plugins: {
legend: {
labels: {
color: '#ffffff'
}
}
}
}
});
const chartPath = 'chart.png';
const buffer = canvas.toBuffer('image/png');
fs.writeFileSync(chartPath, buffer);
return chartPath;
}
async function sendOrUpdateDiscordEmbed(channel, hourlyData, chartPath) {
if (hourlyData.length > 0) {
const maxUsage = hourlyData.reduce((max, item) => parseFloat(item.Total_Data) > parseFloat(max.Total_Data) ? item : max, hourlyData[0]);
const mostUsageTime = `${maxUsage.Time} with ${maxUsage.Total_Data} GB`;
const thisHour = hourlyData[hourlyData.length - 1];
const thisHourUsage = `${thisHour.Time} Usage: ${thisHour.Total_Data} GB`;
const totalUsage = hourlyData.reduce((sum, item) => sum + parseFloat(item.Total_Data), 0).toFixed(2);
const totalUsageMessage = `📊 **Total Usage Today**: ${totalUsage} GB`;
const messages = await channel.messages.fetch({ limit: 1 });
const lastMessage = messages.first();
const embed = {
title: `⚠️ Today Usage By Hour | ${thisHour.Time}`,
description: `⏰ **This Hour**: (${thisHourUsage})\n\n🔋 **Most Usage Time**: (${mostUsageTime})\n${totalUsageMessage}`,
color: '#ffffff',
image: {
url: `attachment://${chartPath}`
},
timestamp: new Date()
};
const files = [{
attachment: chartPath,
name: 'chart.png'
}];
if (lastMessage && lastMessage.author.id === client.user.id) {
await lastMessage.edit({ embeds: [embed], files });
} else {
await channel.send({ embeds: [embed], files });
}
} else {
console.error("No data available for the current day.");
}
}
function getVnstatData() {
return new Promise((resolve, reject) => {
exec('vnstat -h', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
reject(error);
}
if (stderr) {
console.error(`stderr: ${stderr}`);
reject(stderr);
}
const hourlyData = parseVnstatData(stdout);
hourlyData.sort((a, b) => a.Time.localeCompare(b.Time));
resolve(hourlyData);
});
});
}
function loadHourlyData(date) {
const filePath = path.join(CHARTS_DIR, `${date}.json`);
if (fs.existsSync(filePath)) {
const rawData = fs.readFileSync(filePath);
return JSON.parse(rawData);
}
return [];
}
function saveHourlyData(data) {
const today = new Date().toISOString().split('T')[0];
const filePath = path.join(CHARTS_DIR, `${today}.json`);
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
}
function resetHourlyData(date) {
const filePath = path.join(CHARTS_DIR, `${date}.json`);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
}