-
-
Notifications
You must be signed in to change notification settings - Fork 4k
feat: split CommandInteractionOptionResolver
#8439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Syjalo
wants to merge
3
commits into
discordjs:main
Choose a base branch
from
Syjalo:split-CommandInteractionOptionResolver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/discord.js/src/structures/AutocompleteInteractionOptionResolver.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'use strict'; | ||
|
||
const { ApplicationCommandOptionType } = require('discord-api-types/v10'); | ||
const InteractionOptionResolver = require('./InteractionOptionResolver'); | ||
const { ErrorCodes } = require('../errors'); | ||
|
||
/** | ||
* A resolver for autocomplete interaction options. | ||
* @extends {InteractionOptionResolver} | ||
*/ | ||
class AutocompleteInteractionOptionResolver extends InteractionOptionResolver { | ||
/** | ||
* Gets the selected subcommand. | ||
* @param {boolean} [required=true] Whether to throw an error if there is no subcommand. | ||
* @returns {?string} The name of the selected subcommand, or null if not set and not required. | ||
*/ | ||
getSubcommand(required = true) { | ||
if (required && !this._subcommand) { | ||
throw new TypeError(ErrorCodes.CommandInteractionOptionNoSubcommand); | ||
} | ||
return this._subcommand; | ||
} | ||
|
||
/** | ||
* Gets the selected subcommand group. | ||
* @param {boolean} [required=false] Whether to throw an error if there is no subcommand group. | ||
* @returns {?string} The name of the selected subcommand group, or null if not set and not required. | ||
*/ | ||
getSubcommandGroup(required = false) { | ||
if (required && !this._group) { | ||
throw new TypeError(ErrorCodes.CommandInteractionOptionNoSubcommandGroup); | ||
} | ||
return this._group; | ||
} | ||
|
||
/** | ||
* Gets a boolean option. | ||
* @param {string} name The name of the option. | ||
* @param {boolean} [required=false] Whether to throw an error if the option is not found. | ||
* @returns {?boolean} The value of the option, or null if not set and not required. | ||
*/ | ||
getBoolean(name, required = false) { | ||
const option = this._getTypedOption(name, ApplicationCommandOptionType.Boolean, ['value'], required); | ||
return option?.value ?? null; | ||
} | ||
|
||
/** | ||
* Gets a string option. | ||
* @param {string} name The name of the option. | ||
* @param {boolean} [required=false] Whether to throw an error if the option is not found. | ||
* @returns {?string} The value of the option, or null if not set and not required. | ||
*/ | ||
getString(name, required = false) { | ||
const option = this._getTypedOption(name, ApplicationCommandOptionType.String, ['value'], required); | ||
return option?.value ?? null; | ||
} | ||
|
||
/** | ||
* Gets an integer option. | ||
* @param {string} name The name of the option. | ||
* @param {boolean} [required=false] Whether to throw an error if the option is not found. | ||
* @returns {?number} The value of the option, or null if not set and not required. | ||
*/ | ||
getInteger(name, required = false) { | ||
const option = this._getTypedOption(name, ApplicationCommandOptionType.Integer, ['value'], required); | ||
return option?.value ?? null; | ||
} | ||
|
||
/** | ||
* Gets a number option. | ||
* @param {string} name The name of the option. | ||
* @param {boolean} [required=false] Whether to throw an error if the option is not found. | ||
* @returns {?number} The value of the option, or null if not set and not required. | ||
*/ | ||
getNumber(name, required = false) { | ||
const option = this._getTypedOption(name, ApplicationCommandOptionType.Number, ['value'], required); | ||
return option?.value ?? null; | ||
} | ||
|
||
/** | ||
* The full autocomplete option object. | ||
* @typedef {Object} AutocompleteFocusedOption | ||
* @property {string} name The name of the option | ||
* @property {ApplicationCommandOptionType} type The type of the application command option | ||
* @property {string} value The value of the option | ||
* @property {boolean} focused Whether this option is currently in focus for autocomplete | ||
*/ | ||
|
||
/** | ||
* Gets the focused option. | ||
* @param {boolean} [getFull=false] Whether to get the full option object | ||
* @returns {string|AutocompleteFocusedOption} | ||
* The value of the option, or the whole option if getFull is true | ||
*/ | ||
getFocused(getFull = false) { | ||
const focusedOption = this._hoistedOptions.find(option => option.focused); | ||
if (!focusedOption) throw new TypeError(ErrorCodes.AutocompleteInteractionOptionNoFocusedOption); | ||
return getFull ? focusedOption : focusedOption.value; | ||
} | ||
} | ||
|
||
module.exports = AutocompleteInteractionOptionResolver; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||
'use strict'; | ||||||
|
||||||
const ChatInputCommandInteractionOptionResolver = require('./ChatInputCommandInteractionOptionResolver'); | ||||||
const CommandInteraction = require('./CommandInteraction'); | ||||||
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver'); | ||||||
|
||||||
/** | ||||||
* Represents a command interaction. | ||||||
|
@@ -15,7 +15,7 @@ class ChatInputCommandInteraction extends CommandInteraction { | |||||
* The options passed to the command. | ||||||
* @type {CommandInteractionOptionResolver} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
this.options = new CommandInteractionOptionResolver( | ||||||
this.options = new ChatInputCommandInteractionOptionResolver( | ||||||
this.client, | ||||||
data.data.options?.map(option => this.transformOption(option, data.data.resolved)) ?? [], | ||||||
this.transformResolved(data.data.resolved ?? {}), | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ | |||||
|
||||||
const { ApplicationCommandOptionType } = require('discord-api-types/v10'); | ||||||
const CommandInteraction = require('./CommandInteraction'); | ||||||
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver'); | ||||||
const ContextMenuCommandInteractionOptionResolver = require('./ContextMenuCommandInteractionOptionResolver'); | ||||||
const { lazy } = require('../util/Util'); | ||||||
|
||||||
const getMessage = lazy(() => require('./Message').Message); | ||||||
|
@@ -18,7 +18,7 @@ class ContextMenuCommandInteraction extends CommandInteraction { | |||||
* The target of the interaction, parsed into options | ||||||
* @type {CommandInteractionOptionResolver} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
this.options = new CommandInteractionOptionResolver( | ||||||
this.options = new ContextMenuCommandInteractionOptionResolver( | ||||||
this.client, | ||||||
this.resolveContextMenuOptions(data.data), | ||||||
this.transformResolved(data.data.resolved), | ||||||
|
43 changes: 43 additions & 0 deletions
43
packages/discord.js/src/structures/ContextMenuCommandInteractionOptionResolver.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const { ApplicationCommandOptionType } = require('discord-api-types/v10'); | ||
const InteractionOptionResolver = require('./InteractionOptionResolver'); | ||
|
||
/** | ||
* A resolver for context menu interaction options. | ||
* @extends {InteractionOptionResolver} | ||
*/ | ||
class ContextMenuCommandInteractionOptionResolver extends InteractionOptionResolver { | ||
/** | ||
* Gets a user option. | ||
* @param {boolean} [required=false] Whether to throw an error if the option is not found. | ||
* @returns {?User} The value of the option, or null if not set and not required. | ||
*/ | ||
getUser(required = false) { | ||
const option = this._getTypedOption('user', ApplicationCommandOptionType.User, ['user'], required); | ||
return option?.user ?? null; | ||
} | ||
|
||
/** | ||
* Gets a member option. | ||
* @returns {?(GuildMember|APIGuildMember)} | ||
* The value of the option, or null if the user is not present in the guild or the option is not set. | ||
*/ | ||
getMember() { | ||
const option = this._getTypedOption('user', ApplicationCommandOptionType.User, ['member'], false); | ||
return option?.member ?? null; | ||
} | ||
|
||
/** | ||
* Gets a message option. | ||
* @param {boolean} [required=false] Whether to throw an error if the option is not found. | ||
* @returns {?Message} | ||
* The value of the option, or null if not set and not required. | ||
*/ | ||
getMessage(required = false) { | ||
const option = this._getTypedOption('message', '_MESSAGE', ['message'], required); | ||
return option?.message ?? null; | ||
} | ||
} | ||
|
||
module.exports = ContextMenuCommandInteractionOptionResolver; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.