从文件夹获取数组并使用discord.js发送随机文件 [英] Getting an array from folder and sending a random file with discord.js

查看:127
本文介绍了从文件夹获取数组并使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆