import type { Client } from "./client"; export class API { private readonly base_url: string = 'https://discord.com/api/v10'; constructor(private client: Client) {} async request(method: string, path: string, data?: any): Promise { const url = new URL(path, this.base_url).toString(); const headers = { Authorization: `Bot ${this.client.token}`, 'Accept': 'application/json', 'Content-Type': 'application/json' }; console.log(`API Request: ${method} ${url}`); const response = await fetch(url, { method, headers, body: JSON.stringify(data), }); if (!response.ok) { console.error(`Failed to fetch: ${response.statusText} ${response.url} ${response.status}`); console.error(response.json()); throw new Error(`Failed to fetch: ${response.statusText}`); } const body = await response.json(); // console.log('API Response:', JSON.stringify(body, null, 4)); return body; } async get(path: string): Promise { return this.request('GET', path); } async post(path: string, data: any): Promise { return this.request('POST', path, data); } async patch(path: string, data: any): Promise { return this.request('PATCH', path, data); } async delete(path: string): Promise { return this.request('DELETE', path); } }