我编写了让机器人向用户发送私有(private)消息的代码,我需要知道用户是否阻止了机器人或未能向他发送消息。
这是我试过的。如果失败,我不希望机器人在下面发送消息:
Message Sent!
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 ❌`)
});
}
最佳答案
传递一个函数到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));
});
关于javascript - Discord.js 如何知道用户是否阻止了机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57262668/