如何解决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库的一部分,因此您有两种解决方案:
- 使用
new Discord.MessageAttachment
代替new MessageAttachment
- 在需要顶部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/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。