Discord.jc / ReferenceError:未定义MessageAttachment

问题描述

我收到此错误,看来该漫游器找不到图像。

如何定义图像的路径?它位于:C:\Users\Yanzi\Desktop\LaisaBot\Emotes

const discord = require("discord.js");
const config = require("./config.json");

const client = new discord.Client();

const prefix = "!";

client.on("message",function(message) {
    if (message.author.bot) return;
    if (!message.content.startsWith(prefix)) return;

    const commandBody = message.content.slice(prefix.length);
    const args = commandBody.split(' ');
    const command = args.shift().toLowerCase();

    if (command === "ping") {
        const tiMetaken = Date.Now() - message.createdTimestamp;
        message.reply(`Pong! This message had a latency of ${tiMetaken}ms.`);
    } else if (command === "sum") {
        const numArgs = args.map(x => parseFloat(x));
        const sum = numArgs.reduce((counter,x) => counter += x);
        message.reply(`The sum of all the arguments you provided is ${sum}!`);
    }

    // If the message is '!rip'
    else if (message.content === '!rip') {
        // Create the attachment using MessageAttachment
        const attachment = new MessageAttachment('./laisa2.png');
        // Send the attachment in the message channel with a content
        message.channel.send(`${message.author},`,attachment);
    }
});

client.login(config.BOT_TOKEN);

解决方法

您尚未定义MessageAttachment时就调用它。

它是您在顶部需要的Discord库的一部分,因此您有两种解决方案:

  1. 使用new Discord.MessageAttachment代替new MessageAttachment
  2. 在需要顶部Discord的地方将其替换为{ MessageAttachment },请注意,在您进一步调用,client时,还需要向该对象添加Discord.Client和其他对象。这将导致const { MessageAttachment,Client } = require('discord.js');

希望对您有所帮助,请务必阅读文档并查看一些教程。

您可以在此处查看有关销毁的信息:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment https://dmitripavlutin.com/javascript-object-destructuring/