-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
105 lines (86 loc) · 3.68 KB
/
index.js
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
const ms = require('ms')
const fetch = require('node-fetch')
const Discord = require('discord.js')
const client = new Discord.Client({
intents: [
Discord.IntentsBitField.Flags.Guilds,
Discord.IntentsBitField.Flags.GuildMessages,
Discord.IntentsBitField.Flags.MessageContent
]
});
const config = require('./config.json')
/**
* This function is used to update statistics channel
*/
const updateChannel = async () => {
// Fetch statistics from mcapi.us
const res = await fetch(`https://api.mcsrvstat.us/3/${config.ipAddress}${config.port ? `&port=${config.port}` : ''}`)
if (!res) {
const statusChannelName = `【🛡】Status: Offline`
client.channels.cache.get(config.statusChannel).setName(statusChannelName)
return false
}
// Parse the mcapi.us response
const body = await res.json()
// Get the current player count, or set it to 0
const players = body.players.online
// Get the server status
const status = (body.debug.ping ? "Online" : "Offline")
// Generate channel names
const playersChannelName = `【👥】Players: ${players}`
const statusChannelName = `【🛡】Status: ${status}`
// Update channel names
client.channels.cache.get(config.playersChannel).setName(playersChannelName)
client.channels.cache.get(config.statusChannel).setName(statusChannelName)
return true
}
client.on('ready', () => {
console.log(`Ready. Logged as ${client.user.tag}.`)
setInterval(() => {
updateChannel()
}, ms(config.updateInterval))
})
client.on('messageCreate', async (message) => {
console.log(message.content)
if(message.content === `${config.prefix}force-update`){
if (!message.member.permissions.has(Discord.PermissionsBitField.Flags.ManageMessages)) {
return message.channel.send('Only server moderators can run this command!')
}
const sentMessage = await message.channel.send("Updating the channels, please wait...")
await updateChannel()
sentMessage.edit("Channels were updated successfully!")
}
if(message.content === `${config.prefix}stats`){
const sentMessage = await message.channel.send("Fetching statistics, please wait...")
// Fetch statistics from mcapi.us
const res = await fetch(`https://api.mcsrvstat.us/3/${config.ipAddress}${config.port ? `&port=${config.port}` : ''}`)
if (!res) return message.channel.send(`Looks like your server is not reachable... Please verify it's online and it isn't blocking access!`)
// Parse the mcapi.us response
const body = await res.json()
const attachment = new Discord.AttachmentBuilder(Buffer.from(body.icon.substr('data:image/png;base64,'.length), 'base64'), {
name: "icon.png",
description: "Server Icon"
});
const embed = new Discord.EmbedBuilder()
.setAuthor({
name: config.ipAddress
})
.setThumbnail("attachment://icon.png")
.addFields([
{ name: "Version", value: body.version },
{ name: "Connected", value: `${body.players.online} players` },
{ name: "Maximum", value: `${body.players.max} players` },
{ name: "Status", value: (body.debug.ping ? "Online" : "Offline") }
])
.setColor("#FF0000")
.setFooter({
text: "Open Source Minecraft Discord Bot"
})
sentMessage.edit({
embeds: [embed],
content: `:chart_with_upwards_trend: Here are the stats for **${config.ipAddress}**:`,
files: [attachment]
})
}
})
client.login(config.token)