This repository was archived by the owner on Mar 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheconomy.ts
More file actions
164 lines (160 loc) · 4.93 KB
/
economy.ts
File metadata and controls
164 lines (160 loc) · 4.93 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* This file contains important functions used by bot commands that deal with the bot's economy.
* This mainly includes storing and updating the amount of currency a user has.
*/
export async function getUserCurrency(userId: string) {
let database = new pylon.KVNamespace('economy');
let balance = 0;
let success = true;
await database
.get<any[]>('balance')
.then((users) => {
if (users != null) {
let userSearch = (element: any) => element.id == userId;
let user = users.find(userSearch);
if (user != null) {
// The user's listing was found, return the amount.
balance = user.amount;
}
}
// The user's listing was not found, assume they have no currency.
return 0;
})
.catch(() => {
success = false;
});
if (success) {
return balance; // If the user's listing was not found, it is assumed they have no currency.
}
return -1; // There was an error with the database, report this error.
}
export async function addUserCurrency(userId: string, currency: number) {
let database = new pylon.KVNamespace('economy');
let success = true;
if (currency >= 0) {
await database
.get<any[]>('balance')
.then(async (users) => {
if (users != null) {
let userSearch = (element: any) => element.id == userId;
let index = users.findIndex(userSearch);
if (index != -1) {
// The user's listing was found, update the amount.
users[index].amount += currency;
} else {
// The user's listing was not found, add a new listing.
users.push({
id: userId,
amount: currency,
});
}
} else {
// The database has not been initialised, create it with a new listing.
users = [
{
id: userId,
amount: currency,
},
];
}
// Update the database with the new status of every user's currency.
await database.put('balance', users).catch(() => {
success = false;
});
})
.catch(() => {
success = false;
});
} else {
// Cannot add negative amount.
success = false;
}
if (success) {
return 1;
}
return -1; // There was an error with the database, report this error.
}
export async function subtractUserCurrency(userId: string, currency: number) {
let database = new pylon.KVNamespace('economy');
let success = true;
let poor = false;
if (currency >= 0) {
await database.get<any[]>('balance').then(async (users) => {
if (users != null) {
let userSearch = (element: any) => element.id == userId;
let index = users.findIndex(userSearch);
if (index != -1) {
// The user's listing was found, check if the amount can be subtracted.
if (users[index].amount >= currency) {
users[index].amount -= currency;
await database.put('balance', users).catch(() => {
// There was an error with the database, report this error.
success = false;
});
} else {
// The user cannot pay the amount specified.
poor = true;
}
} else {
// The user's listing was not found.
poor = true;
}
} else {
// The database has not been initialised.
poor = true;
}
});
}
if (poor) {
return 0;
} else if (success) {
return 1;
}
// There was an error with the database or the amount, report this error.
return -1;
}
export async function setUserCurrency(userId: string, currency: number) {
let database = new pylon.KVNamespace('economy');
let success = true;
if (currency >= 0) {
await database
.get<any[]>('balance')
.then(async (users) => {
if (users != null) {
let userSearch = (element: any) => element.id == userId;
let index = users.findIndex(userSearch);
if (index != -1) {
// The user's listing was found, set the amount.
users[index].amount = currency;
} else {
// The user's listing was not found, add a new listing.
users.push({
id: userId,
amount: currency,
});
}
} else {
// The database has not been initialised, create it with a new listing.
users = [
{
id: userId,
amount: currency,
},
];
}
// Update the database with the new status of every user's currency
await database.put('balance', users).catch(() => {
success = false;
});
})
.catch(() => {
success = false;
});
} else {
// Cannot set to a negative amount.
success = false;
}
if (success) {
return 1;
}
return -1; // There was an error with the database, report this error.
}