done #4
This commit is contained in:
parent
5601e397c2
commit
fcb6652891
|
@ -3,6 +3,8 @@ import { config } from "dotenv";
|
||||||
config();
|
config();
|
||||||
|
|
||||||
import { getCommands } from "./command/commands.js";
|
import { getCommands } from "./command/commands.js";
|
||||||
|
import mojangAPI from "./wrapper/mojang-api.js";
|
||||||
|
await mojangAPI.load_cache_from_firestore();
|
||||||
|
|
||||||
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
|
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
|
||||||
|
|
||||||
|
@ -33,11 +35,10 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
||||||
content: "There was an error while executing this command!",
|
content: "There was an error while executing this command!",
|
||||||
ephemeral: true,
|
ephemeral: true,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch {
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
content: "There was an error while executing this command!",
|
content: "There was an error while executing this command!",
|
||||||
});
|
});
|
||||||
console.error(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,9 +1,16 @@
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
import TwoWayMap from "../utils/two-way-map.js";
|
import TwoWayMap from "../utils/two-way-map.js";
|
||||||
|
import firebase from "./firebase.js";
|
||||||
|
|
||||||
|
type TCacheData = {
|
||||||
|
uuid: string;
|
||||||
|
id: string;
|
||||||
|
};
|
||||||
|
|
||||||
export class MojangAPI {
|
export class MojangAPI {
|
||||||
private cache: TwoWayMap<string, string> = new TwoWayMap<string, string>(); // uuid, id
|
private cache: TwoWayMap<string, string> = new TwoWayMap<string, string>(); // uuid, id
|
||||||
|
private firestore = firebase.collection("uuidMap");
|
||||||
|
|
||||||
async getIdFromUUID(minecraft_uuid: string): Promise<string> {
|
async getIdFromUUID(minecraft_uuid: string): Promise<string> {
|
||||||
let id = this.cache.get_by_first(minecraft_uuid);
|
let id = this.cache.get_by_first(minecraft_uuid);
|
||||||
|
@ -15,11 +22,16 @@ export class MojangAPI {
|
||||||
id = request.data.name;
|
id = request.data.name;
|
||||||
if (!id) throw new Error("Failed to get minecraft id from api");
|
if (!id) throw new Error("Failed to get minecraft id from api");
|
||||||
|
|
||||||
|
const get_by_id = await this.firestore.where("id", "==", id).get();
|
||||||
|
get_by_id.forEach((doc) => doc.ref.delete());
|
||||||
|
this.firestore.add({ uuid: minecraft_uuid, id: id });
|
||||||
|
|
||||||
this.cache.remove_by_second(id);
|
this.cache.remove_by_second(id);
|
||||||
this.cache.set_by_first(minecraft_uuid, id);
|
this.cache.set_by_first(minecraft_uuid, id);
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
async getUUIDFromId(minecraft_id): Promise<string> {
|
async getUUIDFromId(minecraft_id: string): Promise<string> {
|
||||||
let uuid = this.cache.get_by_second(minecraft_id);
|
let uuid = this.cache.get_by_second(minecraft_id);
|
||||||
if (uuid) return uuid;
|
if (uuid) return uuid;
|
||||||
|
|
||||||
|
@ -29,10 +41,25 @@ export class MojangAPI {
|
||||||
uuid = response.data.id;
|
uuid = response.data.id;
|
||||||
if (!uuid) throw new Error("Failed to get minecraft id from api");
|
if (!uuid) throw new Error("Failed to get minecraft id from api");
|
||||||
|
|
||||||
|
const get_by_uuid = await this.firestore.where("uuid", "==", uuid).get();
|
||||||
|
get_by_uuid.forEach((doc) => doc.ref.delete());
|
||||||
|
this.firestore.add({ uuid: uuid, id: minecraft_id });
|
||||||
|
|
||||||
this.cache.remove_by_first(uuid);
|
this.cache.remove_by_first(uuid);
|
||||||
this.cache.set_by_second(uuid, minecraft_id);
|
this.cache.set_by_second(uuid, minecraft_id);
|
||||||
|
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
async load_cache_from_firestore() {
|
||||||
|
const uuidMap = this.firestore;
|
||||||
|
(await uuidMap.get()).forEach((item) => {
|
||||||
|
const data = item.data() as TCacheData;
|
||||||
|
this.cache.set_by_first(data.uuid, data.id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
get_id_from_uuid_only_cache(minecraft_uuid: string) {
|
||||||
|
return this.cache.get_by_first(minecraft_uuid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const mojangAPI = new MojangAPI();
|
const mojangAPI = new MojangAPI();
|
||||||
export default mojangAPI;
|
export default mojangAPI;
|
||||||
|
|
Loading…
Reference in a new issue