Skip to content

Instantly share code, notes, and snippets.

@devsnek
Created May 11, 2017 15:38
Show Gist options
  • Save devsnek/1f0d1c7a4e913cb5c1f383510b16e6c2 to your computer and use it in GitHub Desktop.
Save devsnek/1f0d1c7a4e913cb5c1f383510b16e6c2 to your computer and use it in GitHub Desktop.
const util = require('util');
const snekfetch = require('snekfetch');
const paramable = ['channels', 'users', 'guilds', 'members'];
const reflectors = ['toString', 'valueOf', 'inspect', Symbol.toPrimitive, util.inspect.custom];
function apiRequest(method, url, options) {
const request = snekfetch[method](url);
if (options.reason) request.set('X-Audit-Log-Reason', options.reason);
if (options.auth !== false) request.set('Authorization', 'token');
if (options.files) {
for (const file of options.files) request.attach(file.name, file.attachment, file.name);
if (typeof options.data !== 'undefined') request.attach('payload_json', JSON.stringify(this.data));
} else if (typeof options.data !== 'undefined') {
request.send(options.data);
}
return request;
}
const handler = {
get: (list, name) => {
if (reflectors.includes(name)) return () => list.join('/');
if (paramable.includes(name)) {
function toReturn(id) { // eslint-disable-line no-inner-declarations
list = list.concat(name);
if (id) list = list.concat(id);
return new Proxy(list, handler);
}
const directJoin = () => `${list.join('/')}/${name}`;
for (const r of reflectors) toReturn[r] = directJoin;
return toReturn;
}
if (name in snekfetch) return (options = {}) => apiRequest(name, list.join('/'), options);
return new Proxy(list.concat(name), handler);
},
};
const api = new Proxy(['https://discordapp.com/api/v7'], handler);
api.gateway.get()
.then((r) => r.body)
.catch((r) => r.body)
.then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment