标题应该很快就能把这个词说出来;我想知道如何检查机器人获取的消息实际上是一条消息还是一个假ID。
我试过了
•如果(!mmm)
•如果(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继续向不存在的通道发送消息,该通道甚至不存在消息;控制台出错。
如果试图获取不存在的消息,将引发以下错误
DiscordapiError:未知消息
您可以使用链接到FetchMessage
的catch
捕获它:
message.channel.fetchMessage('invalid id')
.then(d => console.log('no err', d))
.catch(d => {
console.log('err', d);
// do what you want
);
您还可以在“上面”放置的try
catch
中捕获错误。
因此,不应尝试在then中检查消息是否正确,而应只执行发送消息时应该执行的操作,并在catch中发送警告/错误消息:
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));
});
这应该可以做到,但是您并不是在每个catch/then/if分支中返回值,这意味着存在一些未定义的返回。如果要中止执行的其余部分,只需将return;
或将代码放在If
分支中,而不将else和code放在后面。
我也建议你把你的链接放在新闻线上,以求清晰