Discord.js 如何知道用户是否屏蔽了机器人

3
我编写了一段代码,让机器人将用户发送到私人消息,我需要知道用户是否阻止了机器人或未能向他发送消息。
这是我尝试过的内容。如果失败,我不希望机器人发送以下消息:

消息已发送!

image

if(message.content.startsWith(prefix+'send')){
    if(!botOwner.includes(message.author.id)) return;
    let userID = 'ID'; //to send
    let channel = message.guild.channels.get(message.channel.id);
    let user = bot.users.get(userID);
    let myMessage = message.content.split(' ').slice(1).join(' ');
    if(!myMessage) return;


    user.send(myMessage)
    .then(channel.send('Message Sent!')) //The problem in this line <-
    .catch(err => {
        console.error(err)
        message.channel.send(`**${user.username}**, Failed to send him ❌`)
    });
}
2个回答

4
将一个函数传递给then()方法。
// Keep in mind that 'user' is a GuildMember the way it's been defined, not a User.

user.send(myMessage)
  .then(() => message.channel.send(':incoming_envelope: Message sent successfully!'))
  .catch(err => {
    console.error(`Error while sending message to ${user.displayName}...\n`, err);

    message.channel.send(':x: Message could not be sent.')
      .catch(err => console.error(`Error while sending error message...\n`, err));
  });

-3
我会这样做:
var error = false;

user.send(myMessage)
 .catch(err => {

  error = true;

  console.error(err)
  message.channel.send(`**${user.displayName}**, Failed to send message. ❌`)

 });

if (!error) channel.send('Message Sent!')

if (!error) 存在问题。当我阻止机器人时,它会显示“消息已发送”和错误消息。 当我使用 if (error) 时,不会显示“消息已发送”。你能试一下吗? - Anas7z
我现在无法测试,但也许你是将 if (!error) 放在了 .catch() 之后.then() 中。 - Xge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接