From 43aa30bfd189217bb3bdb26e5b8bd03d0d8273e2 Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 17 Dec 2024 09:31:21 +0000 Subject: [PATCH] couple qol stuff - implement `uuidFromDiscord` and `discordFromUUID` methods. - v3 is now default when calling internal `townyData` method. --- src/OAPI.ts | 26 ++++++++++++++++++++++++-- src/types/player.ts | 4 ++-- src/utils/endpoint.ts | 23 +++++++++++------------ tests/oapi.test.ts | 11 +++-------- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/OAPI.ts b/src/OAPI.ts index 6d9c9b1..8664bd6 100644 --- a/src/OAPI.ts +++ b/src/OAPI.ts @@ -95,11 +95,33 @@ export class OAPIV3 { static serverInfo = (): Promise => townyData('', 'v3') static location = (...objs: [number, number][]): Promise => - townyData('location', 'v3', { query: objs }) + townyData('/location', 'v3', { query: objs }) - static discord = (...objs: DiscordReqObject[]): Promise => + static discord = (...objs: DiscordReqObject[]): Promise => townyData('/discord', 'v3', { query: objs }) + /** + * Same as .discord() but passes only `discord` type for all, returning Minecraft UUIDs. + * @param ids Discord ID string(s). + */ + static uuidFromDiscord = async (...ids: string[]) => { + const objs = ids.map(id => ({ type: 'discord', target: id }) as DiscordReqObject) + const res = await OAPIV3.discord(...objs) + + return res.map(r => r.uuid) + } + + /** + * Same as .discord() but passes only `minecraft` type for all, returning Discord IDs. + * @param ids Minecraft UUID string(s). + */ + static discordFromUUID = async (...uuids: string[]) => { + const objs = uuids.map(uuid => ({ type: 'minecraft', target: uuid }) as DiscordReqObject) + const res = await OAPIV3.discord(...objs) + + return res.map(r => r.id) + } + static quarters = (...ids: string[]): Promise => townyData('/quarters', 'v3', { query: ids }) static quarterList = (): Promise => townyData('/quarters', 'v3') diff --git a/src/types/player.ts b/src/types/player.ts index 376ccf8..348df2c 100644 --- a/src/types/player.ts +++ b/src/types/player.ts @@ -9,10 +9,10 @@ export type ParsedPlayer = { export type OnlinePlayer = Prettify export type Player = Prettify & { - online: boolean + online: boolean }> export type SquaremapOnlinePlayer = Prettify export type SquaremapPlayer = Prettify & { - online: boolean + online: boolean }> \ No newline at end of file diff --git a/src/utils/endpoint.ts b/src/utils/endpoint.ts index f1465ab..3f88a8f 100644 --- a/src/utils/endpoint.ts +++ b/src/utils/endpoint.ts @@ -69,21 +69,20 @@ const mapData = async (mapName: AnyMap): Promise => { * @param endpoint The endpoint not including the domain, e.g: "lists/nations" */ const townyData = async (endpoint = '', version: EndpointVersion = 'v3', body?: V3RequestBody) => { - if (endpoint.startsWith("/")) { - endpoint.replace("/", "") - } - - if (version == "v3") { - const url = get("towny", "v3/aurora") + // if (endpoint.startsWith("/")) { + // endpoint.replace("/", "") + // } - return body ? asJSON(`${url}${endpoint}`, { - method: "POST", - body: JSON.stringify(body) - }) : asJSON(`${url}${endpoint}`) + if (version == "v2") { + const url = get("towny", "v2/aurora") + return asJSON(`${url}${endpoint}?${genRandomString()}`) as unknown } - const url = get("towny", "v2/aurora") - return asJSON(`${url}${endpoint}?${genRandomString()}`) as unknown + const url = get("towny", "v3/aurora") + return body ? asJSON(`${url}${endpoint}`, { + method: "POST", + body: JSON.stringify(body) + }) : asJSON(`${url}${endpoint}`) } export { diff --git a/tests/oapi.test.ts b/tests/oapi.test.ts index 28edf7f..6b875f2 100644 --- a/tests/oapi.test.ts +++ b/tests/oapi.test.ts @@ -26,18 +26,13 @@ describe('[v3] OfficialAPI', async () => { }, 10000) it('can get UUID from Discord ID', async () => { - const res = await OfficialAPI.V3.discord({ - type: "minecraft", - target: "263377802647175170" // My discord ID - }) - - console.log(res) + const res = await OfficialAPI.V3.uuidFromDiscord("394828201215393794") expect(res).toBeDefined() - assertType<{ id: string, uuid: string }[]>(res) + assertType(res) expect(res.length).toBe(1) - expect(res[0].uuid).toBe("d0a2565a-ad93-48d2-8310-81d06e198e53") + expect(res[0]).toBe("b54ee7ae-e1c6-472d-86a2-bc95a1ffc0c2") }) it('can get player list', async () => {