Skip to content

feat: Quality of life changes under the "Eval arguments" header of the Sharding section #1624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions guide/sharding/additional-information.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,22 @@ You can access them later as usual via `process.argv`, which contains an array o

## Eval arguments

There may come the point where you will want to pass arguments from the outer scope into a `.broadcastEval()` call.
There may come the point where you will want to pass arguments from the outer scope into a `.broadcastEval()` call. The `context` property is useful for this purpose:

```js
function funcName(c, { arg }) {
// ...
function funcName(client, context) {
console.log(context.arg);
}

client.shard.broadcastEval(funcName, { context: { arg: 'arg' } });
// Eval on all shards
client.shard.broadcastEval(funcName, {
context: { arg: 'arg' }
});
// Eval on a specific shard
client.shard.broadcastEval(funcName, {
shard: 0,
context: { arg: 'some stuff' }
});
```

The `BroadcastEvalOptions` typedef was introduced in discord.js v13 as the second parameter in `.broadcastEval()`. It accepts two properties: `shard` and `context`. The `context` property will be sent as the second argument to your function.
Expand All @@ -96,3 +104,7 @@ The function will receive the arguments as an object as the second parameter.
The `context` option only accepts properties which are JSON-serializable. This means you cannot pass complex data types in the context directly.
For example, if you sent a `User` instance, the function would receive the raw data object.
:::

::: tip
The [next section](/sharding/extended.md) will go into more detail about various use cases of `.broadcastEval()`
:::