如何在 Discord 中创建一个接受用户输入的弹出窗口 window?
How can you create a pop-up window in Discord that accepts an input from the user?
这是我第一次从 Discord 机器人中看到此功能。我试着到处寻找,但似乎我失败了。 Captcha.bot Discord bot 提供了此功能,您可以在其中接受来自 Discord 内弹出窗口 window 的输入。
Captcha.bot 发出的嵌入消息中有一个按钮,您必须在其中回答验证码测试。按下按钮后,它会创建一个像这样的弹出窗口 window。
将正确答案输入验证码机器人后,这是体验的结果。
我只想学习如何使用 Discord.js 召唤弹出窗口 window,如果可能的话,或者至少了解他们是如何做到的。
这些称为模态,它们将在下一个 discord.js 版本 v14 中可用。已经有一个 pull request 了。
同时,您可以使用像 discord-modals or discordjs-modal.
这样的 npm 包
您可以在下面的 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);
这是我第一次从 Discord 机器人中看到此功能。我试着到处寻找,但似乎我失败了。 Captcha.bot Discord bot 提供了此功能,您可以在其中接受来自 Discord 内弹出窗口 window 的输入。
Captcha.bot 发出的嵌入消息中有一个按钮,您必须在其中回答验证码测试。按下按钮后,它会创建一个像这样的弹出窗口 window。
将正确答案输入验证码机器人后,这是体验的结果。
我只想学习如何使用 Discord.js 召唤弹出窗口 window,如果可能的话,或者至少了解他们是如何做到的。
这些称为模态,它们将在下一个 discord.js 版本 v14 中可用。已经有一个 pull request 了。
同时,您可以使用像 discord-modals or discordjs-modal.
这样的 npm 包您可以在下面的 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);