Getting an array from folder and sending a random file with discord.js

元气小坏坏 提交于 2020-01-06 14:29:46

问题


I'm trying to create a command, when requested will send a random image from a folder. I don't want to have to name them cause I have a server with a PHP server where my friends can upload images for the bot to post. This is what I have:

 if(command === "meme") {
      const path = '/img/memes/';
      const fs = require('fs');

      fs.readdirSync(path).forEach(file => {
          ranfile = Math.floor(Math.random()*file.length);
          message.channel.sendFile(ranfile);
      })

      return;
 }

When I run the bot with Node.js I get this error:

(node:4840) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

Anyone know what the issue is?


回答1:


This is probably just a regular error wrapped in an UnhandledPromiseRejectionWarning. Have you tried running with --trace-warnings? This should solve the problem of getting to the actual issue by providing a reasonable stack trace.



来源:https://stackoverflow.com/questions/50851030/getting-an-array-from-folder-and-sending-a-random-file-with-discord-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!