-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
228 lines (173 loc) · 6.86 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var connection;
const mongoose = require('mongoose');
const currencyModel = require('./models/CurrencyModel');
mongoose.set('useFindAndModify', false);
class mongoCurrency {
/**
*
* @param {string} url - A MongoDB connection string.
*/
static async connect(url) {
if (!url) throw new TypeError("You didn't provide a MongoDB connection string");
connection = url;
return mongoose.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
});
}
/**
*
* @param {string} userId - A valid discord user ID.
* @param {string} guildId - A valid discord guild ID.
*/
static async findUser(userId, guildId) {
if (!userId) throw new TypeError("You didn't provide a user ID.");
if (!guildId) throw new TypeError("You didn't provide a guild ID.");
let user = await currencyModel.findOne({ userId: userId, guildId: guildId });
if (!user) return false;
return user;
}
/**
*
* @param {string} userId - A discord user ID.
* @param {string} guildId - A discord guild ID.
* @param {number} amount - Amount of coins to give.
*/
static async giveCoins(userId, guildId, amount) {
if (!userId) throw new TypeError("You didn't provide a user ID.");
if (!guildId) throw new TypeError("You didn't provide a guild ID.");
if (!amount) throw new TypeError("You didn't provide an amount of coins.");
if (isNaN(amount)) throw new TypeError("The amount must be a number.");
if (amount < 0) throw new TypeError("New amount must not be under 0.");
let user = await currencyModel.findOne({ userId: userId, guildId: guildId });
if (!user) {
const newData = new currencyModel({
userId: userId,
guildId: guildId,
bankSpace: 1000,
coinsInBank: 0,
coinsInWallet: parseInt(amount)
});
await newData.save()
.catch(err => console.log(err));
return amount;
}
user.coinsInWallet += parseInt(amount);
await user.save()
.catch(err => console.log(err));
return amount;
}
/**
*
* @param {string} userId - A discord user ID.
* @param {string} guildId - A discord guild ID.
* @param {string} amount - Amount of coins to deduct.
*/
static async deductCoins(userId, guildId, amount) { // deduct means "remove", something in particular.
if (!userId) throw new TypeError("You didn't provide a user ID.");
if (!guildId) throw new TypeError("You didn't provide a guild ID.");
if (!amount) throw new TypeError("You didn't provide an amount of coins.");
if (isNaN(amount)) throw new TypeError("The amount must be a number.");
if (amount < 0) throw new TypeError("New amount must not be under 0.");
let user = await currencyModel.findOne({ userId: userId, guildId: guildId });
if (!user) {
const newData = new currencyModel({
userId: userId,
guildId: guildId,
bankSpace: 1000,
coinsInBank: 0,
coinsInWallet: 0
});
await newData.save()
.catch(err => console.log(err));
return amount;
}
if (amount > user.coinsInWallet) {
user.coinsInWallet -= user.coinsInWallet;
await user.save()
.catch(err => console.log(err));
return amount;
}
user.coinsInWallet -= parseInt(amount);
await user.save()
.catch(err => console.log(err));
return amount;
}
/**
*
* @param {string} userId - A discord user ID.
* @param {string} guildId - A discord guild ID.
* @param {string} amount - Amount of bank space to give.
*/
static async giveBankSpace(userId, guildId, amount) {
if (!userId) throw new TypeError("You didn't provide a user ID.");
if (!guildId) throw new TypeError("You didn't provide a guild ID.");
if (!amount) throw new TypeError("You didn't provide an amount of coins.");
if (isNaN(amount)) throw new TypeError("The amount must be a number.");
if (amount < 0) throw new TypeError("New amount must not be under 0.");
let user = await currencyModel.findOne({ userId: userId, guildId: guildId });
if (!user) {
let newData = new currencyModel({
userId: userId,
guildId: guildId,
bankSpace: 1000 + parseInt(amount),
coinsInBank: 0,
coinsInWallet: 0
});
await newData.save()
.catch(err => console.log(err));
return amount;
}
user.bankSpace += parseInt(amount);
await user.save()
.catch(err => console.log(err));
return amount;
}
/**
*
* @param {string} userId - A discord user ID.
* @param {string} guildId - A discord guild ID.
*/
static async createUser(userId, guildId) {
if (!userId) throw new TypeError("Please provide a user ID.");
if (!guildId) throw new TypeError("Please provide a guild ID.");
let user = await currencyModel.findOne({ userId: userId, guildId: guildId });
if (user) return false;
let newData = new currencyModel({
userId: userId,
guildId: guildId,
bankSpace: 1000,
coinsInBank: 0,
coinsInWallet: 0
});
await newData.save()
.catch(err => console.log(err));
}
/**
*
* @param {string} userId - A discord user ID.
* @param {string} guildId - A discord guild ID.
*/
static async deleteUser(userId, guildId) {
if (!userId) throw new TypeError("Please provide a user ID.");
if (!guildId) throw new TypeError("Please provide a guild ID.");
let user = await currencyModel.findOne({ userId: userId, guildId: guildId });
if (!user) return false;
await currencyModel.findOneAndRemove({ userId: userId, guildId: guildId });
await user.save()
.catch(err => console.log(err));
}
/**
*
* @param {string} guildId - A discord guild ID.
* @param {number} amount - The amount of users to show.
*/
static async generateLeaderboard(guildId, amount) {
if (!guildId) throw new TypeError("Please provide a guild ID.");
if (!amount) throw new TypeError("Please provide the amount of users to show.");
if (isNaN(amount)) throw new TypeError("Amount must be a number");
let users = await currencyModel.find({ guildId: guildId }).sort([['coinsInWallet', 'descending']]).exec();
return users.slice(0, amount);
}
}
module.exports = mongoCurrency;