-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
217 lines (216 loc) · 10.7 KB
/
functions.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require('dotenv').config()
const fetch = require('node-fetch')
const mongo = require('mongodb')
const MongoClient = new mongo.MongoClient(process.env.MONGO_URL)
const config = require('./config.json')
const pkg = require('./package.json')
const Discord = require('discord.js')
const { colours } = require('./consoleFormatting.js')
const { compare } = require('compare-versions')
module.exports = {
async getTimestampFromID(discordId) {
return (discordId / 4194304 + 1420070400000)
},
async checkForUpdates(client) {
let baseUrl = pkg.repository.urlRaw
let data = await fetch(baseUrl + "package.json")
let packageData = await data.json()
let channel = client.channels.cache.get(config.channels.logChannelId)
if (compare(pkg.version.toString(), packageData.version.toString(), ">")) {
let embed = new Discord.MessageEmbed()
.setColor(config.colours.warning)
.setTimestamp()
.addField(`${config.emoji.warning} Warning!`, `You are running either an unreleased or a misconfigured version of GuildBot. \nOnly proceed if you are sure you know what you're doing. \nIf not, contact ${pkg.author} about this or post an issue on the github repository: ${pkg.repository.url}.`)
channel.send({
embeds: [embed]
})
console.warn(`${colours.reset}${colours.fg.yellow}You are running either an unreleased or a misconfigured version of GuildBot. Only proceed if you are sure you know what you're doing. If not, contact ${pkg.author} about this or post an issue on the github repository: ${pkg.repository.url}.${colours.reset}`)
} else if (compare(pkg.version.toString(), packageData.version.toString(), "<")) {
let githubApiDataRaw = await fetch(pkg.repository.urlApi + "/releases")
let githubApiData = await githubApiDataRaw.json()
let versionsBehind = githubApiData.findIndex(release => compare(release.tag_name.toString(), pkg.version.toString(), "=") === true)
let embed = new Discord.MessageEmbed()
.setColor(config.colours.warning)
.setTimestamp()
.addField(`${config.emoji.warning} Warning!`, `A new version of GuildBot is available!\n**${pkg.version} => ${packageData.version}**\nYou are **${versionsBehind}** updates behind the latest update.\nDownload it now at ${githubApiData[0].html_url}`)
channel.send({
embeds: [embed]
})
console.info(`${colours.reset}${colours.fg.yellow}A new version of GuildBot is available!\n${colours.bright}${pkg.version} => ${packageData.version}\n${colours.reset}${colours.fg.yellow}You are ${colours.bright}${versionsBehind}${colours.reset}${colours.fg.yellow} updates behind the latest update.\n${colours.reset}${colours.fg.yellow}Download it now at ${githubApiData[0].html_url}${colours.reset}`)
} else if (compare(pkg.version.toString(), packageData.version.toString(), "=")) {
console.log(`${colours.fg.white}No updates available. You are running the latest version of GuildBot: ${colours.bright}${pkg.version}${colours.reset}`)
}
},
async leaderboardDataUpdate(client) {
let leaderboardData = {};
let hGuild = {}
let data = await fetch(`https://api.hypixel.net/guild?key=${process.env.APIKEY}&id=${config.hypixelGuildId}`)
try {
hGuild = await data.json()
} catch (err) {
console.error(err)
}
if (hGuild.success) {
let count = hGuild.guild.members.length
let channel = await client.channels.fetch(config.channels.memberCount.guild)
channel.setName(`📊Guild Members: ${count}📊`)
for (const member of hGuild.guild.members) {
let nameData = await fetch(`https://minecraft-api.com/api/pseudo/${member.uuid}/json`)
let mun = undefined;
try {mun = await nameData.json()} catch(err){console.error}
if (mun) {
dates = Object.keys(member.expHistory)
let avgExp = 0;
let totalExp = 0;
let c = 0;
dates.forEach((date) => {
avgExp += member.expHistory[date]
c += 1;
})
totalExp = avgExp;
avgExp /= c;
let rank = {"name": undefined, "default": false, "tag": undefined, "created": undefined, "priority": undefined};
if (member.rank == "GUILDMASTER" || member.rank == "Guild Master") {
rank = {
"name": "Guild Master",
"default": false,
"tag": null,
"created": hGuild.guild.created,
"priority": 101
}
} else {
rank = hGuild.guild.ranks.find(rank => rank.name.toLowerCase() == member.rank.toLowerCase())
}
leaderboardData[mun.pseudo.toString()] = {
avg: avgExp,
total: totalExp,
rankName: rank.name,
rankDefault: rank.default,
rankTag: rank.tag,
rankCreated: rank.created,
rankPriority: rank.priority
}
}
}
setTimeout(async () => {
await MongoClient.connect()
const db = await MongoClient.db()
await db.collection('hypixel-api-data').findOne({ sid: "guild-leaderboard-data" }, async function(err, res) {
if (err) throw err;
if (res == null) {
await db.collection('hypixel-api-data').insertOne({sid: "guild-leaderboard-data", data: leaderboardData, timestamp: Date.now()}, function(err, res) {
if (err) throw err;
MongoClient.close()
})
} else {
await db.collection('hypixel-api-data').updateOne({ sid: "guild-leaderboard-data" }, { $set: { data: leaderboardData, timestamp: Date.now() } })
MongoClient.close()
}
})
}, 5000)
} else {
console.error(hGuild)
}
},
mineflayerConfig() {
let obj = {
host: "mc.hypixel.net",
version: "1.8",
username: process.env.MC_ACC_USERNAME.toString(),
password: process.env.MC_ACC_PASSWORD.toString(),
auth: process.env.MC_ACC_AUTHSERVER.toString(),
hideErrors: true
}
return obj;
},
async fetchJSON(url) {
let rawData = undefined;
try {
rawData = await fetch(url);
} catch(err){ console.error(err); }
if (rawData === undefined) {
return undefined;
} else {
let parsedData = undefined;
try {
parsedData = rawData.json();
} catch (err) { console.error(err); }
if (parsedData === undefined) {
return undefined;
} else {
return parsedData;
}
}
},
hypixelUtil: {
networkLevelFromExp(exp) {
return (Math.sqrt((2 * parseInt(exp)) + 30625) / 50) - 2.5
}
},
statistics: {
async increaseButtonCount() {
await MongoClient.connect()
const db = await MongoClient.db()
await db.collection('statistics').findOne({ sid: "countButtons" }, async function(err, res) {
if (err) throw err;
if (res == null) {
await db.collection('statistics').insertOne({sid: "countButtons", value: 1}, function(err, res) {
if (err) throw err;
MongoClient.close()
})
} else {
await db.collection('statistics').updateOne({ sid: "countButtons" }, { $set: { value: res.value+1 } })
MongoClient.close()
}
})
},
async increaseCommandCount() {
await MongoClient.connect()
const db = await MongoClient.db()
await db.collection('statistics').findOne({ sid: "countCommands" }, async function(err, res) {
if (err) throw err;
if (res == null) {
await db.collection('statistics').insertOne({sid: "countCommands", value: 1}, function(err, res) {
if (err) throw err;
MongoClient.close()
})
} else {
await db.collection('statistics').updateOne({ sid: "countCommands" }, { $set: { value: res.value+1 } })
MongoClient.close()
}
})
},
async increaseSelectMenuCount() {
await MongoClient.connect()
const db = await MongoClient.db()
await db.collection('statistics').findOne({ sid: "countSelectMenu" }, async function(err, res) {
if (err) throw err;
if (res == null) {
await db.collection('statistics').insertOne({sid: "countSelectMenu", value: 1}, function(err, res) {
if (err) throw err;
MongoClient.close()
})
} else {
await db.collection('statistics').updateOne({ sid: "countSelectMenu" }, { $set: { value: res.value+1 } })
MongoClient.close()
}
})
},
async increaseGuildApplicationCount() {
await MongoClient.connect()
const db = await MongoClient.db()
await db.collection('statistics').findOne({ sid: "countGuildApplications" }, async function(err, res) {
if (err) throw err;
if (res == null) {
await db.collection('statistics').insertOne({sid: "countGuildApplications", value: 1}, function(err, res) {
if (err) throw err;
MongoClient.close()
})
} else {
await db.collection('statistics').updateOne({ sid: "countGuildApplications" }, { $set: { value: res.value+1 } })
MongoClient.close()
}
})
}
}
}