Skip to content
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

feat: Add Modals and Text Inputs #7023

Merged
merged 42 commits into from
Mar 4, 2022

Conversation

suneettipirneni
Copy link
Member

@suneettipirneni suneettipirneni commented Nov 22, 2021

Please describe the changes this PR makes and why it should be merged:

Adds support for modal submit interactions as well as text input components.

Prerequisites:

Ref:

Usage

Sending

import {
  ActionRow,
  TextInputComponent,
  TextInputStyle,
  Modal,
  type ModalActionRowComponent,
} from 'discord.js';

// Create the modal
const modal = new Modal()
  .setTitle('My Awesome Form')
  .setCustomId('AwesomeForm');

// Create text input fields
const tvShowInputComponent = new TextInputComponent()
  .setCustomId('tvField')
  .setLabel('Favorite TV show')
  .setStyle(TextInputStyle.Short);

const haikuInputComponent = new TextInputComponent()
  .setCustomId('haikuField')
  .setLabel('Write down your favorite haiku')
  .setStyle(TextInputStyle.Paragraph);

const rows = [tvShowInputComponent, haikuInputComponent].map(
  (component) =>
    new ActionRow<ModalActionRowComponent>().addComponents(component)
);

// Add action rows to form
modal.addComponents(...rows);

// --- snip ---

// Present the modal to the user
await interaction.showModal(modal);

Receiving

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isModalSubmit()) {
    return;
  }
  
  // Verify we have the right modal
  if (interaction.customId !== 'AwesomeForm') {
    return;
  }

  // Extract text input components from submitted modal
  const [favTVShow, favHaiku] = ['tvField', 'haikuField']
    .map(id => interaction.fields.getTextInputValue(id));
  
  // ACK the interaction
  await interaction.reply(`Favorite TV Show: ${favTVShow}, Favorite haiku: ${favHaiku}`);
});

TODO:

  • Add the rest of the typings
  • Implement the proper type guards
  • Refine and finish doc types and descriptions
  • Fix any discrepancies with data deserialization of components.
  • Throw API errors in unsupported configurations
  • Test

Status and versioning classification:

  • Code changes have been tested against the Discord API, or there are no code changes
  • I know how to update typings and have done so, or typings don't need updating
  • This PR changes the library's interface (methods or parameters added)

Copy link
Contributor

@ImRodry ImRodry left a comment

Choose a reason for hiding this comment

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

Not sure if some of these things were done on purpose due to this being a draft, so sorry if they were!

src/structures/MessageForm.js Outdated Show resolved Hide resolved
src/structures/MessageForm.js Outdated Show resolved Hide resolved
src/structures/BaseMessageComponent.js Outdated Show resolved Hide resolved
src/structures/FormTextInput.js Outdated Show resolved Hide resolved
src/structures/FormTextInput.js Outdated Show resolved Hide resolved
src/structures/FormTextInput.js Outdated Show resolved Hide resolved
src/structures/MessageForm.js Outdated Show resolved Hide resolved
src/structures/MessageForm.js Outdated Show resolved Hide resolved
src/util/Constants.js Outdated Show resolved Hide resolved
typings/enums.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
@suneettipirneni suneettipirneni changed the title Feat: add form/modal/text input interaction support. feat: Add Form interactions Nov 23, 2021
@suneettipirneni suneettipirneni changed the title feat: Add Form interactions feat: Add Modal and Text Input Interactions Nov 23, 2021
src/structures/Modal.js Outdated Show resolved Hide resolved
@iCrawl iCrawl added this to the Version 13.x milestone Nov 23, 2021
typings/index.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
src/structures/Modal.js Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@manuelvleeuwen manuelvleeuwen left a comment

Choose a reason for hiding this comment

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

ModalInputText needs to be added to the MessageActionRowComponent type as well.

typings/enums.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
@suneettipirneni suneettipirneni force-pushed the feat/text-input-interactions branch 2 times, most recently from e25bd3b to 36bfe33 Compare November 23, 2021 20:49
Copy link
Member

@almeidx almeidx left a comment

Choose a reason for hiding this comment

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

receive is spelt as recieve a couple of times in this pull request. (search for the misspelt word in the files tab)

src/structures/Modal.js Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
@iCrawl iCrawl merged commit ed92015 into discordjs:main Mar 4, 2022
@vijayymmeena
Copy link

@suneettipirneni I was trying modal with discord.js@dev, receiving this error, do know why?

image

@ImRodry
Copy link
Contributor

ImRodry commented Mar 4, 2022

You need to install the latest builders dev release too. Please send questions like these to the Discord server though

@suneettipirneni suneettipirneni deleted the feat/text-input-interactions branch March 4, 2022 15:13
@berkbuzcu
Copy link

Any plans to implement the rest of the components for the Modal builder?

@ImRodry
Copy link
Contributor

ImRodry commented Mar 4, 2022

That’s a question you gotta ask Discord

@waki285
Copy link
Contributor

waki285 commented Mar 5, 2022

merge 🎉

@maxschnee-dev
Copy link

quick question. when will this be pushed to production?

@Syjalo
Copy link
Contributor

Syjalo commented Mar 15, 2022

quick question. when will this be pushed to production?

When discord.js v14 will be ready™️

@ACGaming1508
Copy link

I got this error while trying to build a modal:
image

const Discord = require('discord.js');
console.log(Discord.ActionRow);
// => undefined

@megatank58
Copy link
Contributor

I got this error while trying to build a modal:
image

const Discord = require('discord.js');
console.log(Discord.ActionRow);
// => undefined

The pull request has been merged, it's better to make an issue or ask in the discord server, also ActionRow has been renamed to ActionRowBuilder now

Copy link

@ArvidWedtstein ArvidWedtstein left a comment

Choose a reason for hiding this comment

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

.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Interaction-Modals