How to check if .fetchMessage gets an actual message

一笑奈何 提交于 2019-12-11 15:54:18

问题


The title should get the word out quickly; I'm figuring out how to check if the message the bot fetched is actually a message or a fake ID.

I've tried

if(!mmm)

if(mmm.deleted !== true)

Didn't work.

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)

Expected result

• Bot can tell if the message that was fetched if it's an actual message sent in the channel

Actual result

• Bot proceeds to send a message to a nonexistent channel for a message that doesn't even exist; error in console.


回答1:


If you try to fetch a message that doesn't exist, the following error is raised

DiscordAPIError: Unknown Message

You can catch it with a catch chained to your fetchMessage:

message.channel.fetchMessage('invalid id')
  .then(d => console.log('no err', d))
  .catch(d => {
    console.log('err', d);
    // do what you want
  ); 

you can also catch the error in a try catch placed 'above'.

So instead of trying to check if the message is correct in the then, you should only do what you're supposed to do if the message was send, and send the warning/error message in the 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));
});

that should do it, however you aren't returning value inside every catch/then/if branch, meaning there is some undefined return. If you want to abort the rest of the execution, just to return; or put the code inside a if branch without else and code after.

I also advise you to put your chaining on news lines for clarity



来源:https://stackoverflow.com/questions/57448940/how-to-check-if-fetchmessage-gets-an-actual-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!