matrixbots
Version:
The API for Bots on MatrixBots
85 lines (79 loc) • 2.24 kB
JavaScript
const { baseUrl, version } = require("./config/config.json");
const fetch = require("node-fetch");
const Likes = require("./structure/likes");
const Success = require("./structure/success");
const Bot = require("./structure/bot");
module.exports = class MatrixBots {
/**
* @type {string}
* @private
*/
api_key;
/**
* @param {string} api_key - Bot API Key
*/
constructor(api_key) {
this.api_key = api_key;
}
/**
* Get all Likes
* @returns {Promise<Array<Likes>>}
*/
getLikes() {
return new Promise((resolve, reject) => {
fetch(`${baseUrl}/${version}/bot/likes`, {
method: "GET",
headers: {
Authorization: `Bearer ${this.api_key}`,
},
}).then(async (res) => {
if (res.status !== 200)
return reject(`${res.status}: ${res.statusText}`);
resolve((await res.json()).data.likes);
});
});
}
/**
* Get all Likes
* @param {string} botId
* @returns {Promise<Bot>}
*/
getBot(botId) {
return new Promise((resolve, reject) => {
fetch(`${baseUrl}/${version}/bot/data/${botId}`, {
method: "GET",
}).then(async (res) => {
if (res.status !== 200)
return reject(`${res.status}: ${res.statusText}`);
resolve((await res.json()).data.bot);
});
});
}
/**
* Update the stats for your Bot
* @param {Number} serverCount
* @param {Number} userCount
* @returns {Promise<Success>}
*/
updateStats(serverCount, userCount) {
return new Promise((resolve, reject) => {
let sCount = serverCount || 0;
let uCount = userCount || 0;
fetch(`${baseUrl}/${version}/bot/stats`, {
method: "POST",
headers: {
Authorization: `Bearer ${this.api_key}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
serverCount: sCount,
userCount: uCount,
}),
}).then(async (res) => {
if (res.status !== 200)
return reject(`${res.status}: ${(await res.json()).message}`);
resolve((await res.json()).success);
});
});
}
};