我创建了一个 bot discord 并且需要一个命令 !edit [id] [new content] 所以我做到了:
if (MessageContent.startsWith('!edit ')) {
if (autority >= 3) {
//if false
if (args.length < 3) {
const modifErrorEmbed = new discord.MessageEmbed()
.setColor('#eb9234')
.setTitle('You made an error')
.setDescription('To execute the command do: !edit <id> <Nouveau message>')
m.channel.send(modifErrorEmbed)
}
//if correct
if (args.length >= 3) {
let id = args[1]
let toRemove = '!edit ' + id + ' '
let toResend = MsgC.replace(toRemove, '')
const toModif = m.channel.messages.cache.find((x) => (x.id === id))
toModif.channel.messages.fetch(toModif.id)
const modifModifierEmbed = new discord.MessageEmbed()
.setColor('#eb9234')
.setDescription(toResend)
//we edit the message
toModif.edit(modifModifierEmbed)
}
}
}
像这样,我可以很好地编辑缓存的消息(在上次机器人激活后发送),但尝试使用非缓存的消息来执行它会返回一个错误:
^
TypeError: Cannot read property 'edit' of undefined
at Client.<anonymous> (C:\Users\draymone\Desktop\VSC\draymone\calypsiovalleybot\index.js:174:33)
at Client.emit (events.js:314:20)
at MessageCreateAction.handle (C:\Users\draymone\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\draymone\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\draymone\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
at WebSocketShard.onPacket (C:\Users\draymone\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
at WebSocketShard.onMessage (C:\Users\draymone\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
at WebSocket.onMessage (C:\Users\miloud\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:314:20)
at Receiver.receiverOnMessage (C:\Users\draymone\node_modules\ws\lib\websocket.js:797:20)
请问我该如何解决?
编辑: 解决方案是:
message.channel.messages.fetch(id).then(msg => {
msg.edit(modifModifierEmbed)
})
答案 0 :(得分:1)
我不确定这会解决它,但您应该尝试直接获取消息,而不是先找到它然后再获取它。
此外,如果您希望 fetch() 函数实际执行某些操作,则需要编辑该变量,这也意味着您需要将 toModif 更改为 var
或 let
。>
我的意思是说你需要做的是:
let toModif = m.channel.messages.cache.find((x) => (x.id === id))
toModif = toModif.channel.messages.fetch(toModif.id)
尝试第一个建议,如果不起作用,请尝试第二个。我希望我正确理解你的问题:)