从文件夹中获取数组并使用 discord.js 发送随机文件 [英] Getting an array from folder and sending a random file with discord.js
问题描述
我正在尝试创建一个命令,在收到请求时将从文件夹中发送随机图像.我不想为它们命名,因为我有一个带有 PHP 服务器的服务器,我的朋友可以在其中上传图像以供机器人发布.这就是我所拥有的:
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;
}
当我使用 Node.js 运行机器人时,出现此错误:
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?
推荐答案
这可能只是包裹在 UnhandledPromiseRejectionWarning
中的常规错误.您是否尝试过使用 --trace-warnings
运行?这应该通过提供合理的堆栈跟踪来解决解决实际问题的问题.
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.
这篇关于从文件夹中获取数组并使用 discord.js 发送随机文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!