Skip to content

Commit

Permalink
Merge branch 'main' into discord.js/role-connections
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Dec 24, 2022
2 parents 59896df + 7ce9909 commit 52f5dff
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .github/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
color: fbca04
- name: backlog
color: 7ef7ef
- name: backport
color: 88aabb
- name: blocked
color: fc1423
- name: bug
Expand Down
1 change: 1 addition & 0 deletions packages/discord.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"dependencies": {
"@discordjs/builders": "workspace:^",
"@discordjs/collection": "workspace:^",
"@discordjs/formatters": "workspace:^",
"@discordjs/rest": "workspace:^",
"@discordjs/util": "workspace:^",
"@sapphire/snowflake": "^3.2.2",
Expand Down
4 changes: 3 additions & 1 deletion packages/discord.js/src/client/websocket/WebSocketShard.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,9 @@ class WebSocketShard extends EventEmitter {
);

// Cleanup connection listeners
this._cleanupConnection();
if (this.connection) {
this._cleanupConnection();
}

this.emitClose();
// Setting the variable false to check for zombie connections.
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/src/errors/ErrorCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
* @property {'CommandInteractionOptionEmpty'} CommandInteractionOptionEmpty
* @property {'CommandInteractionOptionNoSubcommand'} CommandInteractionOptionNoSubcommand
* @property {'CommandInteractionOptionNoSubcommandGroup'} CommandInteractionOptionNoSubcommandGroup
* @property {'CommandInteractionOptionInvalidChannelType'} CommandInteractionOptionInvalidChannelType
* @property {'AutocompleteInteractionOptionNoFocusedOption'} AutocompleteInteractionOptionNoFocusedOption
* @property {'ModalSubmitInteractionFieldNotFound'} ModalSubmitInteractionFieldNotFound
Expand Down Expand Up @@ -281,6 +282,7 @@ const keys = [
'CommandInteractionOptionEmpty',
'CommandInteractionOptionNoSubcommand',
'CommandInteractionOptionNoSubcommandGroup',
'CommandInteractionOptionInvalidChannelType',
'AutocompleteInteractionOptionNoFocusedOption',

'ModalSubmitInteractionFieldNotFound',
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ const Messages = {
`Required option "${name}" is of type: ${type}; expected a non-empty value.`,
[DjsErrorCodes.CommandInteractionOptionNoSubcommand]: 'No subcommand specified for interaction.',
[DjsErrorCodes.CommandInteractionOptionNoSubcommandGroup]: 'No subcommand group specified for interaction.',
[DjsErrorCodes.CommandInteractionOptionInvalidChannelType]: (name, type, expected) =>
`The type of channel of the option "${name}" is: ${type}; expected ${expected}.`,
[DjsErrorCodes.AutocompleteInteractionOptionNoFocusedOption]: 'No focused option for autocomplete interaction.',

[DjsErrorCodes.ModalSubmitInteractionFieldNotFound]: customId =>
Expand Down
36 changes: 18 additions & 18 deletions packages/discord.js/src/managers/AutoModerationRuleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ class AutoModerationRuleManager extends CachedManager {
* @name AutoModerationRuleManager#cache
*/

/**
* Resolves an {@link AutoModerationRuleResolvable} to an {@link AutoModerationRule} object.
* @method resolve
* @memberof AutoModerationRuleManager
* @instance
* @param {AutoModerationRuleResolvable} autoModerationRule The AutoModerationRule resolvable to resolve
* @returns {?AutoModerationRule}
*/

/**
* Resolves an {@link AutoModerationRuleResolvable} to a {@link AutoModerationRule} id.
* @method resolveId
* @memberof AutoModerationRuleManager
* @instance
* @param {AutoModerationRuleResolvable} autoModerationRule The AutoModerationRule resolvable to resolve
* @returns {?Snowflake}
*/

_add(data, cache) {
return super._add(data, cache, { extras: [this.guild] });
}
Expand Down Expand Up @@ -259,24 +277,6 @@ class AutoModerationRuleManager extends CachedManager {
const autoModerationRuleId = this.resolveId(autoModerationRule);
await this.client.rest.delete(Routes.guildAutoModerationRule(this.guild.id, autoModerationRuleId), { reason });
}

/**
* Resolves an {@link AutoModerationRuleResolvable} to an {@link AutoModerationRule} object.
* @method resolve
* @memberof AutoModerationRuleManager
* @instance
* @param {AutoModerationRuleResolvable} autoModerationRule The AutoModerationRule resolvable to resolve
* @returns {?AutoModerationRule}
*/

/**
* Resolves an {@link AutoModerationRuleResolvable} to a {@link AutoModerationRule} id.
* @method resolveId
* @memberof AutoModerationRuleManager
* @instance
* @param {AutoModerationRuleResolvable} autoModerationRule The AutoModerationRule resolvable to resolve
* @returns {?Snowflake}
*/
}

module.exports = AutoModerationRuleManager;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GuildForumThreadManager extends ThreadManager {

/**
* @typedef {BaseMessageOptions} GuildForumThreadMessageCreateOptions
* @property {stickers} [stickers] The stickers to send with the message
* @property {StickerResolvable} [stickers] The stickers to send with the message
* @property {BitFieldResolvable} [flags] The flags to send with the message
*/

Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/structures/BaseSelectMenuComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class BaseSelectMenuComponent extends Component {

/**
* Whether this select menu is disabled
* @type {?boolean}
* @type {boolean}
* @readonly
*/
get disabled() {
return this.data.disabled ?? null;
return this.data.disabled ?? false;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/structures/ButtonComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class ButtonComponent extends Component {

/**
* Whether this button is disabled
* @type {?boolean}
* @type {boolean}
* @readonly
*/
get disabled() {
return this.data.disabled ?? null;
return this.data.disabled ?? false;
}

/**
Expand Down
18 changes: 9 additions & 9 deletions packages/discord.js/src/structures/CategoryChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ const CategoryChannelChildManager = require('../managers/CategoryChannelChildMan
* @extends {GuildChannel}
*/
class CategoryChannel extends GuildChannel {
/**
* A manager of the channels belonging to this category
* @type {CategoryChannelChildManager}
* @readonly
*/
get children() {
return new CategoryChannelChildManager(this);
}

/**
* Sets the category parent of this channel.
* <warn>It is not currently possible to set the parent of a CategoryChannel.</warn>
Expand All @@ -27,6 +18,15 @@ class CategoryChannel extends GuildChannel {
* @param {SetParentOptions} [options={}] The options for setting the parent
* @returns {Promise<GuildChannel>}
*/

/**
* A manager of the channels belonging to this category
* @type {CategoryChannelChildManager}
* @readonly
*/
get children() {
return new CategoryChannelChildManager(this);
}
}

module.exports = CategoryChannel;
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,24 @@ class CommandInteractionOptionResolver {
* Gets a channel option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @param {ChannelType[]} [channelTypes=[]] The allowed types of channels. If empty, all channel types are allowed.
* @returns {?(GuildChannel|ThreadChannel|APIChannel)}
* The value of the option, or null if not set and not required.
*/
getChannel(name, required = false) {
getChannel(name, required = false, channelTypes = []) {
const option = this._getTypedOption(name, [ApplicationCommandOptionType.Channel], ['channel'], required);
return option?.channel ?? null;
const channel = option?.channel ?? null;

if (channel && channelTypes.length > 0 && !channelTypes.includes(channel.type)) {
throw new DiscordjsTypeError(
ErrorCodes.CommandInteractionOptionInvalidChannelType,
name,
channel.type,
channelTypes.join(', '),
);
}

return channel;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/DMChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DMChannel extends BaseChannel {
super._patch(data);

if (data.recipients) {
const recipient = data.recipients[0];
const recipient = data.recipients.find(user => user.id !== this.client.user.id);

/**
* The recipient's id
Expand Down
4 changes: 4 additions & 0 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ module.exports = {
escapeUnderline,
escapeStrikethrough,
escapeSpoiler,
escapeHeading,
escapeBulletedList,
escapeNumberedList,
escapeMaskedLink,
fetchRecommendedShardCount,
parseEmoji,
resolvePartialEmoji,
Expand Down
44 changes: 36 additions & 8 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ export class ActionRowBuilder<T extends AnyComponentBuilder = AnyComponentBuilde
| APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>
>,
);
public static from(
public static from<T extends AnyComponentBuilder = AnyComponentBuilder>(
other:
| JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>>
| APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>,
): ActionRowBuilder;
| JSONEncodable<APIActionRowComponent<ReturnType<T['toJSON']>>>
| APIActionRowComponent<ReturnType<T['toJSON']>>,
): ActionRowBuilder<T>;
}

export type MessageActionRowComponent =
Expand Down Expand Up @@ -690,7 +690,7 @@ export class ButtonComponent extends Component<APIButtonComponent> {
public get style(): ButtonStyle;
public get label(): string | null;
public get emoji(): APIMessageComponentEmoji | null;
public get disabled(): boolean | null;
public get disabled(): boolean;
public get customId(): string | null;
public get url(): string | null;
}
Expand Down Expand Up @@ -771,7 +771,7 @@ export class BaseSelectMenuComponent<Data extends APISelectMenuComponent> extend
public get maxValues(): number | null;
public get minValues(): number | null;
public get customId(): string;
public get disabled(): boolean | null;
public get disabled(): boolean;
}

export class StringSelectMenuComponent extends BaseSelectMenuComponent<APIStringSelectComponent> {
Expand Down Expand Up @@ -1130,8 +1130,36 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
public getSubcommandGroup(required?: boolean): string | null;
public getBoolean(name: string, required: true): boolean;
public getBoolean(name: string, required?: boolean): boolean | null;
public getChannel(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['channel']>;
public getChannel(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['channel']> | null;
public getChannel<T extends ChannelType = ChannelType>(
name: string,
required: true,
channelTypes?: T[],
): Extract<
NonNullable<CommandInteractionOption<Cached>['channel']>,
{
// The `type` property of the PublicThreadChannel class is typed as `ChannelType.PublicThread | ChannelType.AnnouncementThread`
// If the user only passed one of those channel types, the Extract<> would have resolved to `never`
// Hence the need for this ternary
type: T extends ChannelType.PublicThread | ChannelType.AnnouncementThread
? ChannelType.PublicThread | ChannelType.AnnouncementThread
: T;
}
>;
public getChannel<T extends ChannelType = ChannelType>(
name: string,
required?: boolean,
channelTypes?: T[],
): Extract<
NonNullable<CommandInteractionOption<Cached>['channel']>,
{
// The `type` property of the PublicThreadChannel class is typed as `ChannelType.PublicThread | ChannelType.AnnouncementThread`
// If the user only passed one of those channel types, the Extract<> would have resolved to `never`
// Hence the need for this ternary
type: T extends ChannelType.PublicThread | ChannelType.AnnouncementThread
? ChannelType.PublicThread | ChannelType.AnnouncementThread
: T;
}
> | null;
public getString(name: string, required: true): string;
public getString(name: string, required?: boolean): string | null;
public getInteger(name: string, required: true): number;
Expand Down
40 changes: 40 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
APIEmbed,
ApplicationCommandType,
APIMessage,
APIActionRowComponent,
APIActionRowComponentTypes,
APIStringSelectComponent,
} from 'discord-api-types/v10';
import {
Expand Down Expand Up @@ -134,6 +136,8 @@ import {
Webhook,
WebhookClient,
InteractionWebhook,
ActionRowComponent,
ActionRow,
GuildAuditLogsActionType,
GuildAuditLogsTargetType,
ModalSubmitInteraction,
Expand All @@ -152,6 +156,8 @@ import {
AutoModerationActionExecution,
AutoModerationRule,
AutoModerationRuleManager,
PrivateThreadChannel,
PublicThreadChannel,
} from '.';
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
Expand Down Expand Up @@ -1744,6 +1750,22 @@ client.on('interactionCreate', async interaction => {

expectType<GuildBasedChannel>(interaction.options.getChannel('test', true));
expectType<Role>(interaction.options.getRole('test', true));

expectType<PublicThreadChannel>(interaction.options.getChannel('test', true, [ChannelType.PublicThread]));
expectType<PublicThreadChannel>(interaction.options.getChannel('test', true, [ChannelType.AnnouncementThread]));
expectType<PublicThreadChannel>(
interaction.options.getChannel('test', true, [ChannelType.PublicThread, ChannelType.AnnouncementThread]),
);
expectType<PrivateThreadChannel>(interaction.options.getChannel('test', true, [ChannelType.PrivateThread]));

expectType<TextChannel>(interaction.options.getChannel('test', true, [ChannelType.GuildText]));
expectType<TextChannel | null>(interaction.options.getChannel('test', false, [ChannelType.GuildText]));
expectType<ForumChannel | VoiceChannel>(
interaction.options.getChannel('test', true, [ChannelType.GuildForum, ChannelType.GuildVoice]),
);
expectType<ForumChannel | VoiceChannel | null>(
interaction.options.getChannel('test', false, [ChannelType.GuildForum, ChannelType.GuildVoice]),
);
} else {
// @ts-expect-error
consumeCachedCommand(interaction);
Expand Down Expand Up @@ -2028,6 +2050,24 @@ EmbedBuilder.from(embedData);
declare const embedComp: Embed;
EmbedBuilder.from(embedComp);

declare const actionRowData: APIActionRowComponent<APIActionRowComponentTypes>;
ActionRowBuilder.from(actionRowData);

declare const actionRowComp: ActionRow<ActionRowComponent>;
ActionRowBuilder.from(actionRowComp);

declare const buttonsActionRowData: APIActionRowComponent<APIButtonComponent>;
declare const buttonsActionRowComp: ActionRow<ButtonComponent>;

expectType<ActionRowBuilder<ButtonBuilder>>(ActionRowBuilder.from<ButtonBuilder>(buttonsActionRowData));
expectType<ActionRowBuilder<ButtonBuilder>>(ActionRowBuilder.from<ButtonBuilder>(buttonsActionRowComp));

declare const anyComponentsActionRowData: APIActionRowComponent<APIActionRowComponentTypes>;
declare const anyComponentsActionRowComp: ActionRow<ActionRowComponent>;

expectType<ActionRowBuilder>(ActionRowBuilder.from(anyComponentsActionRowData));
expectType<ActionRowBuilder>(ActionRowBuilder.from(anyComponentsActionRowComp));

declare const stageChannel: StageChannel;
declare const partialGroupDMChannel: PartialGroupDMChannel;

Expand Down
10 changes: 10 additions & 0 deletions packages/formatters/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

All notable changes to this project will be documented in this file.

# [@discordjs/formatters@0.1.0](https://github.com/discordjs/discord.js/tree/@discordjs/[email protected]) - (2022-12-16)

## Features

- Add `@discordjs/formatters` (#8889) ([3fca638](https://github.com/discordjs/discord.js/commit/3fca638a8470dcea2f79ddb9f18526dbc0017c88))

1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8471,6 +8471,7 @@ __metadata:
"@discordjs/builders": "workspace:^"
"@discordjs/collection": "workspace:^"
"@discordjs/docgen": "workspace:^"
"@discordjs/formatters": "workspace:^"
"@discordjs/rest": "workspace:^"
"@discordjs/util": "workspace:^"
"@favware/cliff-jumper": ^1.9.0
Expand Down

0 comments on commit 52f5dff

Please sign in to comment.