fixed the issue that the bot would crash when running the command while server is offline

This commit is contained in:
valentijn
2024-01-04 22:43:10 +01:00
parent 5152eb3834
commit 8e636f75a1

View File

@@ -9,28 +9,31 @@ module.exports = {
async execute(interaction) { async execute(interaction) {
const SERVER_IP = `${server_ip}`; const SERVER_IP = `${server_ip}`;
try {
axios.get(`https://api.mcsrvstat.us/3/${SERVER_IP}`).then((resp) => {
const data = resp.data;
axios.get(`https://api.mcsrvstat.us/3/${SERVER_IP}`).then((resp) => { const embedData = {
const data = resp.data; title: SERVER_IP,
description: data.online ? 'Server is online' : 'Server is offline',
color: data.online ? 0x00FF00 : 0xFF0000,
fields: [
{ name: 'Version', value: `${data.version} (Protocol ${data.protocol.version})`, inline: false },
],
footer: { text: `${data.players.online}/${data.players.max} players` },
};
const embedData = { if (data.players && data.players.list && data.players.list.length > 0) {
title: SERVER_IP, embedData.fields.push({ name: 'Player List', value: data.players.list.map(player => player.name).join('\n'), inline: false });
description: data.online ? 'Server is online' : 'Server is offline', } else {
color: data.online ? 0x00FF00 : 0xFF0000, embedData.fields.push({ name: 'Player List', value: 'No players online :(', inline: false });
fields: [ }
{ name: 'Version', value: `${data.version} (Protocol ${data.protocol.version})`, inline: false },
],
footer: { text: `${data.players.online}/${data.players.max} players` },
};
if (data.players && data.players.list && data.players.list.length > 0) { const embed = new EmbedBuilder(embedData);
embedData.fields.push({ name: 'Player List', value: data.players.list.map(player => player.name).join('\n'), inline: false }); interaction.reply({ content: 'Server status:', embeds: [embed] });
} else { });
embedData.fields.push({ name: 'Player List', value: 'No players online :(', inline: false }); } catch (e){
} await interaction.reply("An Error Occurred, and i am too lazy to add a proper embed for it. the server is probably offline or something")
}
const embed = new EmbedBuilder(embedData);
interaction.reply({ content: 'Server status:', embeds: [embed] });
});
}, },
}; };