K
K
killeryStark2019-12-22 10:37:59
JavaScript
killeryStark, 2019-12-22 10:37:59

How to output server data to discord bot via embed?

I’ll clarify right away in node js and I’m weak in coding
Need help, possibly a financial reward.
In principle, I understand how to build a simple discord bot, but the task is as follows:
there is a library: https://www.npmjs.com/package/gamedig#usage-from-c...
You need to display the
name
map
Players & maxplayers
connect data on the command
How bind them to embed (and how can they be updated?) thanks

const Discord = module.require("discord.js");
const Gamedig = require('gamedig');
const fs = require("fs");
const qs = require("querystring");

module.exports.run = async (bot,message,args) => {
    message.delete().catch();

Gamedig.query({
    type: 'arkse',
    host: '85.190.155.194',
    port: '27015'
}).then((state) => {
    console.log(state);
}).catch((error) => {
    console.log("Server is offline");

});

const embed = new Discord.RichEmbed()
.setTitle(name);
.setColor((online ? 0x37963F : 0x933836))
    .addField(Map, true)
    .addField(Players/maxplayers, true)
    .addField(Connect:, true)
    .setTimestamp()


message.channel.send(embed);

};
module.exports.help = {
    name: "ark"
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Ptimirov, 2019-12-22
@killeryStark

.then(()=>{})
.catch(()=>{})

These are the structures of a promise - a system that returns the result of the execution of a particular asynchronous command. That is, in your code Gamedig.query({... is called before creating and sending embed, but in practice it may receive a response from the server with a delay, i.e. after sending the message.
So in your case you need (for example) to prepare Embed only After receiving a response from the library, then your option is:
// Создание запроса к библиотеке
Gamedig.query({
    type: 'arkse',
    host: '85.190.155.194',
    port: '27015'
})
// Код который вызовется при успешном запросе т.е. ответ, данные получены
.then((state) => {

const embed = new Discord.RichEmbed() 
.setTitle(state.name)
.addField(state.map, true)
.addField(state.players.length/state.maxplayers, true)
.addField(state.connect:, true) 
.setTimestamp();
message.channel.send(embed);

})
// Произошла ошибка 
.catch((e)=>{/*ну а тут обработчик ошибки*/})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question