feat(guide): port remaining prs/issues from legacy (#10974)

* chore: remove await wait placeholder

prefer using an explanatory placeholder rather than this artificial
example
original issue: https://github.com/discordjs/guide/issues/1360

* chore: remove implicit grant guide and add disclaimer

issue: https://github.com/discordjs/guide/issues/1370/
pr: https://github.com/discordjs/guide/pull/1543/

* chore(sharding): improve broadcast sample and use of context argument

original PR: https://github.com/discordjs/guide/pull/1624

* feat: add page about setup with proxy

original PR: https://github.com/discordjs/guide/pull/1623

* chore: clarify hiding of commands

original PR: https://github.com/discordjs/guide/pull/1617

* feat(voice): seeking

original PR: https://github.com/discordjs/guide/pull/1483

* chore(oauth2): typo

* chore: align with rest of the guide

remove abstraction layers in ws proxy handling in favour of directly setting globals

* chore: branding over grammar

* Apply suggestions from code review

Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com>

* chore: remove now obsolete example explanation from comments

---------

Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com>
This commit is contained in:
Souji
2025-07-12 02:13:54 +02:00
committed by GitHub
parent 1f0fe39156
commit 591668099e
8 changed files with 162 additions and 64 deletions

View File

@@ -1,3 +1,3 @@
{
"pages": ["async-await", "collections", "es6-syntax", "notation", "rest-api"]
"pages": ["async-await", "collections", "es6-syntax", "notation", "rest-api", "proxy"]
}

View File

@@ -0,0 +1,73 @@
---
title: 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:
```sh tab="npm"
npm install undici global-agent
```
```sh tab="yarn"
yarn add undici global-agent
```
```sh tab="pnpm"
pnpm add undici global-agent
```
```sh tab="bun"
bun add 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:
```js title="index.js" lineNumbers
const { ProxyAgent } = require('undici'); // [!code word:ProxyAgent]
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');
```
<Callout>
For further information on the `undici` `ProxyAgent`, please refer to the [undici
documentation](https://undici.nodejs.org/#/docs/api/ProxyAgent.md).
</Callout>
## 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:
```js title="index.js" lineNumbers
const { ProxyAgent } = require('undici');
const { Client } = require('discord.js');
const { bootstrap } = require('global-agent'); // [!code ++:5]
bootstrap(); // [!code word: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');
```