Ive never worked with objects and dont know how they work. Tried searching it up but doesnt come up with what im looking for. I want to get peoples username in sample and put them all into an embed to display whos online
if (command === "status") {
util.status('server', 25565, options)
.then(
(result) => {
console.log(result)
dumbarry = result.players.sample.names
console.log(dumbarry)
if (!dumbarry) return message.channel.send("No one is online")
let embed = new MessageEmbed()
.setTitle("Mincraft Players Online")
for (let i = 0; i < dumbarry.length; i ) {
embed.addField(dumbarry[i], 'is online')
}
message.channel.send(embed)
}
)
.catch((error) => console.error(error));
}
returns
{
version: { name: 'Paper 1.18.2', protocol: 758 },
players: { online: 1, max: 100, sample: [ [Object] ] },
motd: {
raw: '§fWelcome to hell',
clean: 'Welcome to hell',
html: '<span><span style="color: #FFFFFF;">Welcome to hell</span></span>'
},
favicon: null,
srvRecord: { host: 'server', port: 25600 },
roundTripLatency: 139
}
In the object
[ { id: '8c075091-7837-41ca-9d9c-bb618843b15f', name: 'newdabz' }
CodePudding user response:
Your code doesn't look to bad, just a few issues i see.
Since the player list is an array you need to correct this line first,
because you can't destructure the array like that:
- dumbarry = result.players.sample.names
let dumbarry = result.players.sample
From there on you now have an array of the playerlist and you can use the for loop
To add fields or add them to a string.
// Sample 1
for (let i = 0; i < dumbarry.length; i ) {
embed.addField(dumbarry[i].name, 'is online')
}
// Sample 2
for (let player of dumbarry) {
embed.addField(player.name, 'is online.');
}
// With string
let string = '';
for (let player of dumbarry) {
string = `**${player.name}** is online!\n`
}
embed.setDescription(string);
I hope that helped!
If someone has a better / more efficient way, let me know :)