-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscount_shorttime.js
90 lines (85 loc) · 2.48 KB
/
discount_shorttime.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
import { readFileSync, writeFileSync } from "bun:fs";
import { postToDiscord } from "./post_to_discord.js";
import { config } from "./config.js";
import {
getRandomValueWithChance,
generateDiscountCode,
addMinutesToIsoTime,
expiresIn,
} from "./functions.js";
const discountSpecialPath = "./docs/api/discount_special.json";
let skipRequest = false;
try {
const discountSpecialData = JSON.parse(
readFileSync(discountSpecialPath, "utf-8"),
);
const expiresAt = new Date(discountSpecialData.data.attributes.expires_at);
const now = new Date();
if (expiresAt > now) {
console.log("Special discount exists. Skipping...");
skipRequest = true;
}
} catch (error) {
console.error("Error reading discount_special.json:", error);
}
const data = {
data: {
type: "discounts",
attributes: {
name: "Limited time discount code!",
code: `LTD${generateDiscountCode(9)}`,
amount: getRandomValueWithChance(config.discountPercentages),
amount_type: "percent",
expires_at: addMinutesToIsoTime(
getRandomValueWithChance(config.discountDuration),
),
},
relationships: {
store: {
data: {
type: "stores",
id: config.storeId,
},
},
},
},
};
if (!skipRequest && Math.random() < config.chanceToRun) {
fetch("https://api.lemonsqueezy.com/v1/discounts", {
method: "POST",
headers: {
Accept: "application/vnd.api+json",
"Content-Type": "application/vnd.api+json",
Authorization: `Bearer ${process.env.LEMONSQUEEZY_API_KEY}`,
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((json) => {
if (json.data?.id) {
writeFileSync(
"docs/api/discount_shorttime.json",
JSON.stringify(json, null, 2),
);
console.log("Discount code created successfully");
if (Math.random() < config.chanceToShare) {
postToDiscord(
config.channelId,
`🎁 daisyUI Store: short time discount
Use code \`${json.data.attributes.code}\` at checkout to get ${json.data.attributes.amount}% discount on all products
${expiresIn(json.data.attributes.expires_at)}
https://daisyui.com/store`,
);
} else {
console.log("skipped posting to Discord");
}
} else {
console.error("Failed to create discount code:", json);
}
})
.catch((error) => {
console.error("Error:", error);
});
} else {
console.log("skipped");
}