久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何根據(jù)通過提示從用戶輸入收集的輸入進行自

How to make a custom embed based on input gathered from user input through a prompt - Discord.js(如何根據(jù)通過提示從用戶輸入收集的輸入進行自定義嵌入 - Discord.js)
本文介紹了如何根據(jù)通過提示從用戶輸入收集的輸入進行自定義嵌入 - Discord.js的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試創(chuàng)建一個建議命令,如果用戶在服務器中鍵入 =suggest,它將在他們的 DM 中創(chuàng)建一個提示,要求不同的規(guī)范.

但是,我不確定如何獲取用戶輸入并在嵌入中使用該輸入.我該怎么做?

例如:機器人會在 DM 的 中詢問你希望標題是什么?",然后用戶會用他們想要的標題進行響應,并將該輸入設置為標題.p>

解決方案

你可以使用消息收集器.我使用

我剛剛注意到您想通過 DM 發(fā)送它.在這種情況下,您可以 .send() 第一條消息,等待它被解析,這樣你就可以使用 channel.createMessageCollector().我添加了一些評論來解釋它:

const prefix = '!';client.on('message', async (message) => {如果 (message.author.bot || !message.content.startsWith(prefix)) 返回;const args = message.content.slice(prefix.length).split(/+/);常量命令 = args.shift().toLowerCase();常量分鐘 = 5;如果(命令==='建議'){常量問題 = [{答案:空,字段:'標題'},{答案:空,字段:'描述'},{答案:空,字段:'作者姓名'},{答案:空,字段:'顏色'},];讓電流 = 0;//等待消息發(fā)送并抓取返回的消息//所以我們可以添加消息收集器常量發(fā)送 = 等待 message.author.send(`**${questions.length} 的第 1 題:**
您希望 ${questions[current].field} 是什么?`,);常量過濾器=(響應)=>response.author.id === message.author.id;//在發(fā)送原始問題的 DM 頻道中發(fā)送常量收集器 = sent.channel.createMessageCollector(過濾器,{最大值:問題.長度,時間:分鐘 * 60 * 1000,});//每次收集消息時觸發(fā)收集器.on('收集', (消息) => {//添加答案并增加當前索引問題[當前++].answer = message.content;const hasMoreQuestions = 當前 <問題.長度;如果(有更多問題){消息.作者.發(fā)送(`**問題 ${current + 1} of ${questions.length}:**
你希望 ${questions[current].field} 是什么?`,);}});//當超時或達到限制時觸發(fā)collector.on('end', (collected, reason) => {如果(原因 === '時間'){返回消息.作者.發(fā)送(`我并不是說你很慢,但你只在 ${MINUTES} 分鐘內(nèi)回答了 ${questions.length} 中的 ${collected.size} 個問題.我放棄了.`,);}常量嵌入 = 新 MessageEmbed().setTitle(問題[0].answer).setDescription(問題[1].answer).setAuthor(問題[2].answer).setColor(問題[3].answer);message.author.send('這是你的嵌入:');message.author.send(嵌入);//發(fā)送確認消息message.author.send('你想發(fā)表嗎?`y[es]/n[o]`');message.author.dmChannel.awaitMessages(//設置一個過濾器只接受 y、yes、n 和 no(m) =>['y', 'yes', 'n', 'no'].includes(m.content.toLowerCase()),{最大值:1},).then((coll) => {讓響應 = coll.first().content.toLowerCase();if (['y', 'yes'].includes(response)) {//發(fā)布嵌入,例如在頻道中發(fā)送等//然后讓成員知道你已經(jīng)完成了message.author.send('太好了,嵌入已發(fā)布.');} 別的 {//沒什么可做的,只是讓他們知道發(fā)生了什么message.author.send('發(fā)布嵌入被取消.');}}).catch((coll) => console.log('handle error'));});}});

PS:您可以在 DiscordJS.guide.

I'm attempting to make a suggestions command, where if the user types =suggest in the server, it would create a prompt in their DM's, asking for different specifications.

However, I am unsure how to obtain user input and use that input in an embed. How would I do this?

For example: The bot would ask in DM's "What would you like the title to be?", and then the user would respond with their desired title, and it would set that input as the title.

解決方案

You can use a message collector. I used channel.awaitMessages here:

const prefix = '!';

client.on('message', (message) => {
  if (message.author.bot || !message.content.startsWith(prefix)) return;
  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();

  if (command === 'suggest') {
    // send the question
    message.channel.send('What would you like the title to be?');

    // use a filter to only collect a response from the message author
    const filter = (response) => response.author.id === message.author.id;
    // only accept a maximum of one message, and error after one minute
    const options = { max: 1, time: 60000, errors: ['time'] };

    message.channel
      .awaitMessages(filter, options)
      .then((collected) => {
        // the first collected message's content will be the title
        const title = collected.first().content;
        const embed = new MessageEmbed()
          .setTitle(title)
          .setDescription('This embed has a custom title');

        message.channel.send(embed);
      })
      .catch((collected) =>
        console.log(`After a minute, only collected ${collected.size} messages.`),
      );
  }
});

I've just noticed that you wanted to send it in a DM. In that case, you can .send() the first message, wait for it to be resolved, so you can add a message collector in the channel using channel.createMessageCollector(). I've added some comments to explain it:

const prefix = '!';

client.on('message', async (message) => {
  if (message.author.bot || !message.content.startsWith(prefix)) return;

  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();
  const MINUTES = 5;

  if (command === 'suggest') {
    const questions = [
      { answer: null, field: 'title' },
      { answer: null, field: 'description' },
      { answer: null, field: 'author name' },
      { answer: null, field: 'colour' },
    ];
    let current = 0;

    // wait for the message to be sent and grab the returned message
    // so we can add the message collector
    const sent = await message.author.send(
      `**Question 1 of ${questions.length}:**
What would you like the ${questions[current].field} be?`,
    );

    const filter = (response) => response.author.id === message.author.id;
    // send in the DM channel where the original question was sent
    const collector = sent.channel.createMessageCollector(filter, {
      max: questions.length,
      time: MINUTES * 60 * 1000,
    });

    // fires every time a message is collected
    collector.on('collect', (message) => {
      // add the answer and increase the current index
      questions[current++].answer = message.content;
      const hasMoreQuestions = current < questions.length;

      if (hasMoreQuestions) {
        message.author.send(
          `**Question ${current + 1} of ${questions.length}:**
What would you like the ${questions[current].field} be?`,
        );
      }
    });

    // fires when either times out or when reached the limit
    collector.on('end', (collected, reason) => {
      if (reason === 'time') {
        return message.author.send(
          `I'm not saying you're slow but you only answered ${collected.size} questions out of ${questions.length} in ${MINUTES} minutes. I gave up.`,
        );
      }

      const embed = new MessageEmbed()
        .setTitle(questions[0].answer)
        .setDescription(questions[1].answer)
        .setAuthor(questions[2].answer)
        .setColor(questions[3].answer);

      message.author.send('Here is your embed:');
      message.author.send(embed);
      // send a message for confirmation
      message.author.send('Would you like it to be published? `y[es]/n[o]`');
      message.author.dmChannel
        .awaitMessages(
          // set up a filter to only accept y, yes, n, and no
          (m) => ['y', 'yes', 'n', 'no'].includes(m.content.toLowerCase()),
          { max: 1 },
        )
        .then((coll) => {
          let response = coll.first().content.toLowerCase();
          if (['y', 'yes'].includes(response)) {
            // publish embed, like send in a channel, etc
            // then let the member know that you've finished
            message.author.send('Great, the embed is published.');
          } else {
            // nothing else to do really, just let them know what happened
            message.author.send('Publishing the embed is cancelled.');
          }
        })
        .catch((coll) => console.log('handle error'));
    });
  }
});

PS: You can learn more about message collectors on DiscordJS.guide.

這篇關(guān)于如何根據(jù)通過提示從用戶輸入收集的輸入進行自定義嵌入 - Discord.js的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發(fā)出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 色网站在线 | av大片 | 伊人久操| 欧美日韩专区 | 日本亚洲精品成人欧美一区 | 欧美日韩电影一区二区 | 久久激情视频 | 成人欧美一区二区三区黑人孕妇 | 欧美极品少妇xxxxⅹ免费视频 | 人碰人操 | аⅴ资源新版在线天堂 | 欧美一区二区三区久久精品 | 国产又色又爽又黄又免费 | 成人久久视频 | 精品中文字幕一区二区三区 | 三级av在线 | 亚洲一一在线 | 日韩精品免费在线观看 | 欧日韩在线观看 | 亚洲成人av | 精品啪啪 | 欧美激情久久久 | 岛国av免费在线观看 | 大乳boobs巨大吃奶挤奶 | 国产欧美在线播放 | 亚洲第一在线视频 | 国产精品视频一二三区 | 天天色天天色 | 亚洲第一福利视频 | 女人毛片a毛片久久人人 | 欧美亚洲另类在线 | 成人免费视频 | 日产精品久久久一区二区福利 | 亚洲欧美日韩一区二区 | 黄色成人免费看 | 91久久 | 久久久91 | 久久影院一区 | 欧美电影在线观看网站 | 亚洲婷婷一区 | 欧美在线天堂 |