UNPKG

aifordiscord-api

Version:

An advanced npm package for Discord bots providing AI-enhanced random content, memes, jokes, and utilities with comprehensive documentation.

271 lines 10.3 kB
"use strict"; /** * External API integration service */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.APIService = void 0; const axios_1 = __importDefault(require("axios")); class APIService { constructor() { this.baseTimeout = 10000; // 10 seconds } async fetchMeme() { try { const response = await axios_1.default.get('https://meme-api.herokuapp.com/gimme', { timeout: this.baseTimeout, params: { _t: Date.now() // Cache busting parameter } }); return response.data; } catch (error) { throw new Error(`Failed to fetch meme: ${error instanceof Error ? error.message : String(error)}`); } } async fetchAdvice() { try { const response = await axios_1.default.get('https://api.adviceslip.com/advice', { timeout: this.baseTimeout, params: { _t: Date.now() // Cache busting parameter } }); return response.data; } catch (error) { throw new Error(`Failed to fetch advice: ${error instanceof Error ? error.message : String(error)}`); } } async fetchNeko() { try { const response = await axios_1.default.get('https://nekos.life/api/v2/img/neko', { timeout: this.baseTimeout, params: { _t: Date.now() // Cache busting parameter } }); return response.data; } catch (error) { throw new Error(`Failed to fetch neko image: ${error instanceof Error ? error.message : String(error)}`); } } async fetchJoke() { try { const response = await axios_1.default.get('https://v2.jokeapi.dev/joke/Any?safe-mode&type=single,twopart', { timeout: this.baseTimeout, params: { _t: Date.now() // Cache busting parameter } }); return response.data; } catch (error) { throw new Error(`Failed to fetch joke: ${error instanceof Error ? error.message : String(error)}`); } } async fetchAnimeImage(type) { const validTypes = ['pat', 'hug', 'waifu', 'cry', 'kiss', 'slap', 'smug', 'punch']; if (!validTypes.includes(type)) { throw new Error(`Invalid anime image type. Valid types: ${validTypes.join(', ')}`); } try { const response = await axios_1.default.get(`https://nekos.life/api/v2/img/${type}`, { timeout: this.baseTimeout }); return response.data; } catch (error) { throw new Error(`Failed to fetch anime image: ${error instanceof Error ? error.message : String(error)}`); } } async fetchFact() { try { const response = await axios_1.default.get('https://uselessfacts.jsph.pl/random.json?language=en', { timeout: this.baseTimeout }); return response.data; } catch (error) { throw new Error(`Failed to fetch fact: ${error instanceof Error ? error.message : String(error)}`); } } async fetchNPMInfo(packageName) { if (!packageName || typeof packageName !== 'string') { throw new Error('Package name is required and must be a string'); } try { const response = await axios_1.default.get(`https://registry.npmjs.org/${encodeURIComponent(packageName)}`, { timeout: this.baseTimeout }); return response.data; } catch (error) { if (error.response?.status === 404) { throw new Error(`Package "${packageName}" not found on npm registry`); } throw new Error(`Failed to fetch npm info: ${error instanceof Error ? error.message : String(error)}`); } } async fetchQuote() { try { const response = await axios_1.default.get('https://api.quotable.io/random', { timeout: this.baseTimeout }); return response.data; } catch (error) { throw new Error(`Failed to fetch quote: ${error instanceof Error ? error.message : String(error)}`); } } async fetchDogImage() { try { const response = await axios_1.default.get('https://dog.ceo/api/breeds/image/random', { timeout: this.baseTimeout }); return response.data; } catch (error) { throw new Error(`Failed to fetch dog image: ${error instanceof Error ? error.message : String(error)}`); } } async fetchCatImage() { try { const response = await axios_1.default.get('https://api.thecatapi.com/v1/images/search', { timeout: this.baseTimeout }); return response.data[0]; } catch (error) { throw new Error(`Failed to fetch cat image: ${error instanceof Error ? error.message : String(error)}`); } } async fetchTrivia(category, difficulty) { try { let url = 'https://opentdb.com/api.php?amount=1&type=multiple'; if (category) url += `&category=${encodeURIComponent(category)}`; if (difficulty) url += `&difficulty=${encodeURIComponent(difficulty)}`; const response = await axios_1.default.get(url, { timeout: this.baseTimeout }); return response.data; } catch (error) { throw new Error(`Failed to fetch trivia: ${error instanceof Error ? error.message : String(error)}`); } } async fetchRandomColor() { try { const response = await axios_1.default.get('https://www.colr.org/json/color/random', { timeout: this.baseTimeout }); return response.data; } catch (error) { throw new Error(`Failed to fetch random color: ${error instanceof Error ? error.message : String(error)}`); } } async fetchGitHubUser(username) { if (!username || typeof username !== 'string') { throw new Error('Username is required and must be a string'); } try { const response = await axios_1.default.get(`https://api.github.com/users/${encodeURIComponent(username)}`, { timeout: this.baseTimeout }); return response.data; } catch (error) { if (error.response?.status === 404) { throw new Error(`User "${username}" not found on GitHub`); } throw new Error(`Failed to fetch GitHub user: ${error instanceof Error ? error.message : String(error)}`); } } async fetchInsult() { try { const response = await axios_1.default.get('https://evilinsult.com/generate_insult.php?lang=en&type=json', { timeout: this.baseTimeout }); return response.data; } catch (error) { throw new Error(`Failed to fetch insult: ${error instanceof Error ? error.message : String(error)}`); } } async fetchCompliment() { try { const response = await axios_1.default.get('https://complimentr.com/api', { timeout: this.baseTimeout }); return response.data; } catch (error) { // Fallback to a simple compliment generator const compliments = [ "You're an amazing person!", "You bring out the best in other people.", "You're a gift to those around you.", "You're a smart cookie.", "You are awesome!", "You have impeccable manners.", "You're incredibly thoughtful.", "You're a great listener." ]; return { compliment: compliments[Math.floor(Math.random() * compliments.length)] }; } } async fetchAffirmation() { try { const response = await axios_1.default.get('https://www.affirmations.dev/', { timeout: this.baseTimeout }); return response.data; } catch (error) { // Fallback affirmations const affirmations = [ "I am capable of amazing things.", "I choose to be happy today.", "I am worthy of love and respect.", "I trust myself to make good decisions.", "I am becoming the best version of myself.", "I have the power to create change.", "I am grateful for this moment.", "I believe in my abilities." ]; return { affirmation: affirmations[Math.floor(Math.random() * affirmations.length)] }; } } async fetchDadJoke() { try { const response = await axios_1.default.get('https://icanhazdadjoke.com/', { headers: { 'Accept': 'application/json' }, timeout: this.baseTimeout }); return response.data; } catch (error) { throw new Error(`Failed to fetch dad joke: ${error instanceof Error ? error.message : String(error)}`); } } async fetchChuckNorrisJoke() { try { const response = await axios_1.default.get('https://api.chucknorris.io/jokes/random', { timeout: this.baseTimeout }); return response.data; } catch (error) { throw new Error(`Failed to fetch Chuck Norris joke: ${error instanceof Error ? error.message : String(error)}`); } } } exports.APIService = APIService; //# sourceMappingURL=api.js.map