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

Commit dfac793

Browse files
committed
refactor: switch to undici
undici's fetch is coming to node in v18: nodejs/node#41749 Not putting this in production because undici is using many experimental node features for now Ref: 6ce33eb
1 parent 817fafa commit dfac793

File tree

8 files changed

+43
-88
lines changed

8 files changed

+43
-88
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"dependencies": {
2020
"@crowdin/crowdin-api-client": "^1.18.2",
2121
"@messageformat/core": "^3.0.1",
22-
"axios": "^0.27.2",
2322
"canvas": "^2.9.1",
2423
"discord.js": "^14.0.0-dev.1655381041-4df491c",
2524
"language-flag-colors": "^2.0.4",

src/commands/Admin/eval.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable import/order */
22
/* eslint-disable @typescript-eslint/no-unused-vars */
33
const fs = require("node:fs"),
4-
axios = require("axios"),
54
flagColors = require("language-flag-colors"),
65
{ colors, listeningStatuses, watchingStatuses, playingStatuses, ids } = require("../../config.json"),
76
{ crowdin } = require("../../index"),

src/commands/Utility/hypixelstats.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import axios from "axios"
21
import { type GuildMember, EmbedBuilder, SelectMenuBuilder, ComponentType, ApplicationCommandOptionType, Colors } from "discord.js"
32

43
import { ids } from "../../config.json"
54
import { client } from "../../index"
65
import { db, type DbUser } from "../../lib/dbclient"
7-
import { fetchSettings, generateTip, getMCProfile, getUUID, gql, type GraphQLQuery, transformDiscordLocale, updateRoles } from "../../lib/util"
6+
import { postSettings, generateTip, getMCProfile, getUUID, gql, type GraphQLQuery, transformDiscordLocale, updateRoles } from "../../lib/util"
87

98
import type { Command, GetStringFunction } from "../../lib/imports"
109

@@ -48,19 +47,18 @@ const command: Command = {
4847
if (!uuid) throw "falseUser"
4948

5049
// Make a request to the slothpixel api (hypixel api but we dont need an api key)
51-
const graphqlQuery = await axios
52-
.get<GraphQLQuery>("https://api.slothpixel.me/api/graphql", {
53-
...fetchSettings,
54-
data: { query: query, variables: { uuid }, operationName: "HypixelStats" },
55-
})
56-
.then(res => res.data)
50+
const graphqlQuery = (await fetch("https://api.slothpixel.me/api/graphql", {
51+
...postSettings,
52+
body: JSON.stringify({ query: query, variables: { uuid }, operationName: "HypixelStats" }),
53+
})
54+
.then(res => res.json())
5755
.catch(e => {
58-
if (e.code === "ECONNABORTED") {
56+
if (e.code === "ECONNRESET") {
5957
// This means the request timed out
6058
console.error("Slothpixel is down, sending error.")
6159
throw "apiError"
6260
} else throw e
63-
}),
61+
})) as GraphQLQuery,
6462
playerJson = graphqlQuery.data.players.player,
6563
guildJson = graphqlQuery.data.guild
6664

src/commands/Utility/hypixelverify.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import axios from "axios"
21
import { ApplicationCommandOptionType, EmbedBuilder } from "discord.js"
32

43
import { colors, ids } from "../../config.json"
@@ -36,16 +35,15 @@ const command: Command = {
3635
if (!uuid) throw "noUser"
3736

3837
// Make a response to the slothpixel api (hypixel api but we dont need an api key)
39-
const json = await axios
40-
.get<GraphQLQuery["data"]["players"]["player"] & { error?: string }>(`https://api.slothpixel.me/api/players/${uuid}`, fetchSettings)
41-
.then(res => res.data)
38+
const json = (await fetch(`https://api.slothpixel.me/api/players/${uuid}`, fetchSettings)
39+
.then(res => res.json())
4240
.catch(e => {
43-
if (e.code === "ECONNABORTED") {
41+
if (e.code === "ECONNRESET") {
4442
// This means the request timed out
4543
console.error("slothpixel is down, sending error.")
4644
throw "apiError"
4745
} else throw e
48-
})
46+
})) as GraphQLQuery["data"]["players"]["player"] & { error?: string }
4947

5048
// Handle errors
5149
if (json.error === "Player does not exist" || json.error === "Invalid username or UUID!") throw "falseUser"

src/commands/Utility/languagestats.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const command: Command = {
4242
.getProjectProgress(ids.projects.hypixel)
4343
.then(res => res.data.find(language => language.data.languageId === lang.id)?.data ?? null)
4444
.catch(e => {
45-
if (e.code === "ECONNABORTED") {
45+
if (e.code === "ECONNRESET") {
4646
// This means the request timed out
4747
console.error("Crowdin API is down, sending error.")
4848
throw "apiError"

src/commands/Utility/minecraft.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import axios from "axios"
21
import { ComponentType, GuildMember, ApplicationCommandOptionType, EmbedBuilder } from "discord.js"
32

43
import { colors, ids } from "../../config.json"
@@ -186,17 +185,17 @@ const command: Command = {
186185
export default command
187186

188187
async function getPlayer(uuid: string) {
189-
const json = await axios
190-
.get<UserProfile & { error?: string }>(`https://sessionserver.mojang.com/session/minecraft/profile/${uuid}`, fetchSettings)
191-
.then(res => res.data)
188+
const json = (await fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${uuid}`, fetchSettings).then(res =>
189+
res.json(),
190+
)) as UserProfile & { error?: string }
192191
if (json.error) throw "falseUUID"
193192
return json
194193
}
195194

196195
async function getNameHistory(uuid: string) {
197-
const json = await axios
198-
.get<NameHistory[] | MCAPIError>(`https://api.mojang.com/user/profiles/${uuid}/names`, fetchSettings)
199-
.then(res => res.data || null)
196+
const json = (await fetch(`https://api.mojang.com/user/profiles/${uuid}/names`, fetchSettings)
197+
.then(res => res.json())
198+
.catch(() => null)) as NameHistory[] | MCAPIError
200199
if (!json || "error" in json) throw "falseUUID"
201200
return json.reverse()
202201
}

src/lib/util.ts

+20-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { readdirSync } from "node:fs"
33
import process from "node:process"
44
import { setInterval } from "node:timers"
55

6-
import axios from "axios"
76
import {
87
type ChatInputCommandInteraction,
98
type GuildMember,
@@ -33,7 +32,12 @@ import type { ResponseObject, TranslationStatusModel } from "@crowdin/crowdin-ap
3332

3433
// #region Variables
3534

36-
export const fetchSettings = { headers: { "User-Agent": "Hypixel Translators Bot" }, timeout: 30_000 }
35+
export const fetchSettings: RequestInit = { headers: { "User-Agent": "Hypixel Translators Bot" } }
36+
37+
export const postSettings: RequestInit = {
38+
headers: { "Content-Type": "application/json", ...fetchSettings.headers },
39+
method: "POST",
40+
}
3741

3842
// Browser-related variables, not exported
3943
let browser: puppeteer.Browser | null = null,
@@ -303,17 +307,15 @@ export async function getInviteLink() {
303307
return `https://discord.gg/${inviteCode}`
304308
}
305309

306-
export async function getMCProfile(uuid: string) {
307-
return await axios
308-
.get<MinecraftProfile>(`https://sessionserver.mojang.com/session/minecraft/profile/${uuid}`, fetchSettings)
309-
.then(json => json.data)
310+
export function getMCProfile(uuid: string) {
311+
return fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${uuid}`, fetchSettings)
312+
.then(res => res.json() as Promise<MinecraftProfile>)
310313
.catch(() => null)
311314
}
312315

313-
export async function getUUID(username: string): Promise<string | null> {
314-
return await axios
315-
.get(`https://api.mojang.com/users/profiles/minecraft/${username}`, fetchSettings)
316-
.then(data => data.data.id ?? null)
316+
export function getUUID(username: string) {
317+
return fetch(`https://api.mojang.com/users/profiles/minecraft/${username}`, fetchSettings)
318+
.then(async res => ((await res.json()) as UUIDResponse).id ?? null)
317319
.catch(() => null)
318320
}
319321

@@ -350,13 +352,14 @@ export function parseToNumberString(num: number, getString: GetStringFunction):
350352
return [format(num), num]
351353
}
352354

353-
export async function restart(interaction?: ChatInputCommandInteraction) {
354-
await axios.delete("https://api.heroku.com/apps/hypixel-translators/dynos", {
355+
export function restart(interaction?: ChatInputCommandInteraction) {
356+
return fetch("https://api.heroku.com/apps/hypixel-translators/dynos", {
355357
headers: {
356358
"User-Agent": `${interaction?.user.tag ?? client.user.tag}`,
357359
Authorization: `Bearer ${process.env.HEROKU_API}`,
358360
Accept: "application/vnd.heroku+json; version=3",
359361
},
362+
method: "DELETE",
360363
})
361364
}
362365

@@ -628,4 +631,9 @@ export interface Stats {
628631
errorMessage?: string
629632
}
630633

634+
interface UUIDResponse {
635+
name: string
636+
id: string
637+
}
638+
631639
// #endregion

yarn.lock

+4-50
Original file line numberDiff line numberDiff line change
@@ -379,26 +379,13 @@ array.prototype.flat@^1.2.5:
379379
es-abstract "^1.19.2"
380380
es-shim-unscopables "^1.0.0"
381381

382-
asynckit@^0.4.0:
383-
version "0.4.0"
384-
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
385-
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==
386-
387382
388383
version "0.21.3"
389384
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.3.tgz#f85d9b747f9b66d59ca463605cedf1844872b82e"
390385
integrity sha512-JtoZ3Ndke/+Iwt5n+BgSli/3idTvpt5OjKyoCmz4LX5+lPiY5l7C1colYezhlxThjNa/NhngCUWZSZFypIFuaA==
391386
dependencies:
392387
follow-redirects "^1.14.0"
393388

394-
axios@^0.27.2:
395-
version "0.27.2"
396-
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
397-
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
398-
dependencies:
399-
follow-redirects "^1.14.9"
400-
form-data "^4.0.0"
401-
402389
balanced-match@^1.0.0:
403390
version "1.0.2"
404391
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
@@ -510,13 +497,6 @@ color-support@^1.1.2:
510497
resolved "https://registry.yarnpkg.com/color-support/-/color-support-1.1.3.tgz#93834379a1cc9a0c61f82f52f0d04322251bd5a2"
511498
integrity sha512-qiBjkpbMLO/HL68y+lh4q0/O1MZFj2RX6X/KmMa3+gJD3z+WwI1ZzDHysvqHGS3mP6mznPckpXmw1nI9cJjyRg==
512499

513-
combined-stream@^1.0.8:
514-
version "1.0.8"
515-
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
516-
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
517-
dependencies:
518-
delayed-stream "~1.0.0"
519-
520500
521501
version "0.0.1"
522502
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
@@ -584,11 +564,6 @@ define-properties@^1.1.3, define-properties@^1.1.4:
584564
has-property-descriptors "^1.0.0"
585565
object-keys "^1.1.1"
586566

587-
delayed-stream@~1.0.0:
588-
version "1.0.0"
589-
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
590-
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==
591-
592567
delegates@^1.0.0:
593568
version "1.0.0"
594569
resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a"
@@ -994,19 +969,10 @@ flatted@^3.1.0:
994969
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.5.tgz#76c8584f4fc843db64702a6bd04ab7a8bd666da3"
995970
integrity sha512-WIWGi2L3DyTUvUrwRKgGi9TwxQMUEqPOPQBVi71R96jZXJdFskXEmf54BoZaS1kknGODoIGASGEzBUYdyMCBJg==
996971

997-
follow-redirects@^1.14.0, follow-redirects@^1.14.9:
998-
version "1.15.1"
999-
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.1.tgz#0ca6a452306c9b276e4d3127483e29575e207ad5"
1000-
integrity sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==
1001-
1002-
form-data@^4.0.0:
1003-
version "4.0.0"
1004-
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
1005-
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
1006-
dependencies:
1007-
asynckit "^0.4.0"
1008-
combined-stream "^1.0.8"
1009-
mime-types "^2.1.12"
972+
follow-redirects@^1.14.0:
973+
version "1.14.7"
974+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.7.tgz#2004c02eb9436eee9a21446a6477debf17e81685"
975+
integrity sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ==
1010976

1011977
fs-constants@^1.0.0:
1012978
version "1.0.0"
@@ -1441,18 +1407,6 @@ micromatch@^4.0.4:
14411407
braces "^3.0.2"
14421408
picomatch "^2.3.1"
14431409

1444-
1445-
version "1.52.0"
1446-
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
1447-
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==
1448-
1449-
mime-types@^2.1.12:
1450-
version "2.1.35"
1451-
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
1452-
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
1453-
dependencies:
1454-
mime-db "1.52.0"
1455-
14561410
mimic-response@^2.0.0:
14571411
version "2.1.0"
14581412
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-2.1.0.tgz#d13763d35f613d09ec37ebb30bac0469c0ee8f43"

0 commit comments

Comments
 (0)