这是我第一次在Discord机器人上看到这个功能.我试着到处找,但似乎失败了.验证码中有这个功能.bot Discord bot,您可以从Discord内的弹出窗口接受输入.

在验证码生成的嵌入消息中有一个按钮.你必须回答验证码测试的机器人.按下按钮后,它会创建一个这样的弹出窗口.

enter image description here

在验证码机器人上输入正确答案后,以下是体验的后果.

enter image description here

我想学的就是如何使用Discord调用弹出窗口.如果可能的话,或者至少了解他们是如何做到的.

推荐答案

这些被称为情态动词,它们将在下一个discord中提供.js版本,v14.这个已经有pull request个了.

同时,您可以使用npm包,比如discord-modalsdiscordjs-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);

enter image description here

Javascript相关问答推荐

使用Astro和React的动态API

一次仅播放一个音频

Vega中的模运算符

在页面上滚动 timeshift 动垂直滚动条

如何修复我的js构建表每当我添加一个额外的列作为它的第一列?

CheckBox作为Vue3中的一个组件

阿波罗返回的数据错误,但在网络判断器中是正确的

获取Uint8ClampedArray中像素数组的宽度/高度

如何解决useState错误—setSelect Image不是函数''

我怎么才能得到Kotlin的密文?

如何在文本字段中输入变量?

TypeError:无法分解';React2.useContext(...)';的属性';basename';,因为它为空

基于props 类型的不同props ,根据来自接口的值扩展类型

使用Ace编辑器对子组件实例的native-element 进行Angular 获取时面临的问题

按什么顺序接收`storage`事件?

如何在AG-Grid文本字段中创建占位符

是否可以在不更改组件标识的情况下换出Reaction组件定义(以维护状态/引用等)?如果是这样的话,是如何做到的呢?

JAVASCRIPT SWITCH CASE语句:当表达式为';ALL';

react 路由DOM有条件地呈现元素

如何在Web项目中同步语音合成和文本 colored颜色 更改