Skip to content

Commit 48d4eb4

Browse files
authored
Discord::requestGuildMembers (#1379)
1 parent f6da96b commit 48d4eb4

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

src/Discord/Discord.php

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,16 +1070,13 @@ protected function setupChunking()
10701070
$this->logger->debug('sending chunk with '.count($chunk).' large guilds');
10711071

10721072
foreach ($chunk as $guild_id) {
1073-
$payload = Payload::new(
1074-
Op::OP_GUILD_MEMBER_CHUNK,
1073+
$this->requestGuildMembers(
1074+
$guild_id,
10751075
[
1076-
'guild_id' => $guild_id,
10771076
'query' => '',
10781077
'limit' => 0,
1079-
],
1078+
]
10801079
);
1081-
1082-
$this->send($payload);
10831080
}
10841081
$this->loop->addTimer(1, $sendChunks);
10851082
};
@@ -1137,6 +1134,65 @@ public function connectWs(): void
11371134
});
11381135
}
11391136

1137+
/**
1138+
* Requests guild members from the Discord gateway.
1139+
*
1140+
* @param Guild|string $guild_id ID of the guild or Guild object. Required.
1141+
* @param array $options
1142+
* @param string $options['query'] String that username starts with, or an empty string to return all members. Required when not including user_ids.
1143+
* @param int $options['limit'] Maximum number of members to send matching the query. 0 with empty query returns all. Required when including a query.
1144+
* @param ?bool|null $options['presences'] Whether to include presences of matched members.
1145+
* @param ?string|array|null $options['user_ids'] Snowflake or array of snowflakes to specify which users to fetch. Required when not including a query.
1146+
* @param ?string|null $options['nonce'] Nonce to identify the Guild Members Chunk response.
1147+
*
1148+
* @throws \InvalidArgumentException Either query or user_ids must be set.
1149+
*
1150+
* @since v10.19.0
1151+
*/
1152+
public function requestGuildMembers($guild_id, array $options = []): void
1153+
{
1154+
if (! isset($options['query']) && ! isset($options['user_ids'])) {
1155+
if (! isset($options['limit'])) {
1156+
$options['limit'] = 0;
1157+
}
1158+
$options['query'] = '';
1159+
}
1160+
1161+
if (! is_string($guild_id)) {
1162+
$guild_id = $guild_id->id;
1163+
}
1164+
1165+
$payloadData = [
1166+
'guild_id' => $guild_id,
1167+
];
1168+
1169+
if (isset($options['user_ids'])) {
1170+
// If user_ids is set, query and limit must NOT be set
1171+
$payloadData['user_ids'] = is_array($options['user_ids'])
1172+
? array_values($options['user_ids'])
1173+
: [$options['user_ids']];
1174+
} else {
1175+
// If user_ids is not set, query and limit are required
1176+
$payloadData['query'] = $options['query'] ?? '';
1177+
$payloadData['limit'] = $options['limit'] ?? 0;
1178+
}
1179+
1180+
if (array_key_exists('presences', $options)) {
1181+
$payloadData['presences'] = (bool) $options['presences'];
1182+
}
1183+
1184+
if (isset($options['nonce'])) {
1185+
$payloadData['nonce'] = (string) $options['nonce'];
1186+
}
1187+
1188+
$payload = Payload::new(
1189+
Op::OP_GUILD_MEMBER_CHUNK,
1190+
$payloadData,
1191+
);
1192+
1193+
$this->send($payload);
1194+
}
1195+
11401196
/**
11411197
* Requests soundboard sounds for the specified guilds.
11421198
*

src/Discord/WebSockets/Op.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ class Op
4949
/** Used to redirect clients to a new gateway. */
5050
public const OP_RECONNECT = 7;
5151
/** Used to request member chunks. */
52-
public const OP_GUILD_MEMBER_CHUNK = 8;
52+
public const OP_REQUEST_GUILD_MEMBERS = 8;
53+
/** @deprecated 10.18.31 Use `OP_REQUEST_GUILD_MEMBERS` */
54+
public const OP_GUILD_MEMBER_CHUNK = self::OP_REQUEST_GUILD_MEMBERS;
5355
/** Used to notify clients when they have an invalid session. */
5456
public const OP_INVALID_SESSION = 9;
5557
/** Used to pass through the heartbeat interval. */

0 commit comments

Comments
 (0)