【问题标题】:Discord.js - setStatus not being called inside .thenDiscord.js - setStatus 内部没有被调用。then
【发布时间】:2020-08-10 05:08:26
【问题描述】:

我目前正在使用 Discord 机器人,我正在查询服务器的状态并尝试将机器人的状态设置为服务器的状态(在线=在线、完全=空闲、离线=请勿打扰)。

我已经测试了很多东西。 setStatus 方法 正在 被调用,我已经对其进行了测试,但机器人的状态没有更新。此外,下面的setActivity 方法每次都有效。 res.statusres.playerCount 也显示有效值。此外,在查询服务器状态之前,将状态设置为函数顶部的 dnd 之类的东西。是.then的问题吗?

如果您需要更多信息,请告诉我。感谢您的帮助!

function updatePresence() {
    utils.getServerStatus(config.serverIp, config.serverPort)
        .then(res => {
            if(res.status == 'online') {
                client.user.setStatus('online');
            } else if(res.status == 'full') {
                client.user.setStatus('idle');
            } else {
                client.user.setStatus('dnd');
            }

            client.user.setActivity(res.status == 'offline' ? 'server OFFLINE' : `${res.playerCount} players online`, { type: 'WATCHING' });
        });
}

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    您可以尝试使用.catch(console.error) 来查看它的输出。试试这个,因为你只需要调用一次 catch:

    function updatePresence() {
        utils.getServerStatus(config.serverIp, config.serverPort)
            .then(res => {
                const data = { online: "online", full: "idle" };
                return client.user.setPresence({
                    activity: {
                        name: res.status == 'offline' ? 'server OFFLINE' : `${res.playerCount} players online`,
                        type: "WATCHING"
                    },
                    status: data[res.status] || "dnd"
                })
            }).catch(console.error);;
    }
    

    【讨论】:

    • 感谢您的回复!虽然似乎没有任何错误,并且状态仍然没有刷新。编辑:由于某种原因,它似乎在进一步测试后工作 - 也许 setPresence 比 setStatus 更有效的解决方案。
    • Np,我刚刚意识到它没有记录任何错误的原因是因为 catch 在 then 函数中,如果有错误它将转到父级。 utils.getServerStatus(...).then().catch() 将是正确的代码,现在可能无关紧要,但很高兴知道将来调试
    【解决方案2】:

    这是 JavaScript。

    不要使用双等式==,而是使用三等式===,因此您的代码应如下所示:

    
    function updatePresence() {
        utils.getServerStatus(config.serverIp, config.serverPort)
            .then(res => {
                if(res.status === 'online') {
                    client.user.setStatus('online');
                } else if(res.status === 'full') {
                    client.user.setStatus('idle');
                } else {
                    client.user.setStatus('dnd');
                }
    
                client.user.setActivity(res.status === 'offline' ? 'server OFFLINE' : `${res.playerCount} players online`, { type: 'WATCHING' });
            });
    }
    

    【讨论】:

    • 感谢您的回复!不幸的是,我测试了您的解决方案,但它似乎不起作用。然而,这个问题的另一个答案是能够通过不同的方法来解决它。
    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2020-09-23
    相关资源
    最近更新 更多