2020-10-22 15:50:47 +00:00
|
|
|
import { Client } from '../models/client.ts'
|
2020-10-24 15:00:42 +00:00
|
|
|
import * as cache from '../models/cache.ts'
|
2020-10-21 16:30:42 +00:00
|
|
|
|
2020-10-22 15:50:47 +00:00
|
|
|
export class Base {
|
|
|
|
client: Client
|
2020-10-24 15:00:42 +00:00
|
|
|
static useCache = true
|
|
|
|
static cacheName: string
|
|
|
|
static cacheArgIndex = 0
|
|
|
|
static restFunc: (...restArgs: string[]) => string
|
|
|
|
|
|
|
|
constructor (client: Client, _data?: any) {
|
2020-10-22 15:50:47 +00:00
|
|
|
this.client = client
|
2020-10-21 16:30:42 +00:00
|
|
|
}
|
2020-10-24 15:00:42 +00:00
|
|
|
|
|
|
|
static async autoInit (client: Client, ...restURLfuncArgs: string[]) {
|
|
|
|
if (this.useCache) {
|
|
|
|
const cached = cache.get(
|
|
|
|
this.cacheName,
|
|
|
|
restURLfuncArgs[this.cacheArgIndex]
|
|
|
|
)
|
|
|
|
if (cached !== undefined && cached instanceof this) {
|
|
|
|
return cached
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const resp = await fetch(this.restFunc(...restURLfuncArgs), {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bot ${client.token}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const jsonParsed = await resp.json()
|
|
|
|
const initialized = new this(client, jsonParsed)
|
|
|
|
cache.set(this.cacheName, restURLfuncArgs[this.cacheArgIndex], initialized)
|
|
|
|
|
|
|
|
return initialized
|
|
|
|
}
|
2020-10-21 16:30:42 +00:00
|
|
|
}
|