This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestManager.js
72 lines (49 loc) · 1.84 KB
/
RestManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const fetch = require('node-fetch')
const Cache = require('./util/Cache')
const encodeJSON = require('./util/encodeJSON')
const Bucket = require('./Bucket')
const RouteBuilder = require('./RouteBuilder')
class RestManager {
constructor (token) {
this.token = token
this.buckets = new Cache(60000)
this.global = null
this.statuses = {}
this.builder = RouteBuilder
}
_key (route) {
const bucket = []
for (let i = 0; i < route.length; i++) {
if (route[i - 1] === 'reactions') break
if (/\d{16,19}/g.test(route[i]) && !/channels|guilds/.test(route[i - 1])) bucket.push(':id')
else bucket.push(route[i])
}
return bucket.join('-')
}
run (method, route, options) {
return new Promise((resolve) => {
const key = this._key(route)
let bucket = this.buckets.get(key)
if (!bucket) {
bucket = new Bucket(key, this)
this.buckets.set(key, bucket)
}
bucket.add({ method, route, options, resolve })
})
}
async request ({ method, route, options }) {
const headers = {}
if (this.token) headers.Authorization = `Bot ${this.token}`
if (options.body) headers['Content-Type'] = 'application/json'
if (options.reason) headers['X-Audit-Log-Reason'] = options.reason
headers['User-Agent'] = 'DiscordBot (JPBBots-Discord-Rest, v1)'
const res = await fetch(`https://discord.com/api/v7/${route.join('/')}${options.query ? `?${encodeJSON(options.query)}` : ''}`, {
method, headers: { ...headers, ...(options.headers || {}) }, body: options.body ? (options.parser || JSON.stringify)(options.body) : null
})
if (!this.statuses[res.status]) this.statuses[res.status] = 0
this.statuses[res.status]++
const json = res.status === 204 ? { success: true } : await res.json()
return { res, json }
}
}
module.exports = RestManager