Skip to content
This repository was archived by the owner on Nov 22, 2023. It is now read-only.

Commit 6ce33eb

Browse files
committed
Remove node-fetch in favor of axios
node-fetch/node-fetch#1141 and node-fetch/node-fetch#810 should be proof these lads are high on acids
1 parent 18bd384 commit 6ce33eb

File tree

8 files changed

+147
-124
lines changed

8 files changed

+147
-124
lines changed

package-lock.json

Lines changed: 0 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"countryjs": "^1.8.0",
2121
"discord.js": "^13.2.0-dev.1630325279.581921f",
2222
"mongodb": "^4.1.1",
23-
"node-fetch": "^2.6.1",
2423
"puppeteer": "^10.2.0",
2524
"source-map-support": "^0.5.19",
2625
"typescript": "^4.4.2",
@@ -32,7 +31,6 @@
3231
},
3332
"devDependencies": {
3433
"@types/countryjs": "^1.8.1",
35-
"@types/node-fetch": "^2.5.12",
3634
"@types/uuid": "^8.3.1",
3735
"dotenv": "^10.0.0",
3836
"rimraf": "^3.0.2"

src/commands/Utility/hypixelstats.ts

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Discord from "discord.js"
2-
import fetch, { FetchError } from "node-fetch"
2+
import axios from "axios"
33
import { db, DbUser } from "../../lib/dbclient"
44
import { Command, client, GetStringFunction } from "../../index"
55
import { fetchSettings, getUUID, updateRoles } from "../../lib/util"
@@ -63,9 +63,9 @@ const command: Command = {
6363

6464
await interaction.deferReply()
6565
// make a request to the slothpixel api (hypixel api but we dont need an api key)
66-
const playerJson = await fetch(`https://api.slothpixel.me/api/players/${uuid}`, fetchSettings).then(res => res.json())
66+
const playerJson = await axios.get<PlayerJson>(`https://api.slothpixel.me/api/players/${uuid}`, fetchSettings).then(res => res.data)
6767
.catch(e => {
68-
if (e instanceof FetchError) {
68+
if (e.code === "ECONNABORTED") { //this means the request timed out
6969
console.error("Slothpixel is down, sending error.")
7070
throw "apiError"
7171
} else throw e
@@ -90,13 +90,17 @@ const command: Command = {
9090
color = parseColorCode(playerJson.rank_formatted)
9191
rank = playerJson.rank_formatted.replace(/&([0-9]|[a-z])/g, "")
9292
}
93-
const username = playerJson.username.replaceAll("_", "\\_") // change the nickname in a way that doesn't accidentally mess up the formatting in the embed
93+
94+
// change the nickname in a way that doesn't accidentally mess up the formatting in the embed
95+
const username = playerJson.username.replaceAll("_", "\\_")
9496

9597
//Update user's roles if they're verified
9698
const uuidDb = await db.collection<DbUser>("users").findOne({ uuid: playerJson.uuid })
9799
if (uuidDb) updateRoles(client.guilds.cache.get("549503328472530974")!.members.cache.get(uuidDb.id)!, playerJson)
98100

99-
const stats = async () => {
101+
const skinRender = `https://mc-heads.net/body/${playerJson.uuid}/left`
102+
103+
const stats = () => {
100104
//Define each value
101105
let online: string
102106
if (playerJson.online) online = getString("online")
@@ -106,15 +110,13 @@ const command: Command = {
106110
if (!playerJson.last_game) last_seen = getString("lastGameHidden")
107111
else last_seen = getString("lastSeen", { game: playerJson.last_game.replace(/([A-Z]+)/g, " $1").trim() })
108112

109-
let lastLoginSelector: string
110-
if (playerJson.online) lastLoginSelector = "last_login"
111-
else lastLoginSelector = "last_logout"
113+
const lastLoginLogout = playerJson.online ? playerJson.last_login : playerJson.last_logout
112114

113115
let locale: string = getString("region.dateLocale", "global")
114116
if (locale.startsWith("crwdns")) locale = getString("region.dateLocale", "global", "en")
115117

116118
let lastLogin: string
117-
if (playerJson[lastLoginSelector]) lastLogin = `<t:${Math.round(new Date(playerJson[lastLoginSelector]).getTime() / 1000)}:F>`
119+
if (lastLoginLogout) lastLogin = `<t:${Math.round(new Date(lastLoginLogout).getTime() / 1000)}:F>`
118120
else lastLogin = getString("lastLoginHidden")
119121

120122
let firstLogin: string
@@ -125,22 +127,22 @@ const command: Command = {
125127
.setColor(color)
126128
.setAuthor(getString("moduleName"))
127129
.setTitle(`${rank} ${username}`)
128-
.setThumbnail(`https://mc-heads.net/body/${playerJson.uuid}/left`)
130+
.setThumbnail(skinRender)
129131
.setDescription(`${getString("description", { username: username, link: `(https://api.slothpixel.me/api/players/${uuid})` })}\n${uuidDb ? `${getString("userVerified", { user: `<@!${uuidDb.id}>` })}\n` : ""}${getString("updateNotice")}\n${getString("otherStats")}`)
130132
.addFields(
131133
{ name: getString("networkLevel"), value: Math.abs(playerJson.level).toLocaleString(locale), inline: true },
132134
{ name: getString("ap"), value: playerJson.achievement_points.toLocaleString(locale), inline: true },
133135
{ name: getString("first_login"), value: firstLogin, inline: true },
136+
134137
{ name: getString("language"), value: getString(playerJson.language), inline: true },
135138
{ name: online, value: last_seen, inline: true },
136-
{ name: getString(lastLoginSelector), value: lastLogin, inline: true }
137-
139+
{ name: getString(playerJson.online ? "last_login" : "last_logout"), value: lastLogin, inline: true }
138140
)
139141
.setFooter(`${executedBy} | ${credits}`, interaction.user.displayAvatarURL({ format: "png", dynamic: true }))
140142
return statsEmbed
141143
}
142144

143-
const social = async () => {
145+
const social = () => {
144146
const socialMedia = playerJson.links
145147

146148
let twitter: string
@@ -172,7 +174,7 @@ const command: Command = {
172174
if (socialMedia.DISCORD) {
173175
if (!socialMedia.DISCORD.includes("discord.gg")) discord = socialMedia.DISCORD.replaceAll("_", "\\_")
174176
else {
175-
await interaction.client.fetchInvite(socialMedia.DISCORD)
177+
interaction.client.fetchInvite(socialMedia.DISCORD)
176178
.then(invite => {
177179
if (allowedGuildIDs.includes(invite.guild?.id!)) discord = `[${getString("link")}](${invite.url})`
178180
else {
@@ -196,7 +198,7 @@ const command: Command = {
196198
.setColor(color)
197199
.setAuthor(getString("moduleName"))
198200
.setTitle(`${rank} ${username}`)
199-
.setThumbnail(`https://mc-heads.net/body/${playerJson.uuid}/left`)
201+
.setThumbnail(skinRender)
200202
.setDescription(`${getString("socialMedia", { username: username, link: `(https://api.slothpixel.me/api/players/${uuid})` })}\n${uuidDb ? `${getString("userVerified", { user: `<@!${uuidDb.id}>` })}\n` : ""}${getString("updateNotice")}\n${getString("otherStats")}`)
201203
.addFields(
202204
{ name: "Twitter", value: twitter, inline: true },
@@ -242,8 +244,8 @@ const command: Command = {
242244
content: getString("pagination.notYours", { command: `/${this.name}` }, "global", userDb.lang),
243245
ephemeral: true
244246
})
245-
else if (option === "stats") embed = await stats()
246-
else if (option === "social") embed = await social()
247+
else if (option === "stats") embed = stats()
248+
else if (option === "social") embed = social()
247249
optionsSelect.options.forEach(o => o.default = option === o.value)
248250
await menuInteraction.update({ embeds: [embed], components: [{ type: "ACTION_ROW", components: [optionsSelect] }] })
249251
})
@@ -255,29 +257,54 @@ const command: Command = {
255257
}
256258
}
257259

258-
function parseColorCode(rank: string): Discord.HexColorString {
259-
const colorCode: string = rank.substring(1, 2)
260-
const colorsJson: {
261-
[key: string]: Discord.HexColorString
262-
} = {
263-
"0": "#000000",
264-
"1": "#0000AA",
265-
"2": "#00AA00",
266-
"3": "#00AAAA",
267-
"4": "#AA0000",
268-
"5": "#AA00AA",
269-
"6": "#FFAA00",
270-
"7": "#AAAAAA",
271-
"8": "#555555",
272-
"9": "#5555FF",
273-
a: "#55FF55",
274-
b: "#55FFFF",
275-
c: "#FF5555",
276-
d: "#FF55FF",
277-
e: "#FFFF55",
278-
f: "#FFFFFF"
279-
}
260+
function parseColorCode(color: string): Discord.HexColorString {
261+
const colorCode: string = color.substring(1, 2).toLowerCase(),
262+
colorsJson: {
263+
[key: string]: Discord.HexColorString
264+
} = {
265+
"0": "#000000",
266+
"1": "#0000AA",
267+
"2": "#00AA00",
268+
"3": "#00AAAA",
269+
"4": "#AA0000",
270+
"5": "#AA00AA",
271+
"6": "#FFAA00",
272+
"7": "#AAAAAA",
273+
"8": "#555555",
274+
"9": "#5555FF",
275+
a: "#55FF55",
276+
b: "#55FFFF",
277+
c: "#FF5555",
278+
d: "#FF55FF",
279+
e: "#FFFF55",
280+
f: "#FFFFFF"
281+
}
280282
return colorsJson[colorCode]
281283
}
282284

283285
export default command
286+
287+
export interface PlayerJson {
288+
error?: string
289+
uuid: string
290+
username: string
291+
online: boolean
292+
rank: string
293+
rank_formatted: string
294+
prefix: string | null
295+
level: number
296+
achievement_points: number
297+
first_login: number
298+
last_login: number | null
299+
last_logout: number | null
300+
last_game: string | null
301+
language: string
302+
links: {
303+
TWITTER: string | null
304+
YOUTUBE: string | null
305+
INSTAGRAM: string | null
306+
TWITCH: string | null
307+
DISCORD: string | null
308+
HYPIXEL: string | null
309+
}
310+
}

src/commands/Utility/hypixelverify.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import Discord from "discord.js"
22
import { successColor, errorColor } from "../../config.json"
3-
import fetch, { FetchError } from "node-fetch"
3+
import axios from "axios"
44
import { db, DbUser } from "../../lib/dbclient"
55
import { fetchSettings, getUUID, updateRoles } from "../../lib/util"
66
import type { Command, GetStringFunction } from "../../index"
7+
import { PlayerJson } from "./hypixelstats"
78

89
const command: Command = {
910
name: "hypixelverify",
@@ -31,9 +32,9 @@ const command: Command = {
3132

3233
await interaction.deferReply()
3334
// make a response to the slothpixel api (hypixel api but we dont need an api key)
34-
const json = await fetch(`https://api.slothpixel.me/api/players/${uuid}`, fetchSettings).then(res => res.json())
35+
const json = await axios.get<PlayerJson>(`https://api.slothpixel.me/api/players/${uuid}`, fetchSettings).then(res => res.data)
3536
.catch(e => {
36-
if (e instanceof FetchError) {
37+
if (e.code === "ECONNABORTED") { //this means the request timed out
3738
console.error("slothpixel is down, sending error.")
3839
throw "apiError"
3940
} else throw e

0 commit comments

Comments
 (0)