doctor8296
doctor8296
DIAdiscord.js - Imagine an app
Created by doctor8296 on 4/5/2025 in #djs-questions
What is the proper way to use discord.js with proxy server?
I want to run my Discord bot using a different IP address for both HTTP (REST API) and WebSocket (WSS) connections — so that all outbound traffic from the bot is routed through a specific proxy. So far I have such simple configuration:
import "dotenv/config";
import { setGlobalDispatcher, ProxyAgent } from "undici";
import { Client, GatewayIntentBits } from "discord.js";

setGlobalDispatcher(new ProxyAgent(process.env.PROXY_URL!));

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});

client.once("ready", () => {
console.log(`Logged in as ${client.user?.tag}`);
});

client.login(process.env.DISCORD_TOKEN);
import "dotenv/config";
import { setGlobalDispatcher, ProxyAgent } from "undici";
import { Client, GatewayIntentBits } from "discord.js";

setGlobalDispatcher(new ProxyAgent(process.env.PROXY_URL!));

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});

client.once("ready", () => {
console.log(`Logged in as ${client.user?.tag}`);
});

client.login(process.env.DISCORD_TOKEN);
But it gives me timeout error (or doesn't output anything):
i113861615:cards doctor8296$ npx ts-node src/bot.ts
/Users/doctor8296/cards/node_modules/ws/lib/websocket.js:873
abortHandshake(websocket, req, 'Opening handshake has timed out');
^
Error: Opening handshake has timed out
at ClientRequest.<anonymous> (/Users/doctor8296/cards/node_modules/ws/lib/websocket.js:873:7)
at ClientRequest.emit (node:events:518:28)
at ClientRequest.emit (node:domain:489:12)
at TLSSocket.emitRequestTimeout (node:_http_client:851:9)
at Object.onceWrapper (node:events:632:28)
at TLSSocket.emit (node:events:518:28)
at TLSSocket.emit (node:domain:489:12)
at TLSSocket.Socket._onTimeout (node:net:595:8)
at listOnTimeout (node:internal/timers:594:17)
at processTimers (node:internal/timers:529:7)
i113861615:cards doctor8296$ npx ts-node src/bot.ts
/Users/doctor8296/cards/node_modules/ws/lib/websocket.js:873
abortHandshake(websocket, req, 'Opening handshake has timed out');
^
Error: Opening handshake has timed out
at ClientRequest.<anonymous> (/Users/doctor8296/cards/node_modules/ws/lib/websocket.js:873:7)
at ClientRequest.emit (node:events:518:28)
at ClientRequest.emit (node:domain:489:12)
at TLSSocket.emitRequestTimeout (node:_http_client:851:9)
at Object.onceWrapper (node:events:632:28)
at TLSSocket.emit (node:events:518:28)
at TLSSocket.emit (node:domain:489:12)
at TLSSocket.Socket._onTimeout (node:net:595:8)
at listOnTimeout (node:internal/timers:594:17)
at processTimers (node:internal/timers:529:7)
node version: v22.11.0 discord.js: ^14.18.0, My proxy has http(s) type. It is indeed working. I have successfully requested main discord page through my proxy with https.
7 replies