discord-prevname
Version:
A discord prevname api by Mxtorie
83 lines (80 loc) • 3.75 kB
JavaScript
const got = require('custom-got')
const URL = require('url')
module.exports = class MxtorieAPI {
constructor(authorization) {
this.authorization = authorization
}
/**
* Get the available balance on this authorization
* @returns {Promise<number>} return the remaining credit on the used authorization (float)
*/
async getBalance() {
return new Promise(async (resolve, reject) => {
let callOptions = {}
callOptions = URL.parse("http://mxtorie.xyz/api/balance")
callOptions.method = "GET"
callOptions.headers = {
'authorization': this.authorization,
'use_x_forwarded_for': true
}
callOptions.use_x_forwarded_for = true
callOptions.retries = 2
got(`http://mxtorie.xyz/api/balance`, callOptions).then(result => {
const body = JSON.parse(result.body)
if (body.code === 200) return resolve(body)
else return reject(new Error(JSON.stringify(body)))
}).catch(e => {
try {
return reject(new Error(`Code: ${e.body.code}\nName: ${e.body.name}\nMessage: ${e.body.message}`))
} catch (err) {
try {
return reject(new Error(`Code: ${e.code}\nName: ${e.name}\nMessage: ${e.message}`))
} catch (errr) {
return reject(new Error(e))
}
}
})
})
}
/**
* @typedef {object} prevname
* @property {string} old the old name of the user
* @property {string} new the new name of the user
* @property {string} date the date of the changement
* @property {boolean} dateFormat if true the date is like : "<t:timestamp>"" else it's a full date like : "20/12/2021 16:56"
*/
/**
* Fetch and return from the API all registred previous names of a user
* @param {string} id the Id of the user
* @param {boolean} [duplicated=true] if true the api will return all names in the database duplicated included, default set to true
* @returns {Promise<Array<prevname>>} this array can be empty if no data is registred on the user
*/
async fetchNames(id, duplicated = true) {
return new Promise(async (resolve, reject) => {
let callOptions = {}
callOptions = URL.parse(`http://mxtorie.xyz/api/prevname/${id}?duplicated=${duplicated ? 'true' : 'false'}`) //
callOptions.method = "GET"
callOptions.headers = {
'authorization': this.authorization,
'use_x_forwarded_for': true
}
callOptions.use_x_forwarded_for = true
callOptions.retries = 2
got(`http://mxtorie.xyz/api/prevname/${id}?duplicated=${duplicated ? 'true' : 'false'}`, callOptions).then(result => { //?duplicated=${duplicated ? 'true' : 'false'}
const body = JSON.parse(result.body)
if (body.code === 200) return resolve(body)
return reject(new Error(JSON.stringify(body)))
}).catch(e => {
try {
return reject(new Error(`Code: ${e.body.code}\nName: ${e.body.name}\nMessage: ${e.body.message}`))
} catch (err) {
try {
return reject(new Error(`Code: ${e.code}\nName: ${e.name}\nMessage: ${e.message}`))
} catch (errr) {
return reject(new Error(e))
}
}
})
})
}
}