pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何根據(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的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

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

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

例如:機器人會在 DM 的 中詢問你希望標(biāo)題是什么?",然后用戶會用他們想要的標(biāo)題進行響應(yīng),并將該輸入設(shè)置為標(biāo)題.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;如果(命令==='建議'){常量問題 = [{答案:空,字段:'標(biāo)題'},{答案:空,字段:'描述'},{答案:空,字段:'作者姓名'},{答案:空,字段:'顏色'},];讓電流 = 0;//等待消息發(fā)送并抓取返回的消息//所以我們可以添加消息收集器常量發(fā)送 = 等待 message.author.send(`**${questions.length} 的第 1 題:**
您希望 ${questions[current].field} 是什么?`,);常量過濾器=(響應(yīng))=>response.author.id === message.author.id;//在發(fā)送原始問題的 DM 頻道中發(fā)送常量收集器 = sent.channel.createMessageCollector(過濾器,{最大值:問題.長度,時間:分鐘 * 60 * 1000,});//每次收集消息時觸發(fā)收集器.on('收集', (消息) => {//添加答案并增加當(dāng)前索引問題[當(dāng)前++].answer = message.content;const hasMoreQuestions = 當(dāng)前 <問題.長度;如果(有更多問題){消息.作者.發(fā)送(`**問題 ${current + 1} of ${questions.length}:**
你希望 ${questions[current].field} 是什么?`,);}});//當(dāng)超時或達到限制時觸發(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ā)送確認(rèn)消息message.author.send('你想發(fā)表嗎?`y[es]/n[o]`');message.author.dmChannel.awaitMessages(//設(shè)置一個過濾器只接受 y、yes、n 和 no(m) =>['y', 'yes', 'n', 'no'].includes(m.content.toLowerCase()),{最大值:1},).then((coll) => {讓響應(yīng) = 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(如何修復(fù)必須使用導(dǎo)入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時的歡迎消息)
主站蜘蛛池模板: 珠海冷却塔降噪维修_冷却塔改造报价_凉水塔风机维修厂家- 广东康明节能空调有限公司 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 富森高压水枪-柴油驱动-养殖场高压清洗机-山东龙腾环保科技有限公司 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 柴油发电机组_柴油发电机_发电机组价格-江苏凯晨电力设备有限公司 | 污水/卧式/潜水/钻井/矿用/大型/小型/泥浆泵,价格,参数,型号,厂家 - 安平县鼎千泵业制造厂 | 防爆型气象站_农业气象站_校园气象站_农业四情监测系统「山东万象环境科技有限公司」 | 光栅尺厂家_数显表维修-苏州泽升精密机械| 厚壁钢管-厚壁无缝钢管-小口径厚壁钢管-大口径厚壁钢管 - 聊城宽达钢管有限公司 | 乐之康护 - 专业护工服务平台,提供医院陪护-居家照护-居家康复 | 防水套管厂家-柔性防水套管-不锈钢|刚性防水套管-天翔管道 | 热回收盐水机组-反应釜冷水机组-高低温冷水机组-北京蓝海神骏科技有限公司 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 玉米加工设备,玉米深加工机械,玉米糁加工设备.玉米脱皮制糁机 华豫万通粮机 | 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 对辊破碎机-液压双辊式,强力双齿辊,四辊破碎机价格_巩义市金联机械设备生产厂家 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com | 众能联合-提供高空车_升降机_吊车_挖机等一站工程设备租赁 | 广州工业氧气-工业氩气-工业氮气-二氧化碳-广州市番禺区得力气体经营部 | 道达尔润滑油-食品级润滑油-道达尔导热油-合成导热油,深圳道达尔代理商合-深圳浩方正大官网 | 振动时效_振动时效仪_超声波冲击设备-济南驰奥机电设备有限公司 北京宣传片拍摄_产品宣传片拍摄_宣传片制作公司-现像传媒 | 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 贴板式电磁阀-不锈钢-气动上展式放料阀-上海弗雷西阀门有限公司 工业机械三维动画制作 环保设备原理三维演示动画 自动化装配产线三维动画制作公司-南京燃动数字 | 英思科GTD-3000EX(美国英思科气体检测仪MX4MX6)百科-北京嘉华众信科技有限公司 | loft装修,上海嘉定酒店式公寓装修公司—曼城装饰 | 罐体电伴热工程-消防管道电伴热带厂家-山东沃安电气 | 冰晶石|碱性嫩黄闪蒸干燥机-有机垃圾烘干设备-草酸钙盘式干燥机-常州市宝康干燥 | 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | 上海深蓝_缠绕机_缠膜机-上海深蓝机械装备有限公司 | 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 模型公司_模型制作_沙盘模型报价-中国模型网 | 兰州牛肉面加盟,兰州牛肉拉面加盟-京穆兰牛肉面 | 回收二手冲床_金丰旧冲床回收_协易冲床回收 - 大鑫机械设备 | 咖啡加盟-咖啡店加盟-咖啡西餐厅加盟-塞纳左岸咖啡西餐厅官网 | 沈阳楼承板_彩钢板_压型钢板厂家-辽宁中盛绿建钢品股份有限公司 轴承振动测量仪电箱-轴承测振动仪器-测试仪厂家-杭州居易电气 | 精密线材测试仪-电线电缆检测仪-苏州欣硕电子科技有限公司 | 高空重型升降平台_高空液压举升平台_高空作业平台_移动式升降机-河南华鹰机械设备有限公司 | 818手游网_提供当下热门APP手游_最新手机游戏下载 | 商用绞肉机-熟肉切片机-冻肉切丁机-猪肉开条机 - 广州市正盈机械设备有限公司 |