这些被称为情态动词,它们将在下一个discord中提供.js版本,v14.这个已经有pull request个了.
同时,您可以使用npm包,比如discord-modals或discordjs-modal.
你可以在下面的discord-modals
软件包中找到一个工作示例.别忘了先用npm i discord-modals
安装.
const {
Client,
Intents,
MessageActionRow,
MessageButton,
} = require('discord.js');
const discordModals = require('discord-modals');
const { Modal, TextInputComponent, showModal } = discordModals;
const TOKEN = 'YOUR TOKEN HERE';
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});
discordModals(client);
client.on('messageCreate', (message) => {
if (message.author.bot) return;
let button = new MessageActionRow();
button.addComponents(
new MessageButton()
.setCustomId('verification-button')
.setStyle('PRIMARY')
.setLabel('Open modal dialog'),
);
message.reply({
components: [button],
});
});
client.on('interactionCreate', async (interaction) => {
if (interaction.isButton()) {
if (interaction.customId === 'verification-button') {
const modal = new Modal() // We create a Modal
.setCustomId('verification-modal')
.setTitle('Verify yourself')
.addComponents([
new TextInputComponent()
.setCustomId('verification-input')
.setLabel('Answer')
.setStyle('SHORT')
.setMinLength(4)
.setMaxLength(12)
.setPlaceholder('ABCDEF')
.setRequired(true),
]);
showModal(modal, {
client,
interaction,
});
}
}
});
client.on('modalSubmit', async (modal) => {
if (modal.customId === 'verification-modal') {
const response = modal.getTextInputValue('verification-input');
modal.reply(`Yay, your answer is submitted: "${response}"`);
}
});
client.once('ready', () => {
console.log('Bot v13 is connected...');
});
client.login(TOKEN);