From 7c5bce963cc7321f3bb88335a4e6f1de0671a1df Mon Sep 17 00:00:00 2001 From: Skillz Date: Fri, 7 Feb 2020 16:00:07 -0500 Subject: [PATCH] first request to discord api success --- module/Client.ts | 22 ++++++++++++++++++++++ services/RequestHandler.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 module/Client.ts create mode 100644 services/RequestHandler.ts diff --git a/module/Client.ts b/module/Client.ts new file mode 100644 index 000000000..8208bfc68 --- /dev/null +++ b/module/Client.ts @@ -0,0 +1,22 @@ +import { endpoints } from "../constants/discord.ts" +import RequestHandler from "../services/RequestHandler.ts" + +class Client { + /** The bot's token. This should never be used by end users. It is meant to be used internally to make requests to the Discord API. */ + token: string + /** The Rate limit manager to handle all outgoing requests to discord. Not meant to be used by users. */ + RequestHandler: RequestHandler + + constructor(token: string) { + this.token = `Bot ${token}` + this.RequestHandler = new RequestHandler(this, this.token) + } + + async connect() { + // const data = await fetch(endpoints.GATEWAY_BOT).then(res => res.json()) + // console.log(data) + console.log(await this.RequestHandler.get(endpoints.GATEWAY_BOT)) + } +} + +export default Client \ No newline at end of file diff --git a/services/RequestHandler.ts b/services/RequestHandler.ts new file mode 100644 index 000000000..378944cfb --- /dev/null +++ b/services/RequestHandler.ts @@ -0,0 +1,31 @@ +import Client from "../module/Client.ts"; + +class RequestHandler { + client: Client + token: string + + constructor(client: Client, token: string) { + this.client = client + this.token = token + } + + async get(url: string, payload?: unknown) { + // THIS IS IMPORTANT. It keeps clean stack errors in the users own files to better help debug errors. + // const stackHolder = {}; + // TODO: Figure out why this doesnt work + // Error.captureStackTrace(stackHolder) + + // let attempts = 0 + const headers = { + Authorization: this.token, + "User-Agent": `DiscordBot (https://github.com/skillz4killz/discordeno, 0.0.1)`, + } + + console.log('payload', payload) + + const data = await fetch(url, { headers }).then(res => res.json()) + return data + } +} + +export default RequestHandler \ No newline at end of file