Skip to content

Commit

Permalink
couple qol stuff
Browse files Browse the repository at this point in the history
- implement `uuidFromDiscord` and  `discordFromUUID` methods.
- v3 is now default when calling internal `townyData` method.
  • Loading branch information
Owen3H committed Dec 17, 2024
1 parent bdcfe24 commit 43aa30b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
26 changes: 24 additions & 2 deletions src/OAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,33 @@ export class OAPIV3 {
static serverInfo = (): Promise<RawServerInfoV3> => townyData('', 'v3')

static location = (...objs: [number, number][]): Promise<RawLocationResponseV3> =>
townyData('location', 'v3', { query: objs })
townyData('/location', 'v3', { query: objs })

static discord = (...objs: DiscordReqObject[]): Promise<DiscordResObject[]> =>
static discord = (...objs: DiscordReqObject[]): Promise<DiscordResObject[]> =>
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<RawQuarterResponseV3> => townyData('/quarters', 'v3', { query: ids })
static quarterList = (): Promise<RawEntityV3[]> => townyData('/quarters', 'v3')

Expand Down
4 changes: 2 additions & 2 deletions src/types/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export type ParsedPlayer = {

export type OnlinePlayer = Prettify<ParsedPlayer & Location>
export type Player = Prettify<Resident & Partial<OnlinePlayer> & {
online: boolean
online: boolean
}>

export type SquaremapOnlinePlayer = Prettify<ParsedPlayer & SquaremapLocation>
export type SquaremapPlayer = Prettify<Resident & Partial<SquaremapOnlinePlayer> & {
online: boolean
online: boolean
}>
23 changes: 11 additions & 12 deletions src/utils/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,20 @@ const mapData = async <T>(mapName: AnyMap): Promise<T> => {
* @param endpoint The endpoint not including the domain, e.g: "lists/nations"
*/
const townyData = async <T>(endpoint = '', version: EndpointVersion = 'v3', body?: V3RequestBody<T>) => {
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 {
Expand Down
11 changes: 3 additions & 8 deletions tests/oapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>(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 () => {
Expand Down

0 comments on commit 43aa30b

Please sign in to comment.