-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommands.js
130 lines (122 loc) · 3.32 KB
/
commands.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
let browserProcess = null;
let page = null;
const puppeteer = require("puppeteer");
const { login, goToHomepage } = require("./login");
const { logger } = require("./utils");
const moment = require("moment");
const $cart = `[data-automation-id="cartSidebar"]`;
const addToCart = async (query, shouldShowImage) => {
const { searchProduct } = require("./search");
await login(page);
const { title, dollars, cents, image } = await searchProduct(
page,
query,
true,
shouldShowImage
);
return {
text: `Added ${title} to your cart. $${dollars}.${cents}`,
image
};
};
const searchProduct = async query => {
const { searchProduct } = require("./search");
await login(page);
const { title, cents, dollars, image } = await searchProduct(page, query);
logger("done searching");
return {
text: `I found ${title}. $${dollars}.${cents}\nText !add and I'll add it to your cart.`,
image
};
};
const getCart = async () => {
await login(page);
await page.waitForSelector($cart);
const cart = await page.$($cart);
const filename = `cart-${moment().format("X")}.png`;
await cart.screenshot({
path: `./screenshots/${filename}`
});
return {
text: `Here is your cart`,
image: process.env.SERVER_URL + filename
};
};
(async () => {
browserProcess = await puppeteer.launch({
userDataDir: "data",
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
`--remote-debugging-port=5000`
],
defaultViewport: { width: 1200, height: 800 }
});
logger("opened new browser");
page = await browserProcess.newPage();
logger("opening new page");
await goToHomepage(page);
})();
const prefixes = {
"!search": {
match: "search", // used to match type, useful if we have multiple for same function
value: "!search ", // what the user typed
lang: "en",
display: false,
handle: searchProduct
},
"!add": {
match: "add", // used to match type, useful if we have multiple for same function
value: "!add ", // what the user typed
lang: "en",
display: false,
handle: addToCart
},
"!cart": {
match: "cart", // used to match type, useful if we have multiple for same function
value: "!cart ", // what the user typed
lang: "en",
display: false,
handle: getCart
},
search: {
match: "search", // used to match type, useful if we have multiple for same function
value: "search ", // what the user typed
lang: "en",
display: true,
handle: searchProduct
},
add: {
match: "add", // used to match type, useful if we have multiple for same function
value: "add ", // what the user typed
lang: "en", // maybe use to respond in another language
display: true,
handle: addToCart
},
cart: {
match: "cart", // used to match type, useful if we have multiple for same function
value: "cart ", // what the user typed
lang: "en", // maybe use to respond in another language
display: true,
handle: getCart
}
};
const allCommands = () =>
Object.keys(prefixes)
.filter(p => prefixes[p].display)
.join(", ");
const getPrefix = text => {
const firstword = text.toLowerCase().split(" ")[0]; //
if (firstword in prefixes) {
return prefixes[firstword];
} else {
return null;
}
};
module.exports = {
getPrefix,
addToCart,
searchProduct,
getCart,
allCommands
};