[英]discord.js checking if a DM went through and sending a message if it did or not
[英]Check if a Discord.js DM went through
我知道你可以像這樣發送 DM:
message.author.send("Go to example.com for help");
但是有些人的設置允許其他服務器成員將他們關閉:
如何確定 DM 是否實際發送? 我知道這在 Discord API 中是可能的,因為其他機器人可以做到。
如果用戶有那個選項,或者不是 DMable,它會拋出一個錯誤,即:
DiscordAPIError: Cannot send messages to this user
現在,我們可以捕獲該錯誤並基於它運行一個命令,例如在頻道中回復用戶不能被 DMed。
user.send(...).catch(async err => message.reply("I can't DM this user"));
// In the above, async is useless, but you can actually do what you want with it.
要運行多行命令,請使用基於承諾的 catch。
user.send(...).catch(async err => {
console.log(err);
message.reply("I can't DM this user");
});
如果用戶啟用了該選項,DMing 他們將返回錯誤,因此您將能夠使用.catch()
語句:
user.send().catch(() => console.log('Could not DM this user'));
您可以使用.catch()
獲取錯誤通知以及有關錯誤的信息
在這個例子中,我記錄了錯誤類型和描述
member.send(...).catch(error => {
console.error(`${error.name} :\n${error}`)
message.channel.send('There was an error when trying to DM this member')
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.