我编写了让机器人向用户发送私人消息的代码,我需要知道用户是否阻止了机器人或未能向他发送消息。这是我尝试过的。如果失败,我不希望机器人在下面发送消息:消息已发送!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 回答
![?](http://img1.sycdn.imooc.com/533e4c0500010c7602000200-100-100.jpg)
婷婷同学_
TA贡献1844条经验 获得超8个赞
将函数传入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));
});
添加回答
举报
0/150
提交
取消