fixed the issue that the bot would crash when running the command while server is offline
This commit is contained in:
@@ -9,28 +9,31 @@ module.exports = {
|
||||
|
||||
async execute(interaction) {
|
||||
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 data = resp.data;
|
||||
const embedData = {
|
||||
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 = {
|
||||
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` },
|
||||
};
|
||||
if (data.players && data.players.list && data.players.list.length > 0) {
|
||||
embedData.fields.push({ name: 'Player List', value: data.players.list.map(player => player.name).join('\n'), inline: false });
|
||||
} else {
|
||||
embedData.fields.push({ name: 'Player List', value: 'No players online :(', inline: false });
|
||||
}
|
||||
|
||||
if (data.players && data.players.list && data.players.list.length > 0) {
|
||||
embedData.fields.push({ name: 'Player List', value: data.players.list.map(player => player.name).join('\n'), inline: false });
|
||||
} else {
|
||||
embedData.fields.push({ name: 'Player List', value: 'No players online :(', inline: false });
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder(embedData);
|
||||
interaction.reply({ content: 'Server status:', embeds: [embed] });
|
||||
});
|
||||
const embed = new EmbedBuilder(embedData);
|
||||
interaction.reply({ content: 'Server status:', embeds: [embed] });
|
||||
});
|
||||
} 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")
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user