Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(market): add market module (#44)
* feat(market): add market module * chore: update market module metadata for toolchain * chore: codacy style fixes * chore: delete settings.json * chore: move @types/table to devDependencies * chore: use xiv prefix in module * chore: update readme title * chore: delete settings.json * feat: move utility types into core
- Loading branch information
1 parent
3b9f9c4
commit cb9a8bf
Showing
14 changed files
with
302 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
import { VenatModule } from './module-system/venat-module.decorator'; | ||
import { VenatModuleMetadata } from './module-system/venat-module.metadata'; | ||
import { LookupResult } from './util/io'; | ||
import { cleanText, TextParameter } from './util/text'; | ||
|
||
export { VenatModule, VenatModuleMetadata }; | ||
export { | ||
cleanText, | ||
LookupResult, | ||
TextParameter, | ||
VenatModule, | ||
VenatModuleMetadata, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface LookupResult<TValue> { | ||
value: TValue; | ||
success: boolean; | ||
err?: Error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface TextParameter { | ||
value: string; | ||
} | ||
|
||
/** | ||
* Cleans a text value for use in commands. | ||
* @param value - The value to transform. | ||
* @returns The cleaned text. | ||
*/ | ||
export function cleanText({ value }: TextParameter): string { | ||
const duplicateWhitespace = /(\s\s+)*/gm; | ||
return value.normalize().replaceAll(duplicateWhitespace, '').trim(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# venat-module-xiv-market |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@the-convocation/venat-module-xiv-market", | ||
"version": "0.0.0-semantic-release", | ||
"packageManager": "yarn@3.2.1", | ||
"main": "dist/src/module.js", | ||
"scripts": { | ||
"clean": "rimraf dist", | ||
"build": "yarn clean && tsc" | ||
}, | ||
"dependencies": { | ||
"@the-convocation/venat-core": "workspace:^" | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
modules/venat-module-xiv-market/src/commands/market.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { TransformPipe } from '@discord-nestjs/common'; | ||
import { | ||
Command, | ||
DiscordTransformedCommand, | ||
Payload, | ||
UsePipes, | ||
} from '@discord-nestjs/core'; | ||
import { Logger } from '@nestjs/common'; | ||
import { InteractionReplyOptions, MessageEmbed } from 'discord.js'; | ||
import { getMarketListings } from '../data/universalis'; | ||
import { getItemIdByName } from '../data/xivapi'; | ||
import { table } from 'table'; | ||
import { MarketDto } from '../dto/market.dto'; | ||
|
||
@Command({ | ||
description: 'Look up prices for an item on the market board.', | ||
name: 'market', | ||
}) | ||
@UsePipes(TransformPipe) | ||
export class MarketCommand implements DiscordTransformedCommand<MarketDto> { | ||
private readonly logger: Logger = new Logger('MarketCommand'); | ||
|
||
public async handler( | ||
@Payload() dto: MarketDto, | ||
): Promise<InteractionReplyOptions> { | ||
const itemLookup = await getItemIdByName(dto.item); | ||
if (itemLookup.err != null) { | ||
this.logger.error(itemLookup.err.message, itemLookup.err.stack); | ||
return { | ||
content: 'Failed to access XIVAPI; please try again later.', | ||
}; | ||
} | ||
|
||
if (!itemLookup.success) { | ||
return { | ||
content: | ||
'The item could not be found; please check your spelling and try again.', | ||
}; | ||
} | ||
|
||
const item = itemLookup.value; | ||
const marketLookup = await getMarketListings(item.ID, dto.server); | ||
if (marketLookup.err != null) { | ||
this.logger.error(marketLookup.err.message, marketLookup.err.stack); | ||
return { | ||
content: | ||
'The item could not be found; please check your spelling of the server and try again.', | ||
}; | ||
} | ||
|
||
if (!marketLookup.success) { | ||
return { | ||
content: | ||
'The item could not be found; please check your spelling and try again.', | ||
}; | ||
} | ||
|
||
const { lastUploadTime, listings, worldName, dcName } = marketLookup.value; | ||
const listingsEmbed = new MessageEmbed() | ||
.setTitle(`Cheapest listings for ${item.Name} on ${dcName ?? worldName}`) | ||
.setDescription( | ||
'```' + | ||
table([ | ||
['HQ', 'Unit Price', 'Quantity', 'Total', 'World'], | ||
...listings | ||
.sort((a, b) => a.pricePerUnit - b.pricePerUnit) | ||
.slice(0, 10) | ||
.map((l) => [ | ||
l.hq ? 'Yes' : 'No', | ||
l.pricePerUnit.toLocaleString('en'), | ||
l.quantity.toLocaleString('en'), | ||
l.total.toLocaleString('en'), | ||
worldName ?? l.worldName, | ||
]), | ||
]) + | ||
'```', | ||
) | ||
.setColor('#a58947') | ||
.setFooter({ | ||
text: 'Last updated:', | ||
}) | ||
.setTimestamp(lastUploadTime); | ||
|
||
return { | ||
embeds: [listingsEmbed], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import axios, { AxiosResponse } from 'axios'; | ||
import { LookupResult } from '@the-convocation/venat-core'; | ||
|
||
export interface UniversalisMarketInfo { | ||
lastUploadTime: number; | ||
listings: { | ||
pricePerUnit: number; | ||
quantity: number; | ||
total: number; | ||
hq: boolean; | ||
worldName?: string; | ||
}[]; | ||
|
||
worldName?: string; | ||
dcName?: string; | ||
} | ||
|
||
export async function getMarketListings( | ||
itemId: number, | ||
server: string, | ||
): Promise<LookupResult<UniversalisMarketInfo>> { | ||
let res: AxiosResponse<UniversalisMarketInfo, any>; | ||
try { | ||
res = await axios.get<UniversalisMarketInfo>( | ||
`https://universalis.app/api/${server}/${itemId}`, | ||
); | ||
} catch (err) { | ||
return { value: null, success: false, err }; | ||
} | ||
|
||
return { value: res.data, success: true }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import axios, { AxiosResponse } from 'axios'; | ||
import { LookupResult } from '@the-convocation/venat-core'; | ||
|
||
export interface XIVAPIItem { | ||
ID: number; | ||
Name: string; | ||
} | ||
|
||
export interface XIVAPISearchResponse { | ||
Results: XIVAPIItem[]; | ||
} | ||
|
||
export async function getItemIdByName( | ||
name: string, | ||
): Promise<LookupResult<XIVAPIItem>> { | ||
let res: AxiosResponse<XIVAPISearchResponse, any>; | ||
try { | ||
res = await axios.get<XIVAPISearchResponse>( | ||
`https://xivapi.com/search?string=${name}&filters=ItemSearchCategory.ID>8&columns=ID,Name`, | ||
); | ||
} catch (err) { | ||
return { value: null, success: false, err }; | ||
} | ||
|
||
const nameLower = name.toLowerCase(); | ||
for (const item of res.data.Results) { | ||
if (nameLower === item.Name.toLowerCase()) { | ||
return { value: item, success: true }; | ||
} | ||
} | ||
|
||
return { value: null, success: false }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Param } from '@discord-nestjs/core'; | ||
import { Transform } from 'class-transformer'; | ||
import { cleanText } from '@the-convocation/venat-core'; | ||
|
||
export class MarketDto { | ||
@Transform(cleanText) | ||
@Param({ | ||
description: 'The name of the item to look up prices for.', | ||
name: 'item', | ||
required: true, | ||
}) | ||
public item: string; | ||
|
||
@Transform(cleanText) | ||
@Param({ | ||
description: 'The server to look up prices on.', | ||
name: 'server', | ||
required: true, | ||
}) | ||
public server: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { DiscordModule } from '@discord-nestjs/core'; | ||
import { Logger, Module, OnModuleInit } from '@nestjs/common'; | ||
import { VenatModule } from '@the-convocation/venat-core'; | ||
import { MarketCommand } from './commands/market.command'; | ||
|
||
@VenatModule({ | ||
description: "A module for searching FFXIV's market data.", | ||
name: 'Market', | ||
}) | ||
@Module({ | ||
imports: [DiscordModule.forFeature()], | ||
providers: [MarketCommand], | ||
}) | ||
export class MarketModule implements OnModuleInit { | ||
private readonly logger: Logger = new Logger('MarketModule'); | ||
|
||
public onModuleInit(): void { | ||
this.logger.log('MarketModule loaded!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"rootDir": ".", | ||
"sourceRoot": "src", | ||
"outDir": "./dist" | ||
}, | ||
"references": [{ "path": "../../core" }] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.