有没有办法在从节点服务器发送msg到通道时获取Discord消息ID?

问题描述 投票:0回答:1

在Express / Node.js应用程序中使用Discord.js,我正在尝试构建一个定期抓取外部数据的机器人,并使用包含该数据的某些元素的嵌入消息来更新Discord。我正在尝试添加一项功能,以检查该数据是否已从外部源删除(下次抓取时不再存在),然后删除包含已发送数据的Discord中的特定消息。

Discord中发布的某些消息可能包含重复的数据项,因此我希望按特定的消息ID删除,但似乎在发布到Discord时分配了消息ID。

有没有办法以编程方式从Discord.js发送时获取或返回此消息ID,而不是从Discord GUI手动复制/粘贴消息ID?换句话说,如果它看到msg的源数据不再被抓取,我需要我的机器人知道要删除哪条消息。

// FOR-LOOP TO POST DATA TO DISCORD
// see if those IDs are found in persistent array
        for (var i = 0; i < newIDs.length; i++) {
            if (currentIDs.indexOf(newIDs[i]) == -1) {
                currentIDs.push(newIDs[i]); // add to persistent array
                TD.getTicket(33, newIDs[i]) // get ticket object
                .then(ticket => { postDiscord(ticket); }) // post to DISCORD!
            }
        }

        // if an ID in the persistent array is not in temp array,
        // delete from persistent array & existing DISCORD msg.
        // message.delete() - need message ID to get msg object...
        // var msg = channel.fetchMessage(messageID) ?
node.js express discord.js
1个回答
0
投票

我来推荐你:

https://discord.js.org/#/docs/main/stable/class/Message

假设你正在使用async / await,你会有类似的东西:

async () => {
 let message = await channel.send(some message);
 //now you can grab the ID from it like
 console.log(message.id)
}

如果你打算使用.then作为承诺,那就是同样的想法:

channel.send(some message)
    .then(message => {
     console.log(message.id)
     });

ID是邮件的属性,您只能在收到Discord API的响应后获取ID。这意味着您必须异步处理它们。

© www.soinside.com 2019 - 2024. All rights reserved.