【问题标题】:How can I fix `DiscordAPIError: Cannot send messages to this user`?如何修复“DiscordAPIError:无法向该用户发送消息”?
【发布时间】:2020-09-24 22:40:54
【问题描述】:
我正在创建一个向作者发送消息的新命令,但是,当作者的 DM 关闭时,它给了我错误 DiscordAPIError: Cannot send messages to this user
if (!message.author.send) {
return message.channel.send("DMs closed.");
} else {
message.author.send("DMs opened")
}
【问题讨论】:
标签:
javascript
node.js
bots
discord
discord.js
【解决方案1】:
好吧,正如你所说,作者的 DM 已关闭,所以它不起作用。你可以试试这样:
//Put the messages you wanted to send the author's DM's
//Add this below it
.catch(error => {
console.error(
`Could not send help DM to ${message.author.tag}.\n`,
error
);
message.reply("it seems like I can't DM you! Do you have DMs disabled?");
});
//If the user has the DM's turned off, then it'll send this in the channel and `console.log` the error
【解决方案2】:
send 函数将始终存在,即使用户 dm 已关闭或打开,要检查 dm 是否已关闭,您可以使用 .catch 捕获 DiscordAPIError 错误。
见:Promise.catch
试试这个:
message.author.send("DMs opened").catch(error => message.channel.send("DMs closed"))