-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathinventory.js
170 lines (134 loc) · 6.04 KB
/
inventory.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
import {fetch, isMaintenance, userRegion, WeaponTypeUuid} from "../misc/util.js";
import {authUser, deleteUserAuth, getUser} from "./auth.js";
import {authFailureMessage, basicEmbed, skinCollectionSingleEmbed, collectionOfWeaponEmbed} from "../discord/embed.js";
import config from "../misc/config.js";
import {s} from "../misc/languages.js";
import {riotClientHeaders} from "../misc/util.js";
export const getEntitlements = async (user, itemTypeId, itemType="item") => {
// https://valapidocs.techchrism.me/endpoint/owned-items
const req = await fetch(`https://pd.${userRegion(user)}.a.pvp.net/store/v1/entitlements/${user.puuid}/${itemTypeId}`, {
headers: {
"Authorization": "Bearer " + user.auth.rso,
"X-Riot-Entitlements-JWT": user.auth.ent,
...riotClientHeaders(),
}
});
console.assert(req.statusCode === 200, `Valorant ${itemType} entitlements code is ${req.statusCode}!`, req);
const json = JSON.parse(req.body);
if (json.httpStatus === 400 && json.errorCode === "BAD_CLAIMS") {
deleteUserAuth(user);
return { success: false };
} else if (isMaintenance(json))
return { success: false, maintenance: true };
return {
success: true,
entitlements: json
}
}
const skinCache = {};
export const getSkins = async (user) => {
// get all the owned skins of a user
if(user.puuid in skinCache) {
const cached = skinCache[user.puuid];
const expiresIn = cached.timestamp - Date.now() + config.loadoutCacheExpiration;
if(expiresIn <= 0) {
delete skinCache[user.puuid];
} else {
console.log(`Fetched skins collection from cache for user ${user.username}! It expires in ${Math.ceil(expiresIn / 1000)}s.`);
return {success: true, skins: cached.skins};
}
}
const authResult = await authUser(user.id);
if(!authResult.success) return authResult;
const data = await getEntitlements(user, "e7c63390-eda7-46e0-bb7a-a6abdacd2433", "skins");
if(!data.success) return data;
const skins = data.entitlements.Entitlements.map(ent => ent.ItemID);
skinCache[user.puuid] = {
skins: skins,
timestamp: Date.now()
}
console.log(`Fetched skins collection for ${user.username}`);
return {
success: true,
skins: skins
}
}
const loadoutCache = {};
export const getLoadout = async (user, account) => {
// get the currently equipped skins of a user
if(user.puuid in loadoutCache) {
const cached = loadoutCache[user.puuid];
const expiresIn = cached.timestamp - Date.now() + config.loadoutCacheExpiration;
if(expiresIn <= 0) {
delete loadoutCache[user.puuid];
} else {
console.log(`Fetched loadout from cache for user ${user.username}! It expires in ${Math.ceil(expiresIn / 1000)}s.`);
return {success: true, loadout: cached.loadout, favorites: cached.favorites};
}
}
const authResult = await authUser(user.id, account);
if(!authResult.success) return authResult;
user = getUser(user.id, account);
console.log(`Fetching loadout for ${user.username}...`);
const req = await fetch(`https://pd.${userRegion(user)}.a.pvp.net/personalization/v2/players/${user.puuid}/playerloadout`, {
headers: {
"Authorization": "Bearer " + user.auth.rso,
"X-Riot-Entitlements-JWT": user.auth.ent,
...riotClientHeaders(),
}
});
console.assert(req.statusCode === 200, `Valorant loadout fetch code is ${req.statusCode}!`, req);
const json = JSON.parse(req.body);
if (json.httpStatus === 400 && json.errorCode === "BAD_CLAIMS") {
deleteUserAuth(user);
return { success: false };
} else if (isMaintenance(json))
return { success: false, maintenance: true };
const req2 = await fetch(`https://pd.${userRegion(user)}.a.pvp.net/favorites/v1/players/${user.puuid}/favorites`, {
headers: {
"Authorization": "Bearer " + user.auth.rso,
"X-Riot-Entitlements-JWT": user.auth.ent,
...riotClientHeaders(),
}
});
console.assert(req.statusCode === 200, `Valorant favorites fetch code is ${req.statusCode}!`, req);
const json2 = JSON.parse(req2.body);
if (json2.httpStatus === 400 && json2.errorCode === "BAD_CLAIMS") {
deleteUserAuth(user);
return { success: false };
} else if (isMaintenance(json2))
return { success: false, maintenance: true };
loadoutCache[user.puuid] = {
loadout: json,
favorites: json2,
timestamp: Date.now()
}
console.log(`Fetched loadout for ${user.username}`);
return {
success: true,
loadout: json,
favorites: json2
}
}
export const renderCollection = async (interaction, targetId=interaction.user.id, weaponName=null) => {
const user = getUser(targetId);
if(!user) return await interaction.reply({embeds: [basicEmbed(s(interaction).error.NOT_REGISTERED)]});
if(weaponName) return await renderCollectionOfWeapon(interaction, targetId, weaponName);
const loadout = await getLoadout(user);
if (!loadout.success) return errorFetchingCollection(loadout, interaction, targetId);
return await skinCollectionSingleEmbed(interaction, targetId, user, loadout);
}
const renderCollectionOfWeapon = async (interaction, targetId, weaponName) => {
const user = getUser(targetId);
const skins = await getSkins(user);
if(!skins.success) return errorFetchingCollection(skins, interaction, targetId);
return await collectionOfWeaponEmbed(interaction, targetId, user, WeaponTypeUuid[weaponName], skins.skins)
}
const errorFetchingCollection = (result, interaction, targetId) => {
if(!result.success) {
let errorText;
if(targetId && targetId !== interaction.user.id) errorText = s(interaction).error.AUTH_ERROR_COLLECTION_OTHER.f({u: `<@${targetId}>`});
else errorText = s(interaction).error.AUTH_ERROR_COLLECTION;
return authFailureMessage(interaction, result, errorText);
}
}