简体   繁体   English

Discord.js 使用 try catch 语句处理 api 错误

[英]Discord.js use try catch statement to handle api error

Hello I am currently attempting to catch a discord api error with the try catch statement您好,我目前正在尝试使用 try catch 语句捕获 discord api 错误

However for some reason this does not work and my app still stops executing and gives me the following message但是由于某种原因,这不起作用,我的应用程序仍然停止执行并给我以下消息

 There was an uncaught error DiscordAPIError: Cannot send messages to this user // Example

How do I catch api errors with a try and catch block and I cannot use a.catch expression after calling the function如何使用 try 和 catch 块捕获 api 错误并且在调用 function 后无法使用 a.catch 表达式

Here is my code这是我的代码

try {
    interaction.user.send("Remember")
}
catch(DiscordAPIError) {
    console.log("Oh no")
}

If you want to use a synchronous try/catch block, you need to put it in an async function and await the promise from interaction.user.send :如果你想使用同步 try/catch 块,你需要把它放在一个异步的 function 中并等待来自interaction.user.send的promise:

await interaction.user.send("Remember")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM