-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathvoucher.js
133 lines (67 loc) · 2.87 KB
/
voucher.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
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const vouchersUrl = 'https://pastebin.com/raw/GaUWAukN';
const claimedVouchersPath = path.join(__dirname, '../database/claimed_vouchers.json');
const coinBalancesPath = path.join(__dirname, '../database/coin_balances');
function loadClaimedVouchers() {
if (fs.existsSync(claimedVouchersPath)) {
return JSON.parse(fs.readFileSync(claimedVouchersPath, 'utf8'));
}
return {};
}
function saveClaimedVouchers(claimedVouchers) {
fs.writeFileSync(claimedVouchersPath, JSON.stringify(claimedVouchers, null, 2));
}
function loadCoinBalance(userId) {
const coinBalanceFile = path.join(coinBalancesPath, `${userId}.json`);
if (fs.existsSync(coinBalanceFile)) {
return JSON.parse(fs.readFileSync(coinBalanceFile, 'utf8'));
}
return 0;
}
function saveCoinBalance(userId, balance) {
const coinBalanceFile = path.join(coinBalancesPath, `${userId}.json`);
fs.writeFileSync(coinBalanceFile, JSON.stringify(balance));
}
module.exports = {
name: 'voucher',
description: 'Claim a voucher for coins. Visit leechshares for coins.',
role: 'user',
cooldown: 0,
credits: 'YAFB',
execute: async (api, event, args) => {
const userId = event.senderID;
const voucherCode = args[0];
if (!voucherCode) {
api.sendMessage('Please provide a voucher code.', event.threadID, event.messageID);
return;
}
try {
const response = await axios.get(vouchersUrl);
const vouchers = response.data;
const claimedVouchers = loadClaimedVouchers();
if (claimedVouchers[voucherCode] && claimedVouchers[voucherCode].includes(userId)) {
api.sendMessage('You have already claimed this voucher.', event.threadID, event.messageID);
return;
}
if (!vouchers[voucherCode]) {
api.sendMessage('Invalid voucher code.', event.threadID, event.messageID);
return;
}
const voucherAmount = vouchers[voucherCode];
let coinBalance = loadCoinBalance(userId);
coinBalance += voucherAmount;
saveCoinBalance(userId, coinBalance);
if (!claimedVouchers[voucherCode]) {
claimedVouchers[voucherCode] = [];
}
claimedVouchers[voucherCode].push(userId);
saveClaimedVouchers(claimedVouchers);
api.sendMessage(`Voucher claimed successfully! You have received ${voucherAmount} coins.`, event.threadID, event.messageID);
} catch (error) {
console.error('Error fetching vouchers:', error);
api.sendMessage('There was an error claiming the voucher. Please try again later.', event.threadID, event.messageID);
}
}
};