Skip to content

Commit

Permalink
Merge branch 'main' into fix/ws-initial-resume
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] committed May 11, 2023
2 parents 2af4345 + a51c48e commit 200e01e
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 41 deletions.
91 changes: 91 additions & 0 deletions apps/guide/src/components/DiscordAPITypesLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { FiExternalLink } from '@react-icons/all-files/fi/FiExternalLink';
import type { PropsWithChildren } from 'react';
import {
BASE_URL_DISCORD_API_TYPES,
DISCORD_API_TYPES_VERSION,
DISCORD_API_TYPES_VOICE_VERSION,
} from '~/util/constants';

interface DiscordAPITypesLinkOptions {
/**
* The initial documentation enum, interface, function etc.
*
* @example `'RESTJSONErrorCodes'`
*/
parent?: string;
/**
* The scope of where this link lives.
*
* @remarks API does not have a scope.
*/
scope?: 'gateway' | 'globals' | 'payloads' | 'rest' | 'rpc' | 'utils' | 'voice';
/**
* The symbol belonging to the parent.
*
* @example '`MaximumNumberOfGuildsReached'`
*/
symbol?: string;
/**
* The type of the {@link DiscordAPITypesLinkOptions.parent}.
*
* @example `'enum'`
* @example `'interface'`
*/
type?: string;
}

export function DiscordAPITypesLink({
parent,
scope,
symbol,
type,
children,
}: PropsWithChildren<DiscordAPITypesLinkOptions>) {
let url = BASE_URL_DISCORD_API_TYPES;
let text = 'discord-api-types';

if (type || parent) {
url += `/api/discord-api-types`;

switch (scope) {
case 'globals':
url += `-${scope}`;
break;
case 'gateway':
case 'payloads':
case 'rest':
url += `-${scope}/common`;
break;
case 'rpc':
case 'utils':
url += `-${scope}/${DISCORD_API_TYPES_VERSION}`;
break;
case 'voice':
url += `-${scope}/${DISCORD_API_TYPES_VOICE_VERSION}`;
break;
default:
url += `-${DISCORD_API_TYPES_VERSION}`;
}

if (type) {
url += `/${type}/${parent}`;
if (symbol) url += `#${symbol}`;
} else {
url += `#${parent}`;
}

text = `${parent}${symbol ? `#${symbol}` : ''}${type?.toUpperCase() === 'FUNCTION' ? '()' : ''}`;
}

return (
<a
className="inline-flex flex-row place-items-center gap-1"
href={url}
rel="external noopener noreferrer"
target="_blank"
>
{children ?? text}
<FiExternalLink size={18} />
</a>
);
}
2 changes: 2 additions & 0 deletions apps/guide/src/components/Mdx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Alert, Section, DiscordMessages, DiscordMessage, DiscordMessageEmbed } from '@discordjs/ui';
import { useMDXComponent } from 'next-contentlayer/hooks';
import { DiscordAPITypesLink } from './DiscordAPITypesLink';
import { H1 } from './H1';
import { H2 } from './H2';
import { H3 } from './H3';
Expand All @@ -20,6 +21,7 @@ export function Mdx({ code }: { code: string }) {
DiscordMessages,
DiscordMessage,
DiscordMessageEmbed,
DiscordAPITypesLink,
DocsLink,
ResultingCode,
h1: H1,
Expand Down
2 changes: 1 addition & 1 deletion apps/guide/src/content/04-popular-topics/02-audit-logs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,4 @@ client.on(Events.GuildAuditLogEntryCreate, async (auditLog) => {

</CH.Code>

If you want to check who banned a user, it's the same example as above except the _`action`_ should be _`AuditLogEvent.MemberBanAdd`_. You can check the rest of the types over at the [discord-api-types documentation](https://discord-api-types.dev/api/discord-api-types-v10/enum/AuditLogEvent).
If you want to check who banned a user, it's the same example as above except the _`action`_ should be <DiscordAPITypesLink type="enum" parent="AuditLogEvent" symbol="MemberBanAdd" />. You can check the rest of the possible actions on this page.
2 changes: 0 additions & 2 deletions apps/guide/src/content/04-popular-topics/04-webhooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ You can use <DocsLink type="class" parent="Webhook" symbol="editMessage" bracket
```js
const message = await webhook.editMessage('123456789012345678', {
content: 'Edited!',
username: 'some-username',
avatarURL: 'https://guide.discordjs.dev/assets/discordjs.png',
embeds: [embed],
});
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ discord.js v14 makes the switch to Discord API v10!

Any areas that used to accept a _`string`_ or _`number`_ type for an enum parameter will now only accept a _`number`_.

In addition, the old enums exported by discord.js v13 and lower are replaced with new enums from [discord-api-types](https://discord-api-types.dev/api/discord-api-types-v10).
In addition, the old enums exported by discord.js v13 and lower are replaced with new enums from <DiscordAPITypesLink />.

#### New enum differences

Expand Down Expand Up @@ -121,7 +121,7 @@ Areas like _`Client`_ initialization, JSON slash commands and JSON message compo

#### Channels

Some channel type guard methods that narrowed to one channel type have been removed. Instead compare the _`type`_ property against a [ChannelType](https://discord-api-types.dev/api/discord-api-types-v10/enum/ChannelType) enum member to narrow channels.
Some channel type guard methods that narrowed to one channel type have been removed. Instead compare the _`type`_ property against a <DiscordAPITypesLink type="enum" parent="ChannelType" /> enum member to narrow channels.

<CH.Code>

Expand Down Expand Up @@ -361,7 +361,7 @@ _`IntegrationApplication#summary`_ has been removed as it is no longer supported

### Interaction

Whenever an interaction is replied to and one fetches the reply, it could possibly give an [APIMessage](https://discord-api-types.dev/api/discord-api-types-v10/interface/APIMessage) if the guild was not cached. However, interaction replies now always return a discord.js <DocsLink type="class" parent="Message"/> object with _`fetchReply`_ as _`true`_.
Whenever an interaction is replied to and one fetches the reply, it could possibly give an <DiscordAPITypesLink type="interface" parent="APIMessage" /> if the guild was not cached. However, interaction replies now always return a discord.js <DocsLink type="class" parent="Message"/> object with _`fetchReply`_ as _`true`_.

The base interaction class is now <DocsLink type="class" parent="BaseInteraction"/>.

Expand Down Expand Up @@ -690,7 +690,7 @@ Added support for role connection metadata.

A new <DocsLink type="class" parent="Collector" symbol="e-ignore"/> event has been added which is emitted whenever an element is not collected by the collector.

Component collector options now use the [ComponentType](https://discord-api-types.dev/api/discord-api-types-v10/enum/ComponentType) enum values:
Component collector options now use the <DiscordAPITypesLink type="enum" parent="ComponentType" /> enum values:

<CH.Code>

Expand Down
12 changes: 12 additions & 0 deletions apps/guide/src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export const BASE_URL = 'https://discord.js.org/docs/packages' as const;

export const BASE_URL_LEGACY = 'https://old.discordjs.dev/#/docs/discord.js' as const;

export const BASE_URL_DISCORD_API_TYPES = 'https://discord-api-types.dev' as const;

export const DESCRIPTION = 'Imagine a guide... that explores the many possibilities for your discord.js bot.';

export const GITHUB_BASE_PAGES_PATH = 'https://github.com/discordjs/discord.js/tree/main/apps/guide/src/pages';
Expand All @@ -25,3 +27,13 @@ export const PACKAGES = [
* The stable version of discord.js.
*/
export const VERSION = '14.11.0' as const;

/**
* The API version (for discord-api-types). This is prefixed with a "v".
*/
export const DISCORD_API_TYPES_VERSION = 'v10' as const;

/**
* The voice API version (for discord-api-types). This is prefixed with a "v".
*/
export const DISCORD_API_TYPES_VOICE_VERSION = 'v4' as const;
2 changes: 1 addition & 1 deletion apps/website/src/app/docs/packages/[package]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default async function Page({ params }: { params: { package: string } })
href={`/docs/packages/${params.package}/${version}`}
key={`${version}-${idx}`}
>
<div className="flex flex-row place-content-between place-items-center gap-4">
<div className="flex grow flex-row place-content-between place-items-center gap-4">
<div className="flex flex-row place-content-between place-items-center gap-4">
<VscVersions size={25} />
<h2 className="font-semibold">{version}</h2>
Expand Down
45 changes: 18 additions & 27 deletions apps/website/src/app/docs/packages/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ export default function Page() {
<div className="mx-auto min-h-screen min-w-xs flex flex-col gap-8 px-4 py-6 sm:w-md lg:px-6 lg:py-6">
<h1 className="text-2xl font-semibold">Select a package:</h1>
<div className="flex flex-col gap-4">
<a
className="h-11 flex transform-gpu cursor-pointer select-none appearance-none place-content-between border border-neutral-300 rounded bg-white p-4 text-base font-semibold leading-none text-black outline-none active:translate-y-px dark:border-dark-100 active:bg-neutral-200 dark:bg-dark-400 hover:bg-neutral-100 dark:text-white focus:ring focus:ring-width-2 focus:ring-blurple dark:active:bg-dark-200 dark:hover:bg-dark-300"
href="https://old.discordjs.dev/#/docs/discord.js"
>
<a className={buttonVariants({ variant: 'secondary' })} href="https://old.discordjs.dev/#/docs/discord.js">
<div className="flex grow flex-row place-content-between place-items-center gap-4">
<div className="flex grow flex-row place-content-between place-items-center gap-4">
<div className="flex flex-row place-content-between place-items-center gap-4">
<VscPackage size={25} />
<h2 className="font-semibold">discord.js</h2>
</div>
<div className="flex flex-row place-content-between place-items-center gap-4">
<VscPackage size={25} />
<h2 className="font-semibold">discord.js</h2>
</div>
<VscArrowRight size={20} />
</div>
Expand All @@ -34,31 +29,27 @@ export default function Page() {
key={`${pkg}-${idx}`}
>
<div className="flex grow flex-row place-content-between place-items-center gap-4">
<div className="flex grow flex-row place-content-between place-items-center gap-4">
<div className="flex flex-row place-content-between place-items-center gap-4">
<VscPackage size={25} />
<h2 className="font-semibold">{pkg}</h2>
</div>
{/* <Link href={`/docs/packages/${pkg}`}>
<div
className="bg-blurple focus:ring-width-2 flex h-6 transform-gpu cursor-pointer select-none appearance-none flex-row place-content-center place-items-center rounded border-0 px-2 text-xs font-semibold leading-none text-white outline-none focus:ring focus:ring-white active:translate-y-px"
role="link"
>
Select version
</div>
</Link> */}
<div className="flex flex-row place-content-between place-items-center gap-4">
<VscPackage size={25} />
<h2 className="font-semibold">{pkg}</h2>
</div>
{/* <Link href={`/docs/packages/${pkg}`}>
<div
className="bg-blurple focus:ring-width-2 flex h-6 transform-gpu cursor-pointer select-none appearance-none flex-row place-content-center place-items-center rounded border-0 px-2 text-xs font-semibold leading-none text-white outline-none focus:ring focus:ring-white active:translate-y-px"
role="link"
>
Select version
</div>
</Link> */}
<VscArrowRight size={20} />
</div>
</Link>
))}
<a className={buttonVariants({ variant: 'secondary' })} href="https://discord-api-types.dev/">
<div className="flex grow flex-row place-content-between place-items-center gap-4">
<div className="flex grow flex-row place-content-between place-items-center gap-4">
<div className="flex flex-row place-content-between place-items-center gap-4">
<VscPackage size={25} />
<h2 className="font-semibold">discord-api-types</h2>
</div>
<div className="flex flex-row place-content-between place-items-center gap-4">
<VscPackage size={25} />
<h2 className="font-semibold">discord-api-types</h2>
</div>
<FiExternalLink size={20} />
</div>
Expand Down
20 changes: 15 additions & 5 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
import {
GatewayDispatchEvents,
GatewayOpcodes,
type APIGuildMember,
type GatewayAutoModerationActionExecutionDispatchData,
type GatewayAutoModerationRuleCreateDispatchData,
type GatewayAutoModerationRuleDeleteDispatchData,
Expand Down Expand Up @@ -169,6 +168,13 @@ export interface ClientOptions {
rest: REST;
}

export interface RequestGuildMembersResult {
members: GatewayGuildMembersChunkDispatchData['members'];
nonce: NonNullable<GatewayGuildMembersChunkDispatchData['nonce']>;
notFound: NonNullable<GatewayGuildMembersChunkDispatchData['not_found']>;
presences: NonNullable<GatewayGuildMembersChunkDispatchData['presences']>;
}

export class Client extends AsyncEventEmitter<ManagerShardEventsMap> {
public readonly rest: REST;

Expand Down Expand Up @@ -199,8 +205,10 @@ export class Client extends AsyncEventEmitter<ManagerShardEventsMap> {
const shardId = calculateShardId(options.guild_id, await this.gateway.getShardCount());
const nonce = options.nonce ?? DiscordSnowflake.generate().toString();

const promise = new Promise<APIGuildMember[]>((resolve, reject) => {
const guildMembers: APIGuildMember[] = [];
const promise = new Promise<RequestGuildMembersResult>((resolve, reject) => {
const members: RequestGuildMembersResult['members'] = [];
const notFound: RequestGuildMembersResult['notFound'] = [];
const presences: RequestGuildMembersResult['presences'] = [];

const timer = setTimeout(() => {
reject(new Error('Request timed out'));
Expand All @@ -211,11 +219,13 @@ export class Client extends AsyncEventEmitter<ManagerShardEventsMap> {

if (data.nonce !== nonce) return;

guildMembers.push(...data.members);
members.push(...data.members);
if ('presences' in data) presences.push(...data.presences);
if ('not_found' in data) notFound.push(...data.not_found);

if (data.chunk_index >= data.chunk_count - 1) {
this.off(GatewayDispatchEvents.GuildMembersChunk, handler);
resolve(guildMembers);
resolve({ members, nonce, notFound, presences });
}
};

Expand Down
3 changes: 2 additions & 1 deletion packages/create-discord-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
],
"repository": {
"type": "git",
"url": "git+https://github.com/discordjs/discord.js.git"
"url": "git+https://github.com/discordjs/discord.js.git",
"directory": "packages/create-discord-bot"
},
"bugs": {
"url": "https://github.com/discordjs/discord.js/issues"
Expand Down

0 comments on commit 200e01e

Please sign in to comment.