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

如何正確執(zhí)行 createReactionCollection

How to properly do a createReactionCollection(如何正確執(zhí)行 createReactionCollection)
本文介紹了如何正確執(zhí)行 createReactionCollection的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我一直想在用戶按下 Check 或 X 的反應時創(chuàng)建一個事件.但是,當我使用該函數時,我收到一個錯誤消息對象不存在它.

我已經從 awaitReactions 回到了這個,但它沒有工作.

我對 messageSent 對象做了一個 console.log 并得到了這個 Promise { <pending>}

var messageSent = user.send({embed}).then(函數(消息){message.react('?')message.react('?')});messageSent.createReactionCollection(r => ['?','?'].includes(r.emoji.name)).on('收集', r => {if (r.emoji.name == '?') {user.send("已驗證!?")} else if (r.emoji.name == '?') {user.send("取消!?")}});}

TypeError: messageSent.createReactionCollection 不是函數在 app.post (C:Users	eddyDesktopVerifyapp.js:46:25)在 Layer.handle [as handle_request] (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerlayer.js:95:5)在下一個(C:Users	eddyDesktopVerify
ode_modulesexpresslib
outer
oute.js:137:13)在 Route.dispatch (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outer
oute.js:112:3)在 Layer.handle [as handle_request] (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerlayer.js:95:5)在 C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerindex.js:281:22在 Function.process_params (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerindex.js:335:12)在下一個(C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerindex.js:275:10)在 expressInit (C:Users	eddyDesktopVerify
ode_modulesexpresslibmiddlewareinit.js:40:5)在 Layer.handle [as handle_request] (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerlayer.js:95:5)

解決方案

同步與異步

假設您打算接您的朋友去參加體育賽事.你不確定他們希望你什么時候來,所以你給他們打電話問他們.他們想了一會兒,然后告訴你一個時間.你得到了你要求的信息,所以你掛斷了.在編程術語中,這將是同步代碼的一個示例(有時被認為是 Node.js 中的普通"代碼).

讓自己回到同樣的境地.然而,當你這次打電話給你的朋友時,他們很忙.你不想打擾他們,所以你讓他們稍后給你打電話.你掛斷了,但現在你等著.一個小時后,他們給你回電話,告訴你時間.這就是異步代碼的思考過程.

屏幕后面還有很多內容,但為簡單起見,我不會用所有這些信息轟炸你.

<小時>

承諾

<塊引用>

Promise 對象表示異步操作的最終完成(或失敗)及其結果值.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

讓我們分解代碼以更好地理解問題.

  • User.send() 返回一個 Promise.
  • Promise.then() 返回一個 Promise.

因此,您的代碼實際上是這樣的:

var messageSent = Promise -->承諾

<塊引用>

Promise 處于以下狀態(tài)之一:

  • pending:初始狀態(tài),既不滿足也不拒絕.
  • fulfilled:表示操作成功完成.
  • rejected:表示操作失敗.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

<塊引用>

我對 messageSent 對象做了一個 console.log,我得到了這個 Promise { <pending>}

雖然您將變量定義為 Promise,但它并沒有立即完成,因此還沒有返回任何值.它處于 pending 狀態(tài).

<小時>

解決方案

那么,我們如何檢索 Promise 的結果?我們必須等待它.

  • 保持簡單的流程,您可以使用 await 關鍵字.它所做的只是等待 Promise 被履行或拒絕,然后再繼續(xù)執(zhí)行進一步的代碼.考慮以下示例:

    //需要使用'await'的異步上下文(意思是在異步函數中).var messageSent = await user.send(embed);等待 messageSent.react('?');等待 messageSent.react('?');//創(chuàng)建反應收集器.

  • 或者,您可以堅持使用 then() 鏈.回調將在 Promise 實現時使用返回的值調用.在某些情況下,這很簡單.但是,回調很快就會變得混亂,并且返回值的范圍會受到限制.考慮這個例子:

    user.send(embed).then(messageSent => {messageSent.react('?').then(() => messageSent.react('?')).then(() => {//創(chuàng)建反應收集器.});});//請記住,此處的代碼將在 'user.send(embed).' 之后立即執(zhí)行.

  • <塊引用>

    我可能已經修復了它,由于某種原因它沒有返回消息的對象,所以我將 messageSent = message; 添加到 .then

    這適用于您的情況,因為 then() 回調中的值將是已實現的 Promise,并且您將變量設置為返回值.不過,這不是最好的主意.


錯誤處理

當一個 Promise 被拒絕時,這意味著出現了問題.必須捕獲源自被拒絕的 Promise 的錯誤.如果不是,您將在控制臺中收到帶有錯誤的警告.

  • 您可以附加 catch() 方法,這些方法的工作原理與 then() 類似,除了將錯誤作為回調參數返回并且僅在被拒絕時調用.考慮這個簡短的例子:

    user.send(embed).then(messageSent => {...}).catch(console.error);

  • 您可以使用 try...catch 語句,而不是附加多個 catch() 方法.如果 try 塊內的任何 Promise 被拒絕,則執(zhí)行 catch 塊內的代碼.例如:

    try {const user = await bot.fetchUser('someID');等待用戶.發(fā)送(嵌入);} 捕捉(錯誤){控制臺.錯誤(錯誤);}

<小時>

資源

  • Discord.js 文檔
  • MDN 文檔

I've been wanting to create an event when a user presses a reaction that is a Check or X. Although, when I use the function I get an error that it doesn't exist for the message object.

I've already from awaitReactions back to this and it hasn't worked.

EDIT: I did a console.log to the messageSent object and I got this Promise { <pending> }

var messageSent = user.send({embed})
    .then(function (message) {
         message.react('?')
         message.react('?')
     });
messageSent.createReactionCollection(r => ['?','?'].includes(r.emoji.name))
     .on('collect', r => {
         if (r.emoji.name == '?') {
             user.send("Verified! ?")
          } else if (r.emoji.name == '?') {
             user.send("Canceled! ?")
          }
      });   
  }

TypeError: messageSent.createReactionCollection is not a function
    at app.post (C:Users	eddyDesktopVerifyapp.js:46:25)
    at Layer.handle [as handle_request] (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerlayer.js:95:5)
    at next (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outer
oute.js:137:13)
    at Route.dispatch (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outer
oute.js:112:3)
    at Layer.handle [as handle_request] (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerlayer.js:95:5)
    at C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerindex.js:281:22
    at Function.process_params (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerindex.js:335:12)
    at next (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerindex.js:275:10)
    at expressInit (C:Users	eddyDesktopVerify
ode_modulesexpresslibmiddlewareinit.js:40:5)
    at Layer.handle [as handle_request] (C:Users	eddyDesktopVerify
ode_modulesexpresslib
outerlayer.js:95:5)

解決方案

Sync vs Async

Say you're planning on picking your friend up to go to a sporting event. You're not sure when they want you to come, so you call them on the phone and ask them. They think about it for a while, and then tell you a time. You got the information you requested, so you hang up. In programming terms, this would be an example of synchronous code (sometimes thought of as "normal" code in Node.js).

Put yourself back in the same situation. However, when you call your friend this time, they're very busy. You don't want to bother them so you ask them to call you later. You hang up, but now you wait. An hour later, they call you back and tell you the time. This is the thought process of asynchronous code.

There's a lot more that goes on behind the screen, but for simplicity's sake, I'm not going to bombard you with all that information.


Promises

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Let's break the code down to better understand the problem.

  • User.send() returns a Promise.
  • Promise.then() also returns a Promise.

Therefore, your code really looks like this:

var messageSent = Promise --> Promise

A Promise is in one of these states:

  • pending: initial state, neither fulfilled nor rejected.
  • fulfilled: meaning that the operation completed successfully.
  • rejected: meaning that the operation failed.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

I did a console.log to the messageSent object and I got this Promise { <pending> }

Although you defined the variable as a Promise, it isn't completed right away, and therefore no value is returned yet. It's in its pending state.


Solutions

So, how do we retrieve the result of a Promise? We have to wait for it.

  • Keeping a simple flow, you can use the await keyword. All it does is wait for the Promise to be fulfilled or rejected before continuing the execution of further code. Consider the following example:

    // Asynchronous context (meaning within an async function) needed to use 'await.'
    
    var messageSent = await user.send(embed);
    
    await messageSent.react('?');
    await messageSent.react('?');
    
    // Create reaction collector.
    

  • Alternatively, you could stick to then() chains. The callback will be called with the returned value upon the fulfillment of the Promise. In some contexts, this is simple. However, callbacks can get messy very quickly, and the scope of the returned values will be limited. Consider this example:

    user.send(embed)
      .then(messageSent => {
        messageSent.react('?')
          .then(() => messageSent.react('?'))
            .then(() => {
              // Create reaction collector.
            });
      });
    
    // Keep in mind that the code here will be executed immediately after 'user.send(embed).'
    

  • I might have fixed it, for some reason it wasn't returning the object of the message so I added messageSent = message; to the .then

    This works in your case because the value in the then() callback will be the fulfilled Promise, and you're setting the variable to the returned value. This isn't the best idea, though.


Error Handling

When a Promise is rejected, it means something went wrong. Errors originating from rejected Promises must be caught. If they aren't, you'll receive a warning in the console with the error.

  • You can attach catch() methods which will work similarly to then(), except returning the error as its callback parameter and only being called upon rejection. Consider this short example:

    user.send(embed)
      .then(messageSent => {...})
      .catch(console.error);
    

  • Instead of attaching multiple catch() methods, you can use a try...catch statement. If any Promises inside of the try block are rejected, the code inside the catch block is executed. For example:

    try {
      const user = await bot.fetchUser('someID');
      await user.send(embed);
    } catch(err) {
      console.error(err);
    }
    


Resources

  • Discord.js Documentation
  • MDN Documentation

這篇關于如何正確執(zhí)行 createReactionCollection的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 服務器時的歡迎消息)
主站蜘蛛池模板: 网站建设-临朐爱采购-抖音运营-山东兆通网络科技 | 密集柜_档案密集柜_智能密集架_密集柜厂家_密集架价格-智英伟业 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 档案密集架_电动密集架_移动密集架_辽宁档案密集架-盛隆柜业厂家现货批发销售价格公道 | 永嘉县奥阳陶瓷阀门有限公司| 餐饮小吃技术培训-火锅串串香培训「何小胖培训」_成都点石成金[官网] | 传动滚筒,改向滚筒-淄博建凯机械科技有限公司 | 胶泥瓷砖胶,轻质粉刷石膏,嵌缝石膏厂家,腻子粉批发,永康家德兴,永康市家德兴建材厂 | 凝胶成像仪,化学发光凝胶成像系统,凝胶成像分析系统-上海培清科技有限公司 | 变位机,焊接变位机,焊接变位器,小型变位机,小型焊接变位机-济南上弘机电设备有限公司 | 重庆网站建设,重庆网站设计,重庆网站制作,重庆seo,重庆做网站,重庆seo,重庆公众号运营,重庆小程序开发 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 天长市晶耀仪表有限公司| 安德建奇火花机-阿奇夏米尔慢走丝|高维|发那科-北京杰森柏汇 | 北京自然绿环境科技发展有限公司专业生产【洗车机_加油站洗车机-全自动洗车机】 | 船用烟火信号弹-CCS防汛救生圈-船用救生抛绳器(海威救生设备) | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 莱州网络公司|莱州网站建设|莱州网站优化|莱州阿里巴巴-莱州唯佳网络科技有限公司 | 卫生人才网-中国专业的医疗卫生医学人才网招聘网站! | 产业规划_产业园区规划-产业投资选址及规划招商托管一体化服务商-中机院产业园区规划网 | 采暖炉_取暖炉_生物质颗粒锅炉_颗粒壁炉_厂家加盟批发_烟台蓝澳采暖设备有限公司 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | 东莞画册设计_logo/vi设计_品牌包装设计 - 华略品牌设计公司 | 医学动画公司-制作3d医学动画视频-医疗医学演示动画制作-医学三维动画制作公司 | 兰州UPS电源,兰州山特UPS-兰州万胜商贸 | 哲力实业_专注汽车涂料汽车漆研发生产_汽车漆|修补油漆品牌厂家 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | pbootcms网站模板|织梦模板|网站源码|jquery建站特效-html5模板网 | 气象监测系统_气象传感器_微型气象仪_气象环境监测仪-山东风途物联网 | 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 防腐储罐_塑料储罐_PE储罐厂家_淄博富邦滚塑防腐设备科技有限公司 | 卧涛科技有限公司科技项目申报公司|高新技术企业申报|专利申请 | 电竞馆加盟,沈阳网吧加盟费用选择嘉棋电竞_售后服务一体化 | 北京印刷厂_北京印刷_北京印刷公司_北京印刷厂家_北京东爵盛世印刷有限公司 | 铝箔-铝板-花纹铝板-铝型材-铝棒管-上海百亚金属材料有限公司 | 建筑资质代办-建筑资质转让找上海国信启航 | 强效碱性清洗剂-实验室中性清洗剂-食品级高纯氮气发生器-上海润榕科学器材有限公司 | 济宁工业提升门|济宁电动防火门|济宁快速堆积门-济宁市统一电动门有限公司 | 欧盟ce检测认证_reach检测报告_第三方检测中心-深圳市威腾检验技术有限公司 | 焊缝跟踪系统_激光位移传感器_激光焊缝跟踪传感器-创想智控 | 发电机组|柴油发电机组-批发,上柴,玉柴,潍柴,康明斯柴油发电机厂家直销 | 【星耀裂变】_企微SCRM_任务宝_视频号分销裂变_企业微信裂变增长_私域流量_裂变营销 |