apacheli / discord-api-libs

List of open-source Discord API Libraries
http://apache.li/discord-api-libs/
Apache License 2.0
144 stars 24 forks source link

how can i use cloudflare to connect #53

Closed 363266400 closed 8 months ago

363266400 commented 1 year ago

When using JDA, I proxy the api address of https://discord.com wss://gateway.discord.gg with cloudflare. When I execute the following method:

JDABuilder builder = JDABuilder.createDefault("token");

// Disable parts of the cache builder.disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE); // Enable the bulk delete event builder.setBulkDeleteSplittingEnabled(false); // Set activity (like "playing Something") builder.setActivity(Activity.watching("TV"));

builder.build();

The console shows Login Successful!, and [ConcurrentSessionController-Worker-0] ERROR SessionControllerAdapter - Failed opening handshake, appending to queue. Message: com.neovisionaries.ws.client.OpeningHandshakeException: The status code of the opening handshake response is not '101 Switching Protocols'. The status line is: HTTP/1.1 500 Internal Server Error

so my bot is still not online. Please help me solve this problem.

363266400 commented 1 year ago

and this is my cloudflare workers config

const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS", "Access-Control-Max-Age": "86400", }

// The URL for the remote third party API you want to fetch from // but does not implement CORS const API_URL = "https://discord.com" const WS_API_URL = "wss://gateway.discord.gg" const API_AUTH_KEY = "i don't know how to get"

// List all of the domains here that you want to be able to access this proxy const ALLOWED_DOMAINS = [ 'wss.wybgptonline.uk','a.wybgptonline.uk' ]

/**

/**

/**

function handleOptions(request) { // Make sure the necessary headers are present // for this to be a valid pre-flight request let headers = request.headers; if ( headers.get("Origin") !== null && headers.get("Access-Control-Request-Method") !== null && headers.get("Access-Control-Request-Headers") !== null ){ // Handle CORS pre-flight request. // If you want to check or reject the requested method + headers // you can do that here. let respHeaders = { ...corsHeaders, // Allow all future content Request headers to go back to browser // such as Authorization (Bearer) or X-Client-Name-Version "Access-Control-Allow-Headers": request.headers.get("Access-Control-Request-Headers"), }

return new Response(null, {
  headers: respHeaders,
})

} else { // Handle standard OPTIONS request. // If you want to allow other HTTP Methods, you can do that here. return new Response(null, { headers: { Allow: "GET, HEAD, POST, OPTIONS", }, }) } }

addEventListener("fetch", event => { const request = event.request

if (request.method === "OPTIONS") { // Handle CORS preflight requests event.respondWith(handleOptions(request)) } else if( request.method === "GET" || request.method === "HEAD" || request.method === "POST" ){ // Handle requests to the API server event.respondWith(handleRequest(request)) } else { event.respondWith( new Response(null, { status: 405, statusText: "Method Not Allowed", }), ) } })

apacheli commented 8 months ago

You may want to ask your question here instead -> https://github.com/discord-jda/JDA