【问题标题】:Discord.js looking for nonexistent directoryDiscord.js 寻找不存在的目录
【发布时间】:2020-12-22 01:36:23
【问题描述】:

我正在使用 Discord.js 编写一个 Discord Bot,我正在让该机器人从一组中发送一个随机图像:

client.on('message', msg => {
   if (msg.content === 'I Hate It'){
       const randImg = ['https://i.imgur.com/C0gR27R.png', 'https://i.imgur.com/qErVhIm.jpg'];
       msg.channel.send("", { files: randImg[Math.floor(Math.random() * randImg.length)]});
   } 
});

但是,当我运行代码并输入“我讨厌它”时,nodemon 给了我这个错误:

(node:13) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat '/home/container/h'
(node:13) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

根据错误,它正在尝试查找一个不存在的目录,但在代码中没有告诉它查找目录。我知道这是那个特定的块,因为所有其他命令和机器人的一部分都可以正常工作。我该如何解决这个问题?

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    您无需发送文件,因为它不在您的设备上,您只需发送链接即可。 Discord 会将 imgur 链接变成照片

    msg.channel.send(randImg[Math.floor(Math.random() * randImg.length)])
    

    【讨论】:

    • 我忘了你可以将常量作为常量发送,谢谢。
    【解决方案2】:

    MessageOptions 对象的“文件”属性是一个数组。也许,添加数组括号应该可以解决您的问题。

    client.on('message', msg => {
       if (msg.content === 'I Hate It'){
           const randImg = ['https://i.imgur.com/C0gR27R.png', 'https://i.imgur.com/qErVhIm.jpg'];
           msg.channel.send("", {
             files: [ // bracket
               randImg[Math.floor(Math.random() * randImg.length)]
             ] // bracket
           });
       } 
    });
    

    有用的链接:

    MessageOptions | discord.js

    TextChannel#send | discord.js

    【讨论】:

      猜你喜欢
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 2018-04-15
      • 2011-09-17
      • 2018-01-09
      • 2020-05-26
      • 2020-11-24
      相关资源
      最近更新 更多