通过消息 ID 验证消息是否存在

时间:2021-05-27 09:44:49

标签: javascript discord.js

我想通过消息 ID 验证消息是否存在。

此处的代码有效,但如果消息不存在,则不会告诉我“未定义”。

let channel = bot.channels.cache.get("846857716290813962");
const message = channel.messages.fetch("847383565220970497");

UnhandledPromiseRejectionWarning:DiscordAPIError:未知消息

1 个答案:

答案 0 :(得分:0)

您可以只处理 try/catch 块中出现的错误。

下面的函数应该通过ID检查消息是否存在,如果有任何错误,它会捕获错误,console.log()它,并返回false。

const checkIfMessageExists = async (channelId, messageId) => {
    try {
        let channel = bot.channels.cache.get(channelId);
        const message = await channel.messages.fetch(messageId);
        if (message) return true;
    } catch(error) {
        console.log(error.message)
    }
    return false;
}