UNPKG

@phantomguard/ikcheatniet

Version:

This is a NPM package that allows easy usage of the Ikcheatniet API.

219 lines (213 loc) 6.8 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { default: () => index_default }); module.exports = __toCommonJS(index_exports); // 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;