我想通过消息 ID 验证消息是否存在。
此处的代码有效,但如果消息不存在,则不会告诉我“未定义”。
let channel = bot.channels.cache.get("846857716290813962");
const message = channel.messages.fetch("847383565220970497");
UnhandledPromiseRejectionWarning:DiscordAPIError:未知消息
答案 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;
}