mhsf-dev/src/lib/single.ts

81 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-08-03 09:51:45 -05:00
import { OnlineServer, ServerResponse } from "./types/server";
2024-07-23 18:49:21 -05:00
import toast from "react-hot-toast";
export default class ServerSingle {
private name = "";
private onlineObj: OnlineServer | undefined = undefined;
private offlineObj: ServerResponse | undefined = undefined;
online = false;
constructor(name: string) {
this.name = name;
}
init(): Promise<boolean> {
return new Promise<boolean>((g, bc) => {
fetch("https://api.minehut.com/server/" + this.name + "?byName=true")
.then((d) => {
if (d.ok) {
d.json().then((m) => {
this.online = m.server.online;
this.offlineObj = m.server;
if (this.online == true) {
fetch("https://api.minehut.com/servers").then((l) =>
l.json().then((o) => {
o.servers.forEach((j: OnlineServer) => {
if (j.name == this.name) {
this.onlineObj = j;
g(true);
}
});
2024-08-03 09:51:45 -05:00
})
2024-07-23 18:49:21 -05:00
);
} else g(true);
});
} else {
console.log(
"%c[MHSF] STOP! There was an error while requesting Minehut's API! Heres the fetch object for debugging: ",
"font-weight: bold",
2024-08-03 09:51:45 -05:00
d
2024-07-23 18:49:21 -05:00
);
toast.error(`
Error while grabbing servers from API.
If this is happening alot, make a new issue on GitHub
`);
bc();
}
})
.catch((b) => {
toast.error(`
Error while grabbing servers from API.
If this is happening alot, make a new issue on GitHub
`);
console.log(
"%c[MHSF] STOP! There was an error while requesting Minehut's API! Heres the error for debugging: ",
"font-weight: bold",
2024-08-03 09:51:45 -05:00
b
2024-07-23 18:49:21 -05:00
);
bc();
});
});
}
getAuthor(): string | undefined {
if (this.onlineObj == undefined || this.onlineObj.author == undefined) {
return undefined;
} else {
return this.onlineObj.author;
}
}
grabOnline(): OnlineServer | undefined {
return this.onlineObj;
}
grabOffline(): ServerResponse | undefined {
if (this.offlineObj != undefined) {
this.offlineObj.__unix =
"Time in this file is defined in Unix time. Convert it in something like https://www.epochconverter.com/ (in milliseconds)";
}
return this.offlineObj;
}
}