|
| 1 | +--- |
| 2 | +title: Using a proxy |
| 3 | +--- |
| 4 | + |
| 5 | +It may be necessary to use a proxy when using Discord.js, for example when you are deploying your bot on a server that has a web filtering firewall that only allows outside traffic through the proxy. This guide will show you how to set up a proxy with Discord.js. |
| 6 | + |
| 7 | +A proxy for Discord.js requires 2 separate setups, one for REST and one for WebSocket. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +In order to set up a proxy for Discord.js, you will need to install the following packages: |
| 12 | + |
| 13 | +```sh tab="npm" |
| 14 | +npm install undici global-agent |
| 15 | +``` |
| 16 | + |
| 17 | +```sh tab="yarn" |
| 18 | +yarn add undici global-agent |
| 19 | +``` |
| 20 | + |
| 21 | +```sh tab="pnpm" |
| 22 | +pnpm add undici global-agent |
| 23 | +``` |
| 24 | + |
| 25 | +```sh tab="bun" |
| 26 | +bun add undici global-agent |
| 27 | +``` |
| 28 | + |
| 29 | +## Setting up the proxy for REST calls |
| 30 | + |
| 31 | +To set up a proxy for REST calls, you should provide a custom `ProxyAgent` to the `Client` constructor. The `ProxyAgent` must be imported from the `undici` package, which is the package that `@discordjs/rest` uses to make HTTP requests. |
| 32 | + |
| 33 | +```js |
| 34 | +const { ProxyAgent } = require('undici'); // [!code word:ProxyAgent] |
| 35 | +const { Client } = require('discord.js'); |
| 36 | + |
| 37 | +const myClient = new Client({ |
| 38 | + // other client options |
| 39 | + rest: { |
| 40 | + agent: new ProxyAgent('http://my-proxy-server:port'), |
| 41 | + }, |
| 42 | +}); |
| 43 | +``` |
| 44 | + |
| 45 | +<Callout> |
| 46 | + For further information on the `undici` `ProxyAgent`, please refer to the [undici |
| 47 | + documentation](https://undici.nodejs.org/#/docs/api/ProxyAgent.md). |
| 48 | +</Callout> |
| 49 | + |
| 50 | +## Setting up the proxy for WebSocket |
| 51 | + |
| 52 | +To set up a proxy for WebSocket, you can use the `global-agent` package. You will need to provide some custom configuration to activate it: |
| 53 | + |
| 54 | +```js |
| 55 | +const { bootstrap } = require('global-agent'); // [!code word:bootstrap] |
| 56 | + |
| 57 | +bootstrap(); |
| 58 | + |
| 59 | +class NodeGlobalProxy { |
| 60 | + config = { |
| 61 | + http: '', |
| 62 | + https: '', |
| 63 | + }; |
| 64 | + |
| 65 | + constructor(config) { |
| 66 | + this.config = config; |
| 67 | + } |
| 68 | + |
| 69 | + start() { |
| 70 | + global.GLOBAL_AGENT.HTTP_PROXY = this.config.http; |
| 71 | + global.GLOBAL_AGENT.HTTPS_PROXY = this.config.https; |
| 72 | + } |
| 73 | + |
| 74 | + stop() { |
| 75 | + global.GLOBAL_AGENT.HTTP_PROXY = null; |
| 76 | + global.GLOBAL_AGENT.HTTPS_PROXY = null; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +module.exports = { |
| 81 | + NodeGlobalProxy, |
| 82 | +}; |
| 83 | +``` |
| 84 | + |
| 85 | +Now in the file where you create your client, you can import the `NodeGlobalProxy` class and start it with the proxy URL: |
| 86 | + |
| 87 | +```js |
| 88 | +const { Client } = require('discord.js'); |
| 89 | +const { NodeGlobalProxy } = require('./NodeGlobalProxy'); // [!code word:NodeGlobalProxy] |
| 90 | + |
| 91 | +const proxy = new NodeGlobalProxy({ |
| 92 | + http: 'http://my-proxy-server:port', |
| 93 | + https: 'http://my-proxy-server:port', |
| 94 | +}); |
| 95 | + |
| 96 | +proxy.start(); |
| 97 | + |
| 98 | +const client = new Client({ |
| 99 | + // client options, including the proxy agent as described above |
| 100 | +}); |
| 101 | + |
| 102 | +client.login('your-token-goes-here'); |
| 103 | +``` |
0 commit comments