问题描述
我想用 Visual Studio Code 在 Node.js 中构建一个命令,它将从 FiveM 获取数据,并在消息中显示有多少玩家在线以及是否有队列!
我会发布一张图片,并说明我的意思:
const Gamedig = require('gamedig');
Gamedig.query({
type: 'fivem',host: 'fivem.example.com'
}).then((state) => {
console.log(state);
}).catch((error) => {
console.log("Server is offline");
});
请问有没有办法设置为命令并显示“图像”视图!
解决方法
根据 gamedig documentation,回调函数中的 state
参数没有那么多预定义的属性。您可以通过 players.length
属性直接检索在线玩家的数量,通过 state.players.length
方法中的回调函数内的 then()
访问它。但是还有一个 raw
属性似乎返回服务器给您的所有信息,因此您可以尝试解析它并检索任何其他相关信息。
使用 FiveM 包获取服务器信息。
你可以这样得到球员:
const FiveM = require("fivem") // Import the npm package.
const srv = new FiveM.Server('IP:PORT') // Set the IP with port.
srv.getPlayers().then(data => console.log(data)) // Get & log the data!
或整个服务器对象:
srv.getServer().then(data => console.log(data)) // Get & log the data!
享受。