@phantomguard/ikcheatniet
Version:
This is a NPM package that allows easy usage of the Ikcheatniet API.
186 lines (182 loc) • 5.3 kB
JavaScript
// src/lib/requests.ts
var fetchFn;
if (typeof fetch === "function") {
fetchFn = fetch;
} else {
fetchFn = async (...args) => {
const mod = await import("node-fetch");
const [input, init] = args;
const url = typeof input === "string" || input instanceof URL ? input : input.url;
return mod.default(url, init);
};
}
async function request(url, options) {
return fetchFn(url, options);
}
// src/lib/reputation.ts
var IkcheatnietReputation = class {
_reputation;
constructor(user) {
if (!user || !user.entries) {
throw new Error("Invalid user data");
}
this._reputation = this.calculateReputation(user.entries);
}
calculateReputation(entries) {
const penalty = entries.length * 10;
return Math.max(0, 100 - penalty);
}
get reputation() {
return this._reputation;
}
get reputationLevel() {
if (this._reputation === 100) return "Clean";
if (this._reputation >= 80) return "Suspicious";
if (this._reputation >= 50) return "Untrusted";
return "Cheater";
}
};
var reputation_default = IkcheatnietReputation;
// src/lib/cache.ts
var Cache = class {
constructor(defaultTTL = 6e4) {
this.defaultTTL = defaultTTL;
}
cache = /* @__PURE__ */ new Map();
set(key, value, ttl) {
const expiry = Date.now() + (ttl || this.defaultTTL);
this.cache.set(key, { value, expiry });
}
get(key) {
const entry = this.cache.get(key);
if (!entry) return null;
if (Date.now() > entry.expiry) {
this.cache.delete(key);
return null;
}
return entry.value;
}
delete(key) {
this.cache.delete(key);
}
clear() {
this.cache.clear();
}
};
var cache_default = Cache;
// src/index.ts
var Ikcheatniet = class _Ikcheatniet {
baseUrl = "";
apiKey = "";
cache;
constructor() {
this.cache = new cache_default();
}
static init(apiKey, baseUrl, cacheTTL) {
const instance = new _Ikcheatniet();
instance.baseUrl = baseUrl || "https://ikcheatniet.phantomguard.eu";
instance.apiKey = apiKey;
instance.cache = new cache_default(cacheTTL || 6e4);
return instance;
}
get stats() {
const cacheKey = "stats";
const cachedStats = this.cache.get(cacheKey);
if (cachedStats) {
return Promise.resolve(cachedStats);
}
const url = `${this.baseUrl}/stats`;
return request(url, {
method: "GET",
headers: {
"Authorization": `${this.apiKey}`,
"Content-Type": "application/json"
}
}).then((response) => {
if (!response.ok) {
const statusCode = response.status;
switch (statusCode) {
case 401:
throw new Error("Unauthorized: Invalid API key.");
default:
throw new Error(`Error: ${response.statusText} (Status Code: ${statusCode})`);
}
}
return response.json();
}).then((data) => {
if (!data || !data.last_modified || !data.total_entries || !data.total_size) {
throw new Error("Invalid response from the API.");
}
const stats = {
last_modified: data.last_modified,
total_users: data.total_entries,
total_file_length: data.total_size
};
this.cache.set(cacheKey, stats);
return stats;
}).catch((error) => {
throw new Error(`Failed to fetch stats: ${error.message}`);
});
}
async searchUser(discordId, options) {
const cacheKey = `searchUser:${discordId}:${options?.type}:${options?.searchType}`;
const cachedResponse = this.cache.get(cacheKey);
if (cachedResponse) {
return Promise.resolve(cachedResponse);
}
if (!this.apiKey) {
throw new Error("API key is not set. Please initialize the class with a valid API key.");
}
if (!this.baseUrl) {
throw new Error("Base URL is not set. Please initialize first.");
}
if (!discordId) {
throw new Error("Discord ID is required.");
}
const url = `${this.baseUrl}/lookup`;
const type = options?.type || "cheater";
const searchType = options?.searchType || "single";
const response = await request(url, {
method: "POST",
headers: {
"Authorization": `${this.apiKey}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"type": type,
"searchType": searchType,
"id": discordId
})
});
if (!response.ok) {
const statusCode = response.status;
switch (statusCode) {
case 401:
throw new Error("Unauthorized: Invalid API key.");
default:
throw new Error(`Error: ${response.statusText} (Status Code: ${statusCode})`);
}
}
const result = await response.json();
this.cache.set(cacheKey, result);
return result;
}
async getUserReputation(user) {
let RepUser = user;
if (typeof user === "string") {
RepUser = await this.searchUser(user, { type: "cheater", searchType: "single" });
}
if (!RepUser || !RepUser.entries) {
throw new Error("Invalid response from the API.");
}
const reputation = new reputation_default(RepUser);
if (!reputation) {
throw new Error("Failed to create reputation instance.");
}
return reputation;
}
};
var index_default = Ikcheatniet;
export {
index_default as default
};