Skip to content

feat(builder): add autoSet and localization for commands#60

Merged
vrdons merged 14 commits intomainfrom
feat/localizedbuilder
Mar 8, 2026
Merged

feat(builder): add autoSet and localization for commands#60
vrdons merged 14 commits intomainfrom
feat/localizedbuilder

Conversation

@vrdons
Copy link
Member

@vrdons vrdons commented Mar 8, 2026

No description provided.

vrdons added 2 commits March 8, 2026 13:18
- Add autoSet methods to command builders for automatic name/description
- Implement localization utility for commands and options
- Add report command with localized strings in en-US and tr
- Update ping command to use autoSet and new builder structure
- Extend context with getDefaultLocalization method
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly upgrades the command building experience by introducing automatic naming and description setting for command components, alongside comprehensive localization support. These changes simplify command definition and enable a more internationalized user experience by allowing command elements to adapt to different languages. The refactoring also improves the internal handling of command execution contexts and locale resolution, making the system more robust and easier to extend.

Highlights

  • Command Builder Enhancements: Introduced an autoSet method to ApplicationCommandBuilder and its option builders (e.g., SlashCommandStringOption, SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder). This method automatically sets the name and description of command components based on a provided key, streamlining command definition.
  • Application Command Localization: Implemented a new utility to localize application command names and descriptions. This allows commands and their options to display different names and descriptions based on the user's locale, enhancing internationalization support.
  • Improved Context and Localization Handling: Refactored the Context class to centralize locale resolution and added a getDefaultLocalization method, providing more robust and flexible internationalization capabilities within command execution contexts.
  • New Example Command: Added a new report example command that fully utilizes the new autoSet and localization features, demonstrating how to define complex commands with subcommands and subcommand groups, along with their localized names and descriptions.
  • Command Execution Context Handling: Refined the Command class to use a new createContextHandler for onMessage and onInteraction methods, ensuring more consistent and flexible execution logic, especially when a generic execute method is also provided.
Changelog
  • examples/basic_client/commands/ping.cjs
    • Updated command definition to utilize the new autoSet method for its name, user option, subcommand, and subcommand group.
  • examples/basic_client/commands/report.cjs
    • Added a new example command demonstrating the autoSet feature for command, user option, subcommand, and subcommand group definitions.
    • Implemented interaction and message handlers that use localization keys for responses and dynamically retrieve localized option names.
  • examples/basic_client/index.js
    • Added the 'command' namespace to the i18n configuration to support command localization.
    • Enabled autoRegisterCommands in the client configuration, streamlining command deployment.
    • Removed the explicit autoRegisterCommands: false setting as it is now handled by the new autoRegisterCommands: true.
  • examples/basic_client/locales/en-US/command.json
    • Added English localization keys for the new report command, including its name, description, and options.
  • examples/basic_client/locales/en-US/test.json
    • Extended the English test localization file with new keys for report command responses.
  • examples/basic_client/locales/tr/command.json
    • Added Turkish localization keys for the new report command, including its name, description, and options.
  • examples/basic_client/locales/tr/test.json
    • Extended the Turkish test localization file with new keys for report command responses.
  • src/structures/builder/Builder.ts
    • Introduced autoSet methods for ApplicationCommandBuilder and various SlashCommandOption types, SlashCommandSubcommandBuilder, and SlashCommandSubcommandGroupBuilder to automatically set names and descriptions.
    • Overrode addStringOption, addIntegerOption, addNumberOption, addBooleanOption, addUserOption, addChannelOption, addRoleOption, addMentionableOption, addAttachmentOption, addSubcommand, and addSubcommandGroup methods to use custom AutoSlashCommand builders.
    • Modified toClientJSON to apply localization to application command JSON using the new localizeApplicationCommand utility.
    • Removed setAliases, addAliases, setPrefixSupport, and setSlashSupport methods, streamlining the builder interface.
  • src/structures/builder/Command.ts
    • Added a createContextHandler helper method to simplify the creation of command execution handlers.
    • Updated supportsSlash and supportsPrefix getters to use Boolean() for clearer checks.
    • Refactored the constructor to use createContextHandler for onMessage and onInteraction callbacks, improving logic for handling execute fallbacks.
  • src/structures/builder/Context.ts
    • Added private helper methods #getFallbackLocale() and #resolveLocale() to centralize locale determination logic.
    • Introduced a private #createTranslator() method to encapsulate translator creation.
    • Refactored the t method to use the new locale resolution logic.
    • Added a getDefaultLocalization method to retrieve localized strings with a fallback mechanism.
    • Updated toJSON methods to use the new #createTranslator() for the t property.
  • src/utils/applicationCommandLocalization.ts
    • Added a new utility file containing localizeApplicationCommand function to apply localization to Discord application command JSON bodies.
    • Implemented getLocales to retrieve supported locales from the i18n instance.
    • Created buildLocalizationMap to generate a map of localized strings for a given path across all supported locales.
    • Developed localizeOption to recursively apply localization to command options, subcommands, and subcommand groups.
  • src/utils/builderAutoSet.ts
    • Added a new utility file defining applyAutoSet to automatically set name and description properties on builder objects.
    • Introduced asCustomBuilder to dynamically cast builder instances to custom builder types, enabling autoSet functionality on Discord.js builders.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@vrdons vrdons requested a review from fhyrox March 8, 2026 10:53
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces some valuable refactoring to the command and context builders, aiming to reduce code duplication and improve structure, especially around command execution handlers and localization. While the overall direction is good, there's a critical logic flaw in the new createContextHandler method that could lead to unexpected behavior by executing both a primary and a fallback handler for the same event. Additionally, there's an opportunity to simplify some of the new localization logic for better readability. Addressing these points will help ensure the new abstractions are both robust and maintainable.

vrdons and others added 4 commits March 8, 2026 13:54
…ariable'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Enforce Discord's requirement that commands do not mix subcommands or
subcommand groups with regular options at the top level. An error is
now thrown if both are present.
- Add resolveInteractionCommand and resolveMessageCommand to support
  localized and aliased command names
- Refactor event handlers to use new resolution methods
- Remove unused "hello" keys from test.json locales
- Expose getDefaultLocalization in context JSON
@vrdons
Copy link
Member Author

vrdons commented Mar 8, 2026

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a powerful "autoSet" feature for command builders and robust localization support. However, it also introduces significant performance issues and a potential for Discord mention injection. The command resolution logic repeatedly performs expensive localization operations, which could lead to Denial of Service, and user-provided usernames are not sanitized, allowing for mention injection when escapeValue: false is used. Additionally, critical issues were found, including missing properties in the ApplicationCommandBuilder's toJSON method and a bug in the new localization logic that can produce invalid command names. There's also a potential feature regression with command aliases and opportunities for code clarity improvements.

vrdons added 5 commits March 8, 2026 14:24
- Add MessageCommandParser for parsing message commands with
  localization and fuzzy matching
- Extend Context to support localization aliases
- Update example commands and locales to use aliases
- Add fastest-levenshtein dependency for fuzzy matching
BREAKING CHANGE: Command aliases are no longer supported. The `aliases`
property and related logic have been removed. Command lookup is now
based
solely on command names and their localizations. Update any code or
configuration relying on aliases to use command names instead.
- Removes manual username interpolation in report command
- Updates locale files to use {{user.name}} and {{author.name}} tokens
- Adds context-aware token resolution to Context.t()
- Centralizes Discord text sanitization and template parsing
- Add template parser registration to Client
- Allow custom template token resolution in Context
- Expose i18next.parser API for managing parsers
@vrdons
Copy link
Member Author

vrdons commented Mar 8, 2026

/gemini review

Replaces regex-based parsing with manual parsing to better handle
malformed or nested template tokens
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant enhancements by adding an autoSet feature for command builders and a comprehensive localization system for commands, along with substantial refactoring of command resolution and builders. However, it introduces two significant security issues: a template injection vulnerability in the translation system that could lead to information disclosure (due to user-supplied interpolation values being processed by a custom template parser, allowing injection and evaluation of bot-internal tokens), and missing access control in the report command example, which allows any user to trigger administrative subcommands. Additionally, areas for improvement include handling command alias conflicts and a bug in the new message command parser that prevents 0 from being a valid option value.

- Prevent resolving tokens not present in the translation string
- Add collectTemplateTokens utility to extract allowed tokens
- Improve command lookup conflict logging
- Allow zero as valid value in MessageCommandParser
@vrdons vrdons merged commit 0c020e5 into main Mar 8, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants