Files
discordeno/helpers/members/fetchMembers.ts
Skillz4Killz a0a1554756 refactor: typings using ReturnType (#2105)
* fix: check new types idea

* fix: type errors

* fix: new style

* fix: more cleanup

* fix: more cleanup

* fix: cleanup audit logs

* fix: cleanup stickers

* fix: cleanup integrations

* fix: more cleanup

* fix: organize into 1 place

* fix: few errors

* fix: some broken import fixes

* fix: quite a lot of fixes across the board

* fix: more fixes for broken imports

* fix: more fixes for broken imports

* fix: handler imports

* fix: all remaining import errors

* fix: more errors needing fixes

* fix: clearing up transformers

* fix: few moer types

* fix: more cleanup of extra types

* fix: fmt

* fix: cleanup discordeno file

* Nuke Base Types (#2102)

* fix: cleanup snake stuff

* convert camelCase to snake_case (#2103)

* fix: add camelize

* fix: finalize remaining errors

* fix: imports in test

Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com>
2022-03-14 22:11:22 -04:00

59 lines
2.2 KiB
TypeScript

import type { Bot } from "../../bot.ts";
import { GatewayIntents, GatewayOpcodes } from "../../types/shared.ts";
/**
* Highly recommended to use this function to fetch members instead of getMember from REST.
* REST: 50/s global(across all shards) rate limit with ALL requests this included
* GW(this function): 120/m(PER shard) rate limit. Meaning if you have 8 shards your limit is now 960/m.
*/
export function fetchMembers(
bot: Bot,
guildId: bigint,
shardId: number,
options?: Omit<RequestGuildMembers, "guildId">,
) {
// You can request 1 member without the intent
// Check if intents is not 0 as proxy ws won't set intents in other instances
if (bot.intents && (!options?.limit || options.limit > 1) && !(bot.intents & GatewayIntents.GuildMembers)) {
throw new Error(bot.constants.Errors.MISSING_INTENT_GUILD_MEMBERS);
}
if (options?.userIds?.length) {
options.limit = options.userIds.length;
}
return new Promise((resolve) => {
const nonce = `${guildId}-${Date.now()}`;
bot.cache.fetchAllMembersProcessingRequests.set(nonce, resolve);
bot.gateway.sendShardMessage(bot.gateway, shardId, {
op: GatewayOpcodes.RequestGuildMembers,
d: {
guild_id: guildId.toString(),
// If a query is provided use it, OR if a limit is NOT provided use ""
query: options?.query || (options?.limit ? undefined : ""),
limit: options?.limit || 0,
presences: options?.presences || false,
user_ids: options?.userIds?.map((id) => id.toString()),
nonce,
},
});
}) as Promise<void>;
}
/** https://discord.com/developers/docs/topics/gateway#request-guild-members */
export interface RequestGuildMembers {
/** id of the guild to get members for */
guildId: bigint;
/** String that username starts with, or an empty string to return all members */
query?: string;
/** Maximum number of members to send matching the query; a limit of 0 can be used with an empty string query to return all members */
limit: number;
/** Used to specify if we want the presences of the matched members */
presences?: boolean;
/** Used to specify which users you wish to fetch */
userIds?: bigint[];
/** Nonce to identify the Guild Members Chunk response */
nonce?: string;
}