-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
191 lines (175 loc) · 4.37 KB
/
index.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
// Importing Required Modules
const rp = require("request-promise");
const Discord = require("discord.js");
// Local Imports
const config = require("./config");
const log = require("./classes/log");
// Initializing Discord Client
const client = new Discord.Client();
// Ready Event
client.on("ready", () => {
log.green(`Logged in as ${client.user.tag}!`);
client.user
.setPresence({
game: {
name: `${config.Discord.prefix}adistock PID | @exhwn`
},
status: "avaliable"
})
.then(log.green("Discord Presence Set!"));
});
// Message Event
client.on("message", msg => {
if (
msg.content.toLowerCase().startsWith(`${config.Discord.prefix}adistock`)
) {
let args = msg.content.split(" ");
let pid = args[1];
if (pid && pid.length === 6) {
log.green("Successfully Recieved PID Arugment");
getStock(msg, pid);
} else {
log.red("Failed to Recieve PID Arugment");
sendFail(msg);
}
}
});
// Get Stock Method
const getStock = (msg, pid) => {
var infoOptions = {
uri: `https://www.adidas.com/api/products/${pid}`,
forever: true,
json: true
};
var stockOptions = {
uri: `https://www.adidas.com/api/products/${pid}/availability`,
forever: true,
json: true
};
// Requesting Info Data
rp(infoOptions)
.then(json => {
log.green("Successful Info API Call!");
// Initializing Variables
let resultMsg = "";
let totalStock = 0;
// Saving Static Data
let id = json["id"];
let name = json["name"];
let price =
json["product_link_list"][2]["pricing_information"]["standard_price"];
let link = json["meta_data"]["canonical"].slice(2);
let img = json["view_list"][0]["image_url"];
let waitingRoom = json["attribute_list"]["isWaitingRoomProduct"];
// Requesting Stock Data
rp(stockOptions).then(json => {
log.green("Successful Stock API Call!");
// Looping JSON Stock Data
json.variation_list.forEach(elem => {
// Parsing Desired JSON Stock Data
let _availability = elem["availability"];
let _size = elem["size"];
// Building Total Stock
totalStock += _availability;
// Building Data Message
resultMsg += `Size: ${_size} | Stock: ${_availability} \n`;
});
//Send Success to Discord
sendSuccess(
msg,
resultMsg,
totalStock,
id,
price,
name,
link,
img,
waitingRoom
);
});
})
.catch(err => {
log.red("API Call Failed");
console.log(err);
sendFail(msg);
});
};
// Send Data to Discord if Successful
const sendSuccess = (
msg,
resultMsg,
totalStock,
id,
price,
name,
link,
img,
waitingRoom
) => {
msg.channel.send({
embed: {
color: 0x45c577,
author: {
name: "Adidas Stock Checker",
icon_url: "https://i.gyazo.com/a031d16c2a90b5fc6cf3c6563b9ecffe.png"
},
thumbnail: {
url: img
},
title: name,
url: `https://${link}`,
fields: [
{
name: "PID",
value: id,
inline: true
},
{
name: "Price",
value: `$${price}`,
inline: true
},
{
name: "Waiting Room?",
value: waitingRoom,
inline: true
},
{
name: "Total Stock",
value: totalStock,
inline: true
},
{
name: "Availability",
value: resultMsg,
inline: true
}
],
timestamp: new Date(),
footer: {
icon_url: "https://i.gyazo.com/a031d16c2a90b5fc6cf3c6563b9ecffe.png",
text: "Adidas Stock Checker | @exhwn"
}
}
});
};
// Send MSG to Discord if Failed
const sendFail = msg => {
msg.channel.send({
embed: {
color: 0x45c577,
author: {
name: "Adidas Stock Checker",
icon_url: "https://i.gyazo.com/a031d16c2a90b5fc6cf3c6563b9ecffe.png"
},
title: "Something went wrong...",
description: `Example: ${config.Discord.prefix}adistock B37705`,
timestamp: new Date(),
footer: {
icon_url: "https://i.gyazo.com/a031d16c2a90b5fc6cf3c6563b9ecffe.png",
text: "Adidas Stock Checker | @exhwn"
}
}
});
};
client.login(config.Discord.token);