我编写了让机器人将用户发送到私人消息的代码,我需要知道用户是阻止了机器人还是未能向他发送消息。
这是我尝试过的。如果机器人失败,我不希望它在下面发送消息:
消息已发送!
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 = message.guild.members.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.displayName}**, Failed to send him ❌`)
});
}
答案 0 :(得分:3)
将函数传递到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));
});
答案 1 :(得分:-2)
我会这样:
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!')