Discord.js:MessageAttachment 未定义
2020-04-23
8930
我无法让我的机器人过滤消息并使用计算机中的本地文件进行响应。代码如下:
client.on("message", msg => {
console.log(msg.content);
let wordArray = msg.content.split(" ")
console.log(wordArray)
let filterWords = ['test']
for(var i = 0; i < filterWords.length; i++) {
if(wordArray.includes(filterWords[i])) {
msg.delete()
// Create the attachment using MessageAttachment
const attachment = new MessageAttachment('cbcfilter.png');
msg.channel.send(attachment)
}
}
});
它给出了此错误消息:
ReferenceError: MessageAttachment is not defined
at Client.<anonymous> (/Users/DShirriff/cbcbot/bot.js:108:26)
at Client.emit (events.js:323:22)
at MessageCreateAction.handle (/Users/DShirriff/cbcbot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/Users/DShirriff/cbcbot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/Users/DShirriff/cbcbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
at WebSocketShard.onPacket (/Users/DShirriff/cbcbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:435:22)
at WebSocketShard.onMessage (/Users/DShirriff/cbcbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
at WebSocket.onMessage (/Users/DShirriff/cbcbot/node_modules/ws/lib/event-target.js:120:16)
at WebSocket.emit (events.js:311:20)
at Receiver.receiverOnMessage (/Users/DShirriff/cbcbot/node_modules/ws/lib/websocket.js:801:20)
我是不是太笨了,漏掉了一个简单的括号,还是我应该寻找此行代码的其他形式?
2个回答
您需要导入
MessageAttachment
。
如果您使用
require
导入 discord.js,那么您可以执行以下操作:
const Discord = require('discord.js')
const attachment = new Discord.MessageAttachment('cbcfilter.png');
如果您使用
import
,那么您可以执行以下操作:
import { MessageAttachment } from 'discord.js'
const attachment = new MessageAttachment('cbcfilter.png');
附注
:您不必构建
MessageAttachment
,您只需执行
msg.channel.send({files: ['cbcfilter.png']})
或
msg.channel.send({
files: [{
attachment: 'entire/path/to/cbcfilter.png',
name: 'cbcfilter.png'
}]
})
Syntle
2020-04-24
在 discord.js v14 中,它已更改为 AttachmentBuilder
return await interaction.editReply({
content: responseText,
ephemeral: true,
files: [new AttachmentBuilder(Buffer.from(codeResponseText), {name: 'codes.txt'})]
});
stackers
2024-02-29