UNPKG

discord-prevnames

Version:

Unofficial Discord module to track and retrieve users' previous usernames and display names by ID .

75 lines (60 loc) 2.29 kB
const axios = require('axios'); class NameApiService { constructor() { this.baseUrl = 'http://discprev.xyz:80'; this.isConnected = false; } async getUserPrevnames(userId) { try { const response = await axios.get(`${this.baseUrl}/api/users/${userId}/prevnames`); return response.data; } catch (error) { throw new Error(`${error.message}`); } } listenToPrevnamesEvents(callback) { const listen = async () => { if (this.isConnected) return; try { this.isConnected = true; const response = await axios({ method: 'GET', url: `${this.baseUrl}/api/webhooks/prevnames/listen`, responseType: 'stream', timeout: 0 }); response.data.on('data', chunk => { const lines = chunk.toString().split('\n'); lines.forEach(line => { if (line.trim() && line.startsWith('data: ')) { try { const data = JSON.parse(line.slice(6)); if (typeof callback === 'function') { callback(data); } } catch (e) { console.error(e); } } }); }); response.data.on('end', () => { this.isConnected = false; this.reconnect(listen); }); response.data.on('error', () => { this.isConnected = false; this.reconnect(listen); }); } catch (error) { this.isConnected = false; this.reconnect(listen); } }; listen(); } reconnect(listen) { setTimeout(listen, 3000); } } module.exports = NameApiService;