UNPKG

botads-api

Version:

Module to earn money on your projects by displaying ads

117 lines (100 loc) 4.23 kB
const fetch = globalThis.fetch || require('node-fetch'); class BotAds { /** * Creates an instance of BotAds. * @param {string} userID - The user ID that will receive ad revenue. * @param {boolean} [debug=false] - Enable debug mode for logging errors. */ constructor(userID, debug = false) { if (!userID || typeof userID !== 'string') { throw new Error("The parameter 'userID' must be a valid string representing the user's ID"); } this.user_id = userID; this.debug = debug; } /** * Retrieves an advertisement link. * @param {string|null} [code=null] - Optional tracking code. * @returns {Promise<{ success: boolean, ad_id?: string, description?: string, redirect_url?: string } | null>} */ async getAdLink(code = null) { try { const baseUrl = new URL("https://bot-ads.ovh/api/system/getlink"); baseUrl.searchParams.append("bot_id", this.user_id); if (code) baseUrl.searchParams.append("click_code", code); const response = await fetch(baseUrl.toString(), { method: 'GET', headers: { 'Content-Type': 'application/json' } }); if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`); const data = await response.json(); return data.success ? data : null; } catch (error) { if (this.debug) console.error("Error fetching the ad link:", error); return null; } } /** * Checks if a user clicked on an ad using a tracking code. * @param {string} code - The tracking code to verify. * @returns {Promise<{ success: boolean, clicked?: boolean } | null>} */ async checkCode(code) { try { if (!code) throw new Error("The parameter 'code' is required"); const response = await fetch(`https://bot-ads.ovh/api/public/checkcode?code=${code}&bot_id=${this.user_id}`, { method: 'GET', headers: { 'Content-Type': 'application/json' } }); if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`); const data = await response.json(); return data.success ? data : null; } catch (error) { if (this.debug) console.error("Error checking the code:", error); return null; } } /** * Generates an embed containing the advertisement. * @param {number} [color=0x0099FF] - Optional embed color (default: 0x0099FF). * @param {string|null} [code=null] - Optional tracking code. * @returns {Promise<{ success: boolean, link?: string, embed?: object, message?: string }>} */ async generateAdEmbed(color = 0x0099FF, code = null) { try { const adLink = await this.getAdLink(code); if (!adLink) { return { success: false, message: 'Unable to retrieve the ad link.' }; } const embedString = { title: 'Advertisement', description: `[${adLink.description || 'Click here'}](${adLink.redirect_url})`, url: adLink.redirect_url, color: color, footer: { text: 'Bot-Ads.ovh' } }; return { success: true, link: adLink.redirect_url, embed: embedString }; } catch (error) { if (this.debug) console.error("Error generating the embed:", error); return { success: false, message: "Error generating the embed" }; } } /** * Gets the bot's user ID. * @returns {string} The bot user ID. */ getUserId() { return this.user_id; } /** * Enables or disables debug mode. * @param {boolean} value - True to enable debug mode, false to disable it. */ setDebug(value) { if (typeof value !== "boolean") { throw new Error("Debug mode must be a boolean value."); } this.debug = value; } } module.exports = BotAds;