Skip to content

Commit 65af4ef

Browse files
committed
feat: fortnite shop
1 parent 3d25e10 commit 65af4ef

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/tasks/es.fortnite/Shop.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { format } from 'lua-json'
2+
import type { JobsOptions } from 'bullmq'
3+
import { sleep } from '@quority/core'
4+
import { Task } from '../../framework'
5+
import { Time } from '@sapphire/duration'
6+
7+
export class UserTask extends Task {
8+
public static readonly LANGUAGES = [ 'ar', 'de', 'en', 'es', 'es-419', 'fr', 'id', 'it', 'ja', 'ko', 'pl', 'pt-BR', 'ru', 'th', 'tr', 'vi', 'zh-Hans', 'zh-Hant' ]
9+
10+
public override jobOptions: JobsOptions = {
11+
repeat: {
12+
every: Time.Hour * 6,
13+
}
14+
}
15+
16+
public async run(): Promise<void> {
17+
const wiki = UserTask.getFandomWiki( 'es.fortnite' )
18+
const bot = await UserTask.getBot( wiki )
19+
20+
const shops = await this.getShops()
21+
// @ts-expect-error idc
22+
const shopDate = shops.en.data.date as string // eslint-disable-line @typescript-eslint/no-unsafe-member-access
23+
24+
for ( const [ language, shop ] of Object.entries( shops ) ) {
25+
this.logger.info( `Updating shop: ${ shopDate }/${ language }` )
26+
const text = format( shop as object )
27+
28+
await bot.edit( {
29+
text,
30+
title: `Module:Tienda/${ shopDate }/${ language }`
31+
} )
32+
await sleep( 3000 )
33+
}
34+
}
35+
36+
public async getShops() {
37+
const data: Record<string, unknown> = {}
38+
for ( const language of UserTask.LANGUAGES ) {
39+
data[ language ] = await this.getShop( language )
40+
}
41+
return data
42+
}
43+
44+
public async getShop( language: string ) {
45+
const req = await fetch( `https://fortnite-api.com/v2/shop?language=${ language }` )
46+
const result = await req.json() as Record<string, unknown>
47+
this.transformObject( result )
48+
return result
49+
}
50+
51+
public transformObject( obj: unknown ) {
52+
if ( Array.isArray( obj ) ) {
53+
for ( const item of obj ) {
54+
this.transformObject( item )
55+
}
56+
return
57+
}
58+
if ( typeof obj !== 'object' || obj === null ) return
59+
const data = obj as Record<string, unknown>
60+
61+
for ( const key in data ) {
62+
this.transformObject( data[ key ] )
63+
if ( key === key.toLowerCase() ) continue
64+
const newKey = key.replace( /([A-Z])/g, char => `_${ char.toLowerCase() }` )
65+
66+
data[ newKey ] = data[ key ]
67+
delete data[ key ] // eslint-disable-line @typescript-eslint/no-dynamic-delete
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)