Ya he hecho bots antes, pero en este caso, quiero usar una api de servidores de minecraft para que cambie su estado cada 5 segundos con informacion obtenida de la API, tambien, un simple comando de embed que envia información. Aquí esta el código:
const Discord = require("discord.js");
const client = new Discord.Client();
const datos = require("./datos.json");
const fetch = require('node-fetch');
let prefix = datos.prefix
client.on('ready', () => {
fetch(`https://api.mcsrvstat.us/2/mc.bouncegaming.es`).then(res => res.json()).then(json => {
let bgip = json.ip
let bgport = json.port
let bgplayersonline = json.players.online
let bgplayersmax = json.players.max
let bgmotd = json.motd.clean
let bgversion = json.version
})
const estados = [
" ",
"mc.bouncegaming.es",
"Hay "+bgplayersonline+"/"+bgplayersmax+" conectados.",
"Version: "+bgversion+"."
];
console.log("Bot encendido.")
setInterval(() => {
const index = Math.floor(Math.random() * (estados.length - 1) + 1);
client.user.setActivity(estados[index]);
}, 5000);
client.on("message", (message) => {
let member = message.member;
const args = message.content.slice(prefix.length).split(' ');
const command = args.shift().toLowerCase();
if(message.content.toLowerCase().startsWith(prefix+"statusmc")) {
const embedbg1 = new Discord.RichEmbed()
.setColor(0x1A85CC)
.setTitle('BounceGaming')
.setThumbnail("https://bouncegaming.es/files/logo512.png")
.setDescription('https://discord.gg/FzkuFJg')
.addField('Usuarios conectados:', bgplayersonline+"/"+bgplayersmax, true)
.addField('IP:', bgip+":"+bgport, true)
.addBlankField()
.addField('MOTD:', bgmotd, true)
.addField('Version:', bgversion, true)
message.channel.send(embedbg1)
}
})
});
client.login(datos.token);
Peeero consigo este error:
C:\botBounceGaming\discordapimc\index.js:20 "Hay "+bgplayersonline+"/"+bgplayersmax+" conectados.", ^
ReferenceError: bgplayersonline is not defined at Client. (C:\botBounceGaming\discordapimc\index.js:20:12) [90m at Client.emit (events.js:215:7)[39m at WebSocketConnection.triggerReady (C:\botBounceGaming\discordapimc\node_modules\[4mdiscord.js[24m\src\client\websocket\WebSocketConnection.js:125:17) at WebSocketConnection.checkIfReady (C:\botBounceGaming\discordapimc\node_modules\[4mdiscord.js[24m\src\client\websocket\WebSocketConnection.js:141:61) at GuildCreateHandler.handle (C:\botBounceGaming\discordapimc\node_modules\[4mdiscord.js[24m\src\client\websocket\packets\handlers\GuildCreate.js:13:31) at WebSocketPacketManager.handle (C:\botBounceGaming\discordapimc\node_modules\[4mdiscord.js[24m\src\client\websocket\packets\WebSocketPacketManager.js:105:65) at WebSocketConnection.onPacket (C:\botBounceGaming\discordapimc\node_modules\[4mdiscord.js[24m\src\client\websocket\WebSocketConnection.js:333:35) at WebSocketConnection.onMessage (C:\botBounceGaming\discordapimc\node_modules\[4mdiscord.js[24m\src\client\websocket\WebSocketConnection.js:296:17) at WebSocket.onMessage (C:\botBounceGaming\discordapimc\node_modules\[4mws[24m\lib\event-target.js:120:16) [90m at WebSocket.emit (events.js:210:5)[39m
Agradezco toda ayuda para conseguir guardar la informacion en una variable para usar la informacion fuera del .then(json => { //Blah Blah Blah})