消息的消息id,discord.js?"/>
如何先在MongoDB上插入数据,包括即将创建的消息的消息id,discord.js?
这是我需要做的:
- 用户使用生成新消息的不和谐命令。
- 获取将要创建的消息的消息 ID。
- 然后用消息ID向MongoDB插入数据
- 将数据插入数据库后,发送新消息。
我只想在插入数据库成功后显示消息。我不确定如何获取尚未发送的新消息的 ID。这有意义吗?
这是我目前的代码:
if (command === "generatemessage") {
db.collection("doc").insertOne({newMessageId: "", title: "This is new"}) //empty message id, my problem
.then(() => {
if (insert is success) {
const msg = message.channel.send("This is the new message!");
console.log(msg.id); //get the new msg id here
}
})
}
我可以这样做,但这不是我想要的,因为无论插入数据库是否成功,这都会显示消息:
if (command === "generatemessage") {
const msg = message.channel.send("This is the new message!");
db.collection("doc").insertOne({newMessageId: msg.id, title: "This is new"})
}
回答如下:
更多推荐
如何先在MongoDB上插入数据,包括即将创建的消息的消息id,discord.js?
发布评论