-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (60 loc) · 2.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
import { Client, Events, GatewayIntentBits } from "discord.js";
import { getAkakcePrice } from "./akakce.js";
import { getCimriPrice } from "./cimri.js";
import items from "./items.json" assert { type: "json" };
import dotenv from "dotenv";
import fs from 'fs/promises';
dotenv.config();
const itemsFile = './items.json';
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once(Events.ClientReady, (c) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});
client.login(process.env.TOKEN);
async function main() {
client.on("ready", async () => {
const items = await getLowestPriceOfAllItems();
await fs.writeFile(itemsFile, JSON.stringify(items));
setInterval(async () => {
const items = await getLowestPriceOfAllItems();
await fs.writeFile(itemsFile, JSON.stringify(items));
}, 1000 * 3600);
});
}
async function getLowestPriceOfAllItems() {
for (let i = 0; i < items.length; i++) {
const item = items[i];
console.log(`Checking ${item.name}`);
let price = await getLowestPrice(item).catch(console.log);
console.log(`Price of ${item.name} is ${price} TL`);
// if (item.price < item.lowestPrice) {
// item.lowestPrice = item.price;
// client.channels.fetch(process.env.CHANNEL_ID).then(channel => {
// channel.send(`<@319141014755475467> ${item.name} is now ${price} TL`);
// });
// } else {
// client.channels.fetch(process.env.CHANNEL_ID).then(channel => {
// channel.send(`${item.name} is now ${price} TL`);
// });
// }
if (!!price && price != item.price) {
const prevPrice = item.price;
item.price = price;
item.lowestPrice = item.price < item.lowestPrice ? item.price : item.lowestPrice;
client.channels.fetch(process.env.CHANNEL_ID).then(channel => {
channel.send(`<@${item.discordUserId}> Price of ${item.name} has changed. New Price: ${price} TL\nPrevious Price: ${prevPrice || '<no data>'} TL | Lowest Price: ${item.lowestPrice} TL`);
});
}
}
return items;
}
async function getLowestPrice(item) {
const { price: akakcePrice } = await getAkakcePrice(item.akakce);
const { price: cimriPrice } = await getCimriPrice(item.cimri);
if (isNaN(akakcePrice) || isNaN(cimriPrice)) {
throw Error('At least one of the prices is null!');
}
// Select the lowest
return Math.min(akakcePrice, cimriPrice);
}
main();