2023-12-04 19:21:33 +01:00
|
|
|
import axios from "axios";
|
|
|
|
|
|
|
|
export default class CreditsManager {
|
|
|
|
token: string | undefined;
|
|
|
|
URL = "https://api.thundernetwork.org";
|
|
|
|
|
|
|
|
constructor(customURL: string, token: string | undefined) {
|
|
|
|
this.URL = customURL || this.URL;
|
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-07 17:15:38 +01:00
|
|
|
* Retrieves the balance for a given user ID.
|
2023-12-04 19:21:33 +01:00
|
|
|
*
|
2023-12-07 17:15:38 +01:00
|
|
|
* @param {string|number} id - The ID of the user.
|
2023-12-04 19:21:33 +01:00
|
|
|
* @return {Promise<number>} The balance of the user.
|
|
|
|
*/
|
2023-12-07 13:10:33 +01:00
|
|
|
async balance(id: string|number) {
|
2023-12-04 19:21:33 +01:00
|
|
|
let req = await axios.get(`${this.URL}/credits/${id}`);
|
2023-12-07 17:15:38 +01:00
|
|
|
if (req.status == 404) throw new Error("User not Found");
|
|
|
|
return req.data.credits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the ID for the function.
|
|
|
|
*
|
|
|
|
* @param {number} id - The ID to set.
|
|
|
|
* @return {Promise<number>} - The credits of the user.
|
|
|
|
*/
|
|
|
|
async set(id: number) {
|
|
|
|
let req = await axios.post(`${this.URL}/credits/${id}`);
|
|
|
|
if (req.status == 404) throw new Error("User not Found");
|
2023-12-04 19:21:33 +01:00
|
|
|
return req.data.credits;
|
|
|
|
}
|
|
|
|
}
|