-
Notifications
You must be signed in to change notification settings - Fork 0
/
couponChecker.js
81 lines (72 loc) · 2.21 KB
/
couponChecker.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
import { config } from "dotenv";
import { createClient } from "@supabase/supabase-js";
import { updateBalance } from "./bot.js";
import { getUserBalance } from "./bot.js";
import { deleteRedeemLog } from "./bot.js";
config();
const { DATABASE_URL, SUPABASE_SERVICE_API_KEY } = process.env;
const supabase = createClient(DATABASE_URL, SUPABASE_SERVICE_API_KEY);
async function getCoupons() {
const { data, error } = await supabase.from("remittance").select();
if (!data) {
return {
coupons: [],
creationTimes: [],
funded: [],
originServerIDs: [],
userIDs: [],
amounts: [],
redemptions: [],
};
}
const coupons = data.map((a) => a.coupon);
const creationTimes = data.map((a) => a.creationDate);
const funded = data.map((a) => a.funded);
const originServerIDs = data.map((a) => a.originServerID);
const userIDs = data.map((a) => a.senderID);
const amounts = data.map((a) => a.amount);
const redemptions = data.map((a) => a.redeemed);
return {
coupons,
creationTimes,
funded,
originServerIDs,
userIDs,
amounts,
redemptions,
};
}
export async function deleteCoupon(coupon) {
const { error } = await supabase
.from("remittance")
.delete()
.eq("coupon", coupon);
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export async function checkCoupons() {
while (true) {
const couponList = await getCoupons();
for (let i = 0; i < couponList.coupons.length; i++) {
if (couponList.creationTimes.length > 0) {
if (
Date.now() - new Date(couponList.creationTimes[i]).getTime() >
300000 &&
!couponList.redemptions[i]
) {
await deleteCoupon(couponList.coupons[i]);
await deleteRedeemLog(couponList.coupons[i]);
if (couponList.funded[i]) {
const userID = couponList.userIDs[i];
const serverID = couponList.originServerIDs[i];
const balance = await getUserBalance(userID, serverID);
updateBalance(userID, serverID, balance + couponList.amounts[i]);
}
console.log("Deleted coupon: " + couponList.coupons[i]);
}
}
}
await sleep(60000);
}
}