discord-prevnames
Version:
Unofficial Discord module to track and retrieve users' previous usernames and display names by ID .
61 lines (50 loc) • 1.78 kB
JavaScript
const NameApiService = require('./src/services/nameApiService');
const nameEvents = require('./src/events/nameEvents');
const { validateUserId } = require('./src/utils/validators');
class PrevNames {
constructor() {
this.apiService = new NameApiService();
this.events = nameEvents;
this.isListening = false;
this.startListening();
}
startListening() {
if (this.isListening) return;
this.isListening = true;
this.apiService.listenToPrevnamesEvents((data) => {
try {
if (data && data.type === 'displayname') {
this.events.emitNameAdded({
userId: data.userId,
type: data.type,
name: data.name,
changedAt: data.changedAt
});
}
} catch (error) {
this.isListening = false;
this.startListening();
}
});
}
on(eventName, callback) {
if (typeof callback !== 'function') {
throw new Error('Callback must be function');
}
this.events.on(eventName, callback);
return () => this.events.removeListener(eventName, callback);
}
async getUserPrevnames(userId) {
try {
validateUserId(userId);
return await this.apiService.getUserPrevnames(userId);
} catch (error) {
throw new Error(error.message);
}
}
stop() {
this.isListening = false;
this.events.removeAllListeners();
}
}
module.exports = PrevNames;