-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (88 loc) · 4.75 KB
/
Copy pathindex.js
File metadata and controls
105 lines (88 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
███ ███ █████ ██████ ███████ ██████ ██ ██ ███████ █████ ██ ██ ██████ ███████
████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███
██ ████ ██ ███████ ██ ██ █████ ██████ ████ ███████ ███████ ████ ██████ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███
██ ██ ██ ██ ██████ ███████ ██████ ██ ███████ ██ ██ ██ ██ ██ ███████
Original Repo: https://github.com/ix1g/egg
License: MIT
*/
const axios = require('axios');
const { Client, GatewayIntentBits, EmbedBuilder, PermissionsBitField } = require('discord.js');
const dotenv = require('dotenv');
dotenv.config();
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
});
let statusMessageId = null;
async function getMinecraftServerStatus() {
try {
const response = await axios.get(`https://api.mcsrvstat.us/2/${process.env.SERVER_IP}`);
const { online, players, motd, version } = response.data;
let playerList = players.list ? players.list.join(', ') : 'No players online';
const nextUpdateTime = Math.floor((Date.now() + 5 * 60 * 1000) / 1000);
const statusEmbed = new EmbedBuilder()
.setTitle(`${process.env.SERVER_IP} Server Status`)
.setColor(online ? 0x00FF00 : 0xFF0000)
.addFields(
{ name: 'Server Online:', value: online ? 'Yes' : 'No', inline: true },
{ name: 'Players Online:', value: `${players.online}/${players.max}`, inline: true },
{ name: 'Player List:', value: playerList || 'None', inline: false },
{ name: 'Version:', value: version || 'Unknown', inline: true },
{ name: 'Next Update:', value: `<t:${nextUpdateTime}:R>`, inline: false }
)
.setFooter({ text: `MOTD: ${motd.clean.join(' ')}` })
.setTimestamp();
return statusEmbed;
} catch (error) {
console.error('Error fetching server status:', error.message || error);
const errorEmbed = new EmbedBuilder()
.setTitle('Server Status Error')
.setColor(0xFF0000)
.setDescription('Could not fetch server status. Please try again later.')
.setTimestamp();
return errorEmbed;
}
}
async function sendOrUpdateStatusMessage(channelId) {
try {
const statusEmbed = await getMinecraftServerStatus();
const channel = client.channels.cache.get(channelId);
if (!channel) {
console.error('Error: Channel not found');
return;
}
if (!statusMessageId) {
const statusMessage = await channel.send({ embeds: [statusEmbed] });
statusMessageId = statusMessage.id;
} else {
const statusMessage = await channel.messages.fetch(statusMessageId);
await statusMessage.edit({ embeds: [statusEmbed] });
}
} catch (error) {
console.error('Error sending or updating message:', error.message || error);
}
}
async function startUpdatingStatus(channelId) {
await sendOrUpdateStatusMessage(channelId);
setInterval(async () => {
await sendOrUpdateStatusMessage(channelId);
}, 300000);
}
client.on('messageCreate', async (message) => {
if (message.content === '!start') {
if (!message.member.permissions.has(PermissionsBitField.Flags.Administrator)) {
return message.reply('You need Administrator permissions to use this command. | ليست لديك الصلاحية لأستعمال هذا الأمر.');
}
const channel = client.channels.cache.get(process.env.CHANNEL_ID);
if (!process.env.CHANNEL_ID) {
return message.reply('The specified channel was not found.');
}
message.reply('Starting the Minecraft server status updates...');
await startUpdatingStatus(process.env.CHANNEL_ID);
}
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.login(process.env.TOKEN);