Using a proxy
This guide will show you how to set up a proxy with discord.js. This may be necessary if you are deploying your bot to a server with a firewall only allowing outside traffic through the proxy.
Proxying discord.js requires two components: a REST proxy and a WebSocket proxy.
Prerequisites
To achieve these two components you can utilize the undici
and global-agent
packages:
npm install undici global-agent
Setting up the proxy for REST calls
The @discordjs/rest
package handling HTTP requests in discord.js uses the undici
package. Accordingly, you can provide a custom ProxyAgent
configuration to the client constructor:
const { ProxyAgent } = require('undici');
const { Client } = require('discord.js');
const client = new Client({
// ...other client options
rest: {
agent: new ProxyAgent('http://my-proxy-server:port'),
},
});
client.login('your-token-goes-here');
For further information on the undici
ProxyAgent
, please refer to the undici
documentation.
Setting up the proxy for the WebSocket connection
To set up a proxy for WebSocket, you can use the global-agent
package. You will need to import and call the bootstrap()
function and set the required GLOBAL_AGENT
globals as shown below:
const { ProxyAgent } = require('undici');
const { Client } = require('discord.js');
const { bootstrap } = require('global-agent');
bootstrap();
global.GLOBAL_AGENT.HTTP_PROXY = 'http://my-proxy-server:port';
global.GLOBAL_AGENT.HTTPS_PROXY = 'https://my-proxy-server:port';
const client = new Client({
// ...other client options
rest: {
agent: new ProxyAgent('http://my-proxy-server:port'),
},
});
client.login('your-token-goes-here');