-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (83 loc) · 4.01 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
/**
* Created by the Ikari development team.
* Discord Support: https://dsc.gg/ikaridevelopment
*/
const ms = require('ms');
const fetch = require('node-fetch');
const Discord = require('discord.js');
const client = new Discord.Client();
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://mcapi.us/server/status?ip=${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.now;
// Get the server status
const status = (body.online ? "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('message', async (message) => {
if(message.content === `${config.prefix}force-update`){
if (!message.member.hasPermission('MANAGE_MESSAGES')) {
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://mcapi.us/server/status?ip=${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();
if (body.favicon && typeof body.favicon === 'string') {
const attachment = new Discord.MessageAttachment(Buffer.from(body.favicon.substr('data:image/png;base64,'.length), 'base64'), "icon.png");
const embed = new Discord.MessageEmbed()
.setAuthor(config.ipAddress)
.attachFiles(attachment)
.setThumbnail("attachment://icon.png")
.addField("Version", body.server.name)
.addField("Connected", `${body.players.now} players`)
.addField("Maximum", `${body.players.max} players`)
.addField("Status", (body.online ? "Online" : "Offline"))
.setColor("#FF0000")
.setFooter("Ikari Development Team - Bot Source");
sentMessage.edit(`:chart_with_upwards_trend: Here are the stats for **${config.ipAddress}**:`, { embed });
} else {
const embed = new Discord.MessageEmbed()
.setAuthor(config.ipAddress)
.addField("Version", body.server.name)
.addField("Connected", `${body.players.now} players`)
.addField("Maximum", `${body.players.max} players`)
.addField("Status", (body.online ? "Online" : "Offline"))
.setColor("#FF0000")
.setFooter("Ikari Development Team - Bot Source");
sentMessage.edit(`:chart_with_upwards_trend: Here are the stats for **${config.ipAddress}**:`, { embed });
}
}
});
client.login(config.token);