拥有丰富的Minecraft玩家人数

时间:2019-02-12 14:37:12

标签: discord.js

我是一个初学者,我想知道如何在Rich Presence的Minecraft服务器上放置播放器数量。 我发现了下面的代码该如何做,但是我想将其改编为一个脚本,该脚本允许您每隔x秒修改Rich Presence。

预先感谢您, 真诚的

我的功能,用于显示丰富状态中的玩家人数: https://pastebin.com/7BpcLb9J

我当前更改的代码每秒:

bot.on('ready', async () => {
setInterval(function() {
    let status = statuses[Math.floor(Math.random()*statuses.length)];
    bot.user.setPresence({
        status: "dnd",
        game: {
            name: status,
            type: "WATCHING"
        }
    });
}, 2500)
let statuses = ['firsttext', 'secondtext'];

1 个答案:

答案 0 :(得分:0)

去那里

let currentStatus = false;
var url = 'http://mcapi.us/server/status?ip=' + botconfig.mcIP + '&port=' + botconfig.mcPort;
bot.on('ready', async () => {

    setInterval(() => {
        shuffleStatus(currentStatus);
    }, 2500)


});

const shuffleStatus = (index) => {
    if (index) {
        getServerStatus().then((status) => {
            bot.user.setActivity(status, {
                type: 'PLAYING'
            })
        });
    } else {
        bot.user.setStatus('online')
        bot.user.setActivity('status 2', {
            type: 'PLAYING'
        })
    }
    currentStatus = !currentStatus;
}

const getServerStatus = () => {
    var serverStatus = new Promise(function (resolve, reject) {
        request(url, function (err, response, body) {
            if (err) {
                reject(err);
            }
            body = JSON.parse(body);
            let status = 'Server offline';
            if (body.online) {
                if ((body.motd == "&cWe are under maintenance.") || (body.players.now >= body.players.max)) {
                    bot.user.setStatus('idle')
                        .catch(console.error);
                } else {
                    bot.user.setStatus('online')
                        .catch(console.error);
                }
                if (body.players.now) {
                    status = `${body.players.now} of ${body.players.max}`;
                } else {
                    status = `0 of ${body.players.max}`
                }
            }
            resolve(status);
        });
    })
    return serverStatus;
}