【问题标题】:Discord.js v13 Sending a File Attachment via URLDiscord.js v13 通过 URL 发送文件附件
【发布时间】:2021-10-22 03:39:52
【问题描述】:

Discord.js 发布了 v13,我正在尝试更新我的小自制 discordbot。

我遇到了一个问题,我无法再通过 Web URL 发送附件 (png)。

Discord.js v12

var testchart = `http://jegin.net/testchart2.php?sysid=268.png`;
message.channel.send("Last updated " + updatedAt.fromNow(), {
             files: [{
                attachment: testchart,
                name: 'file.png'
                }]

控制台没有出错(除了折旧警告):

并且机器人不返回图像:

我试过了:

message.channel.send("Last updated " + updatedAt.fromNow(), {
             files: [testchart]

message.channel.send("Last updated " + updatedAt.fromNow(), {
             files: Array.from(testchart)
        });

最后

message.channel.send({
             files: [testchart],
             content: `Last updated ${updatedAt.fromNow()}`
        });

这给了我这个 AWFUL 输出:

感谢您的帮助!

Discord.js 更新指南:https://discordjs.guide/additional-info/changes-in-v13.html#sending-messages-embeds-files-etc

我能找到关于此事的唯一其他问题:Discord.js V13 sending message attachments

【问题讨论】:

  • 在左下角你可以看到testchart2.php

标签: file url discord discord.js attachment


【解决方案1】:

发现问题,与 URL 的 testchart2.php 部分有关 (http://jegin.net/testchart2.php?sysid=268.png)

能够通过以下方式发送它:

message.channel.send({
    files: [{
        attachment: testchart,
        name: 'chart.png'
    }],
    content:`Last updated ${updatedAt.fromNow()}`,
});

基本上,只需将 v12 的内容部分移动到它自己的区域即可。像魅力一样工作。

【讨论】:

    【解决方案2】:

    您的第一次尝试很接近,但并不完全正确。您只需将它们合并在一起(现在发送消息只需要 1 个参数),您将获得一个 png 文件(因为您指定了文件名)以及内容:

    var testchart = `http://jegin.net/testchart2.php?sysid=268.png`;
    message.channel.send({
      content: "Last updated " + updatedAt.fromNow(),
      files: [{
        attachment: testchart,
        name: 'file.png'
      }]
    })
    

    【讨论】:

      猜你喜欢
      • 2021-10-17
      • 2021-12-12
      • 2021-11-14
      • 2016-12-01
      • 1970-01-01
      • 2017-10-30
      • 2022-01-20
      • 2022-01-03
      • 1970-01-01
      相关资源
      最近更新 更多