为什么我无法在命令处理程序中捕获 Discord.js 错误?

Why am I not able to catch a Discord.js error in a command handler?

所以我是 Javascript 的新手,正在尝试制作一个不和谐的机器人。这是说明我的问题的一小部分:

    module.exports = {
    name: "match",
    category: "LOL",
    description: "Give Summoner's Data to User",
    run: async (client, message, args) => {
        var username = `${args}`
        var regionID= "na1"

        pyke.summoner.getBySummonerName(username, regionID).then(data => {
            return pyke.spectator.getCurrentGameInfoBySummoner(`${data.id}`, "na1").then(result => {
                try {
                    console.log(result)
                } catch (err) {
                    console.error(`${args} isn't in game!`)
                }
            })
    })
    }
}

我希望看到是否有错误原因,它会向控制台发送代码。但是,我得到了 UnhandledPromiseRejectionWarning。我的问题是为什么我不能捕获错误并将代码发送到控制台?

所以这就是我尝试的命令

const property1 = result.participants.summonerName
            const BResult = property1
            let FResult =  JSON.stringify(BResult)
            message.channel.send(FResult)

当我尝试时,我收到一条错误消息,提示此人不在游戏中。我知道这是假的,因为他们在游戏中。

所以我更进一步尝试这样做。

const property1 = result.participants[summonerName]
            const BResult = property1
            let FResult =  JSON.stringify(BResult)
            message.channel.send(FResult)

我仍然得到与上一个相同的结果。我也尝试做 const property1 = result.summonerName,但效果不佳。

尝试将 pyke.spectator.getCurrentGameInfoBySummone 包裹在 try/catch 中。此示例使用 try/catchawait 关键字:

module.exports = {
  name: "match",
  category: "LOL",
  description: "Give Summoner's Data to User",
  run: async (client, message, args) => {
    const username = `${args}`;
    const regionID = "na1";

    return pyke.summoner.getBySummonerName(username, regionID).then((data) => {
        try {
            const result = await pyke.spectator.getCurrentGameInfoBySummoner(`${data.id}`, "na1");
            console.log(result);
        } catch (err) {
            console.error(`${args} isn't in game!`);
        }
    });
  },
};

否则你可以尝试使用 Promise catch 来解决错误:

module.exports = {
  name: "match",
  category: "LOL",
  description: "Give Summoner's Data to User",
  run: async (client, message, args) => {
    const username = `${args}`;
    const regionID = "na1";

    return pyke.summoner.getBySummonerName(username, regionID).then((data) => {
        return pyke.spectator.getCurrentGameInfoBySummoner(`${data.id}`, "na1")
            .then(result => {
                console.log(result);
            })
            .catch(err => {
                console.error(`${args} isn't in game!`)
            }); 
    });
  },
};

您可以使用 JSON.stringify to stringify an object and you can use a variety of different methods such as destructuring 提取您只想 return 结合 creating/returning 新对象的特定属性:

// extract specific properties from `result`
// This is use ES6 destructuring, but you can use dot notation instead or whatever you prefer
const { property1, property2, property 3 } = result;
// return the stringified object only with the properties you need
const payload = { property1, property2 ,property };
return JSON.stringify(payload)