问题
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