Spaces:
Runtime error
Runtime error
Create premiumStore.js
Browse files- premiumStore.js +104 -0
premiumStore.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use strict";
|
| 2 |
+
const fs = require("fs");
|
| 3 |
+
const path = require("path");
|
| 4 |
+
|
| 5 |
+
const dataPath = path.resolve(__dirname, "data", "premium.json");
|
| 6 |
+
const dir = path.dirname(dataPath);
|
| 7 |
+
|
| 8 |
+
let data = { premiumUsers: {}, premiumGuilds: {} };
|
| 9 |
+
|
| 10 |
+
function ensureData() {
|
| 11 |
+
try {
|
| 12 |
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
| 13 |
+
if (!fs.existsSync(dataPath)) fs.writeFileSync(dataPath, JSON.stringify(data, null, 2), "utf8");
|
| 14 |
+
const raw = fs.readFileSync(dataPath, "utf8");
|
| 15 |
+
data = JSON.parse(raw);
|
| 16 |
+
if (!data.premiumUsers) data.premiumUsers = {};
|
| 17 |
+
if (!data.premiumGuilds) data.premiumGuilds = {};
|
| 18 |
+
} catch (e) {
|
| 19 |
+
console.error("[PREMIUM] Failed to load premium.json:", e.message);
|
| 20 |
+
data = { premiumUsers: {}, premiumGuilds: {} };
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
function save() {
|
| 25 |
+
try {
|
| 26 |
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
| 27 |
+
fs.writeFileSync(dataPath, JSON.stringify(data, null, 2), "utf8");
|
| 28 |
+
} catch (e) {
|
| 29 |
+
console.error("[PREMIUM] Failed to save premium.json:", e.message);
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
function isPremium(userId) {
|
| 34 |
+
ensureData();
|
| 35 |
+
const record = data.premiumUsers[userId];
|
| 36 |
+
if (!record) return false;
|
| 37 |
+
// Check expiry (null = never expires, only league winners)
|
| 38 |
+
if (record.expiresAt !== null && Date.now() > record.expiresAt) {
|
| 39 |
+
delete data.premiumUsers[userId];
|
| 40 |
+
save();
|
| 41 |
+
return false;
|
| 42 |
+
}
|
| 43 |
+
return true;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
function isPremiumGuild(guildId) {
|
| 47 |
+
ensureData();
|
| 48 |
+
const record = data.premiumGuilds[guildId];
|
| 49 |
+
if (!record) return false;
|
| 50 |
+
if (record.expiresAt !== null && Date.now() > record.expiresAt) {
|
| 51 |
+
delete data.premiumGuilds[guildId];
|
| 52 |
+
save();
|
| 53 |
+
return false;
|
| 54 |
+
}
|
| 55 |
+
return true;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
function grantUser(userId, reason = "manual", expiresAt = null) {
|
| 59 |
+
ensureData();
|
| 60 |
+
data.premiumUsers[userId] = { grantedAt: Date.now(), reason, expiresAt };
|
| 61 |
+
save();
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
function revokeUser(userId) {
|
| 65 |
+
ensureData();
|
| 66 |
+
delete data.premiumUsers[userId];
|
| 67 |
+
save();
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
function grantGuild(guildId, reason = "manual", expiresAt = null) {
|
| 71 |
+
ensureData();
|
| 72 |
+
data.premiumGuilds[guildId] = { grantedAt: Date.now(), reason, expiresAt };
|
| 73 |
+
save();
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
function revokeGuild(guildId) {
|
| 77 |
+
ensureData();
|
| 78 |
+
delete data.premiumGuilds[guildId];
|
| 79 |
+
save();
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
function getAllPremiumUsers() {
|
| 83 |
+
ensureData();
|
| 84 |
+
return { ...data.premiumUsers };
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
function getAllPremiumGuilds() {
|
| 88 |
+
ensureData();
|
| 89 |
+
return { ...data.premiumGuilds };
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// Load on init
|
| 93 |
+
ensureData();
|
| 94 |
+
|
| 95 |
+
module.exports = {
|
| 96 |
+
isPremium,
|
| 97 |
+
isPremiumGuild,
|
| 98 |
+
grantUser,
|
| 99 |
+
revokeUser,
|
| 100 |
+
grantGuild,
|
| 101 |
+
revokeGuild,
|
| 102 |
+
getAllPremiumUsers,
|
| 103 |
+
getAllPremiumGuilds,
|
| 104 |
+
};
|