Skip to content

Commit

Permalink
feat: add support for nsfw commands (#7976)
Browse files Browse the repository at this point in the history
* chore: update

* fix: add edit changes

* chore: make requested changes

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
suneettipirneni and kodiakhq[bot] committed Dec 16, 2022
1 parent 6540914 commit 7a51344
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ const memberPermissionPredicate = s.union(
export function validateDefaultMemberPermissions(permissions: unknown) {
return memberPermissionPredicate.parse(permissions);
}

export function validateNSFW(value: unknown): asserts value is boolean {
booleanPredicate.parse(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
validateDMPermission,
validateMaxOptionsLength,
validateRequiredParameters,
validateNSFW,
} from './Assertions.js';
import { SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder } from './SlashCommandSubcommands.js';
import { SharedNameAndDescription } from './mixins/NameAndDescription.js';
Expand Down Expand Up @@ -64,6 +65,11 @@ export class SlashCommandBuilder {
*/
public readonly dm_permission: boolean | undefined = undefined;

/**
* Whether this command is NSFW
*/
public readonly nsfw: boolean | undefined = undefined;

/**
* Returns the final data that should be sent to Discord.
*
Expand Down Expand Up @@ -134,6 +140,18 @@ export class SlashCommandBuilder {
return this;
}

/**
* Sets whether this command is NSFW
*
* @param nsfw - Whether this command is NSFW
*/
public setNSFW(nsfw = true) {
// Assert the value matches the conditions
validateNSFW(nsfw);
Reflect.set(this, 'nsfw', nsfw);
return this;
}

/**
* Adds a new subcommand group to this command
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ class ApplicationCommandManager extends CachedManager {
name: command.name,
name_localizations: command.nameLocalizations ?? command.name_localizations,
description: command.description,
nsfw: command.nsfw,
description_localizations: command.descriptionLocalizations ?? command.description_localizations,
type: command.type,
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
Expand Down
8 changes: 8 additions & 0 deletions packages/discord.js/src/structures/ApplicationCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class ApplicationCommand extends Base {
*/
this.type = data.type;

/**
* Whether this command is age-restricted (18+)
* @type {boolean}
*/
this.nsfw = data.nsfw ?? false;

this._patch(data);
}

Expand Down Expand Up @@ -188,6 +194,7 @@ class ApplicationCommand extends Base {
* {@link ApplicationCommandType.ChatInput}
* @property {Object<Locale, string>} [nameLocalizations] The localizations for the command name
* @property {string} description The description of the command, if type is {@link ApplicationCommandType.ChatInput}
* @property {boolean} [nsfw] Whether the command is age-restricted
* @property {Object<Locale, string>} [descriptionLocalizations] The localizations for the command description,
* if type is {@link ApplicationCommandType.ChatInput}
* @property {ApplicationCommandType} [type=ApplicationCommandType.ChatInput] The type of the command
Expand Down Expand Up @@ -377,6 +384,7 @@ class ApplicationCommand extends Base {
('description' in command && command.description !== this.description) ||
('version' in command && command.version !== this.version) ||
(command.type && command.type !== this.type) ||
('nsfw' in command && command.nsfw !== this.nsfw) ||
// Future proof for options being nullable
// TODO: remove ?? 0 on each when nullable
(command.options?.length ?? 0) !== (this.options?.length ?? 0) ||
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ export class ApplicationCommand<PermissionsFetchType = {}> extends Base {
>;
public type: ApplicationCommandType;
public version: Snowflake;
public nsfw: boolean;
public delete(): Promise<ApplicationCommand<PermissionsFetchType>>;
public edit(data: Partial<ApplicationCommandData>): Promise<ApplicationCommand<PermissionsFetchType>>;
public setName(name: string): Promise<ApplicationCommand<PermissionsFetchType>>;
Expand Down Expand Up @@ -4169,6 +4170,7 @@ export interface BaseApplicationCommandData {
nameLocalizations?: LocalizationMap;
dmPermission?: boolean;
defaultMemberPermissions?: PermissionResolvable | null;
nsfw?: boolean;
}

export interface AttachmentData {
Expand Down

0 comments on commit 7a51344

Please sign in to comment.