Stryker-SC/Discord-Bot-VCMaster-JavaScript-Revamped
0
1// Cloudflare Pages Proxy for Discord API2// Save this file as `_worker.js` in the root of your Pages project repository.3// (Cloudflare Pages Advanced Mode requires ESM `export default` syntax instead of `addEventListener`)4 5export default {6 async fetch(request, env, ctx) {7 const url = new URL(request.url);8 9 // Safe path concatenation: only append /api if missing, and clean double slashes10 let cleanPathname = url.pathname.replace(/\/+/g, '/');11 const pathname = cleanPathname.startsWith('/api') ? cleanPathname : `/api${cleanPathname}`;12 const targetUrl = new URL(pathname, 'https://discord.com');13 14 // Preserve any search query parameters15 targetUrl.search = url.search;16 17 // Clean headers to avoid Cloudflare anti-abuse triggers on Discord's end18 const cleanHeaders = new Headers();19 if (request.headers.has('Authorization')) cleanHeaders.set('Authorization', request.headers.get('Authorization'));20 if (request.headers.has('Content-Type')) cleanHeaders.set('Content-Type', request.headers.get('Content-Type'));21 if (request.headers.has('User-Agent')) cleanHeaders.set('User-Agent', request.headers.get('User-Agent'));22 if (request.headers.has('X-Audit-Log-Reason')) cleanHeaders.set('X-Audit-Log-Reason', request.headers.get('X-Audit-Log-Reason'));23 24 // Create a new request using the sanitized headers and target URL25 const proxiedRequest = new Request(targetUrl.toString(), {26 method: request.method,27 headers: cleanHeaders,28 body: request.method !== 'GET' && request.method !== 'HEAD' ? request.body : null,29 redirect: 'manual'30 });31 32 // Fetch the request from Discord and return the unmodified response directly33 return await fetch(proxiedRequest);34 }35};36 