1
1
import Discord from "discord.js"
2
- import fetch , { FetchError } from "node-fetch "
2
+ import axios from "axios "
3
3
import { db , DbUser } from "../../lib/dbclient"
4
4
import { Command , client , GetStringFunction } from "../../index"
5
5
import { fetchSettings , getUUID , updateRoles } from "../../lib/util"
@@ -63,9 +63,9 @@ const command: Command = {
63
63
64
64
await interaction . deferReply ( )
65
65
// 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 )
67
67
. catch ( e => {
68
- if ( e instanceof FetchError ) {
68
+ if ( e . code === "ECONNABORTED" ) { //this means the request timed out
69
69
console . error ( "Slothpixel is down, sending error." )
70
70
throw "apiError"
71
71
} else throw e
@@ -90,13 +90,17 @@ const command: Command = {
90
90
color = parseColorCode ( playerJson . rank_formatted )
91
91
rank = playerJson . rank_formatted . replace ( / & ( [ 0 - 9 ] | [ a - z ] ) / g, "" )
92
92
}
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 ( "_" , "\\_" )
94
96
95
97
//Update user's roles if they're verified
96
98
const uuidDb = await db . collection < DbUser > ( "users" ) . findOne ( { uuid : playerJson . uuid } )
97
99
if ( uuidDb ) updateRoles ( client . guilds . cache . get ( "549503328472530974" ) ! . members . cache . get ( uuidDb . id ) ! , playerJson )
98
100
99
- const stats = async ( ) => {
101
+ const skinRender = `https://mc-heads.net/body/${ playerJson . uuid } /left`
102
+
103
+ const stats = ( ) => {
100
104
//Define each value
101
105
let online : string
102
106
if ( playerJson . online ) online = getString ( "online" )
@@ -106,15 +110,13 @@ const command: Command = {
106
110
if ( ! playerJson . last_game ) last_seen = getString ( "lastGameHidden" )
107
111
else last_seen = getString ( "lastSeen" , { game : playerJson . last_game . replace ( / ( [ A - Z ] + ) / g, " $1" ) . trim ( ) } )
108
112
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
112
114
113
115
let locale : string = getString ( "region.dateLocale" , "global" )
114
116
if ( locale . startsWith ( "crwdns" ) ) locale = getString ( "region.dateLocale" , "global" , "en" )
115
117
116
118
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>`
118
120
else lastLogin = getString ( "lastLoginHidden" )
119
121
120
122
let firstLogin : string
@@ -125,22 +127,22 @@ const command: Command = {
125
127
. setColor ( color )
126
128
. setAuthor ( getString ( "moduleName" ) )
127
129
. setTitle ( `${ rank } ${ username } ` )
128
- . setThumbnail ( `https://mc-heads.net/body/ ${ playerJson . uuid } /left` )
130
+ . setThumbnail ( skinRender )
129
131
. 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" ) } ` )
130
132
. addFields (
131
133
{ name : getString ( "networkLevel" ) , value : Math . abs ( playerJson . level ) . toLocaleString ( locale ) , inline : true } ,
132
134
{ name : getString ( "ap" ) , value : playerJson . achievement_points . toLocaleString ( locale ) , inline : true } ,
133
135
{ name : getString ( "first_login" ) , value : firstLogin , inline : true } ,
136
+
134
137
{ name : getString ( "language" ) , value : getString ( playerJson . language ) , inline : true } ,
135
138
{ 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 }
138
140
)
139
141
. setFooter ( `${ executedBy } | ${ credits } ` , interaction . user . displayAvatarURL ( { format : "png" , dynamic : true } ) )
140
142
return statsEmbed
141
143
}
142
144
143
- const social = async ( ) => {
145
+ const social = ( ) => {
144
146
const socialMedia = playerJson . links
145
147
146
148
let twitter : string
@@ -172,7 +174,7 @@ const command: Command = {
172
174
if ( socialMedia . DISCORD ) {
173
175
if ( ! socialMedia . DISCORD . includes ( "discord.gg" ) ) discord = socialMedia . DISCORD . replaceAll ( "_" , "\\_" )
174
176
else {
175
- await interaction . client . fetchInvite ( socialMedia . DISCORD )
177
+ interaction . client . fetchInvite ( socialMedia . DISCORD )
176
178
. then ( invite => {
177
179
if ( allowedGuildIDs . includes ( invite . guild ?. id ! ) ) discord = `[${ getString ( "link" ) } ](${ invite . url } )`
178
180
else {
@@ -196,7 +198,7 @@ const command: Command = {
196
198
. setColor ( color )
197
199
. setAuthor ( getString ( "moduleName" ) )
198
200
. setTitle ( `${ rank } ${ username } ` )
199
- . setThumbnail ( `https://mc-heads.net/body/ ${ playerJson . uuid } /left` )
201
+ . setThumbnail ( skinRender )
200
202
. 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" ) } ` )
201
203
. addFields (
202
204
{ name : "Twitter" , value : twitter , inline : true } ,
@@ -242,8 +244,8 @@ const command: Command = {
242
244
content : getString ( "pagination.notYours" , { command : `/${ this . name } ` } , "global" , userDb . lang ) ,
243
245
ephemeral : true
244
246
} )
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 ( )
247
249
optionsSelect . options . forEach ( o => o . default = option === o . value )
248
250
await menuInteraction . update ( { embeds : [ embed ] , components : [ { type : "ACTION_ROW" , components : [ optionsSelect ] } ] } )
249
251
} )
@@ -255,29 +257,54 @@ const command: Command = {
255
257
}
256
258
}
257
259
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
+ }
280
282
return colorsJson [ colorCode ]
281
283
}
282
284
283
285
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
+ }
0 commit comments