2023-12-04 19:21:33 +01:00
|
|
|
import axios from "axios";
|
2024-08-06 20:21:34 +02:00
|
|
|
import { validateStatus } from "./index.js";
|
2023-12-04 19:21:33 +01:00
|
|
|
|
|
|
|
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) {
|
2024-08-06 20:21:34 +02:00
|
|
|
let req = await axios.get(`${this.URL}/inav/${id}/credits`, { validateStatus });
|
2023-12-07 17:15:38 +01:00
|
|
|
if (req.status == 404) throw new Error("User not Found");
|
2024-08-06 20:21:34 +02:00
|
|
|
return req.data.credits as number;
|
2023-12-07 17:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-09 16:00:08 +01:00
|
|
|
* Sets the credit number for a given ID.
|
2023-12-07 17:15:38 +01:00
|
|
|
*
|
2023-12-09 16:00:08 +01:00
|
|
|
* @param {string|number} id - The ID of the user.
|
|
|
|
* @param {number} number - The new credit number.
|
2023-12-07 17:15:38 +01:00
|
|
|
*/
|
2023-12-09 16:00:08 +01:00
|
|
|
async set(id: string|number, number: number) {
|
2024-08-06 20:21:34 +02:00
|
|
|
let req = await axios.post(
|
|
|
|
`${this.URL}/inav/${id}/credits`,
|
|
|
|
{ number },
|
|
|
|
{ headers: { Authorization: `Bearer ${this.token}`}, validateStatus}
|
|
|
|
);
|
2023-12-09 16:00:08 +01:00
|
|
|
if (req.status == 403) throw new Error("No Permission");
|
2023-12-07 17:15:38 +01:00
|
|
|
if (req.status == 404) throw new Error("User not Found");
|
2023-12-09 14:14:19 +01:00
|
|
|
}
|
2023-12-04 19:21:33 +01:00
|
|
|
}
|