标题应该很快就使人明白。我正在弄清楚如何检查机器人获取的消息实际上是消息还是伪造的ID。
我已经尝试过
•if(!mmm)
•if(mmm.deleted !== true)
没用。
if (!args.length) return await message.channel.send(`You didn't provide a ID!`).then(msg => {msg.delete(30000)}).then(message.delete(50)).catch(error => console.log(error));
else
if (isNaN(args)) {
return message.reply('that doesn\'t seem to be a valid number.').then(msg => {msg.delete(30000)}).then(message.delete(50)).catch(error => console.log(error));
}
else message.channel.send(`Fetching: ${args}`).then(msg => {msg.delete(30000)}).then(message.delete(50)).catch(error => console.log(error));
message.channel.fetchMessage(args).then(async (mmm) => {
console.log(mmm)
if(mmm) return await message.reply(`it appears that the ID you sent was invalid.`).then(msg => {msg.delete(30000)}).then(message.delete(50)).catch(error => console.log(error));
else
await mmm.channel.send(`hi`)
/**
* @ending for .then((mmm) => {`code`})
*/
await message.channel.send('<:Approve:596458828011405334> Finished processes.').then(msg => {msg.delete(5000)}).then(message.delete(50)).catch(error => console.log(error))
}).catch(async (error) => {console.log(error)
预期结果
•Bot可以判断收到的消息是否是在频道中发送的实际消息
实际结果
•Bot继续将消息发送到不存在的消息,甚至不存在;控制台错误。
答案 0 :(得分:0)
如果您尝试获取不存在的消息,则会引发以下错误
DiscordAPIError: Unknown Message
您可以通过链接到catch
的{{1}}来捕获它:
fetchMessage
您还可以在message.channel.fetchMessage('invalid id')
.then(d => console.log('no err', d))
.catch(d => {
console.log('err', d);
// do what you want
);
try
置于“上方”的位置捕获错误。
因此,与其尝试在那时检查消息是否正确,不如仅在发送消息时执行应做的事情,然后在catch中发送警告/错误消息:
catch
应该这样做,但是您不会在每个catch / then / if分支内返回值,这意味着存在一些未定义的返回值。如果要中止执行的其余部分,只需message.channel.fetchMessage(args).then(async (mmm) => {
console.log(mmm);
await mmm.channel.send(`hi`);
/**
* @ending for .then((mmm) => {`code`})
*/
await message.channel.send('<:Approve:596458828011405334> Finished processes.')
.then(msg => {msg.delete(5000);})
.then(message.delete(50))
.catch(error => console.log(error));
}).catch(async (error) => {
console.log('err', error);
return await message.reply(`it appears that the ID you sent was invalid.`)
.then(msg => msg.delete(30000))
.then(message.delete(50))
.catch(error => console.log(error));
});
或将代码放入return;
分支中,而无需其他代码,之后再编写代码。
我也建议您将链接放在新闻行上以使其清晰