node-gamedig/protocols/terraria.js

27 lines
719 B
JavaScript
Raw Normal View History

2019-01-10 13:03:07 +01:00
const Core = require('./core');
2014-10-29 08:02:03 +01:00
class Terraria extends Core {
2019-01-10 13:03:07 +01:00
async run(state) {
const body = await this.request({
2019-01-12 11:43:36 +01:00
uri: 'http://'+this.options.address+':'+this.options.port+'/v2/server/status',
2017-08-09 12:32:09 +02:00
qs: {
players: 'true',
token: this.options.token
}
2019-01-10 13:03:07 +01:00
});
2014-10-29 08:02:03 +01:00
2019-01-10 13:03:07 +01:00
const json = JSON.parse(body);
if(json.status !== 200) throw new Error('Invalid status');
2014-10-29 08:02:03 +01:00
2019-01-10 13:03:07 +01:00
for (const one of json.players) {
state.players.push({name:one.nickname,team:one.team});
}
2017-08-09 12:32:09 +02:00
2019-01-10 13:03:07 +01:00
state.name = json.name;
state.raw.port = json.port;
state.raw.numplayers = json.playercount;
2017-08-09 12:32:09 +02:00
}
}
module.exports = Terraria;