-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (97 loc) · 3.3 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
const TeleBot = require('telebot');
const express = require('express');
const fs = require('fs');
const path = require('path');
const bot = new TeleBot('6488000633:AAHiy6fJrVXl7CVGwjNVj6a8-riuu5p71f8');
const app = express();
const dbPath = path.join(__dirname, 'db', 'listkey.json');
// Function to read and write JSON file
const readJSON = (filePath) => {
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
};
const writeJSON = (filePath, data) => {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
};
// Function to add key
const addKey = (key, duration) => {
const data = readJSON(dbPath);
const now = new Date();
const endTime = new Date(now.getTime() + duration);
data[key] = endTime.toISOString();
writeJSON(dbPath, data);
};
// Function to generate random key
const generateRandomKey = () => {
return 'key_' + Math.random().toString(36).substr(2, 9);
};
// Function to parse duration
const parseDuration = (durationStr) => {
const unit = durationStr.slice(-1);
const value = parseInt(durationStr.slice(0, -1), 10);
const units = {
'd': 86400000, // days
'w': 604800000, // weeks
'm': 2592000000, // months (approx)
'y': 31536000000, // years (approx)
'h': 3600000, // hours
'min': 60000, // minutes
's': 1000 // seconds
};
return value * (units[unit] || 0);
};
// Command /menu
bot.on('/menu', (msg) => {
return bot.sendMessage(msg.chat.id, "Commands:\n/addkey (nama key) (waktu)\n/addrand (waktu)");
});
// Command /addkey
bot.on(/^\/addkey (\w+) (\d+[dwmyhsmin]*)$/, (msg, props) => {
const key = props.match[1];
const durationStr = props.match[2];
const duration = parseDuration(durationStr);
addKey(key, duration);
return bot.sendMessage(msg.chat.id, `Key ${key} added with duration ${durationStr}.\n\nAccess in : http://localhost:3000/status/${key}`);
});
// Command /addrand
bot.on(/^\/addrand (\d+[dwmyhsmin]*)$/, (msg, props) => {
const key = generateRandomKey();
const durationStr = props.match[1];
const duration = parseDuration(durationStr);
addKey(key, duration);
return bot.sendMessage(msg.chat.id, `Random key ${key} added with duration ${durationStr}.\n\nAccess in : http://localhost:3000/status/${key}`);
});
// Endpoint to check key status
app.get('/status/:key', (req, res) => {
const key = req.params.key;
const data = readJSON(dbPath);
if (data[key]) {
const now = new Date();
const endTime = new Date(data[key]);
const timeLeft = endTime - now;
if (timeLeft > 0) {
const days = Math.floor(timeLeft / 86400000);
const hours = Math.floor((timeLeft % 86400000) / 3600000);
const minutes = Math.floor((timeLeft % 3600000) / 60000);
const seconds = Math.floor((timeLeft % 60000) / 1000);
return res.json({
status: true,
time: `${days} Day(s) ${hours} Hour(s) ${minutes} Minute(s) ${seconds} Second(s) Left`
});
} else {
return res.json({
status: false,
time: "Expired"
});
}
} else {
return res.json({
status: false,
time: "Key not found"
});
}
});
// Start the bot and server
bot.start();
const PORT = process.env.PORT || 80;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});