forked from birbified/discord-mongo-currency
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
270 lines (207 loc) · 7.35 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const mongoose = require('mongoose');
const currencyModel = require('./models/CurrencyModel');
class DiscordCurrency {
/**
* @param {object} options - Manager Options
*/
constructor(options) {
if (!options.url) throw new Error('No connection string was provided.');
this.cache = new Map();
this.url = options.url;
mongoose.connect(this.url, {
useNewUrlParser: true,
useUnifiedTopology: true
});
}
/**
* @param {String} userId - Discord user ID
*/
async findUser(userId) {
if (!userId) throw new Error('No user ID was provided');
let user = this.cache.get(`${userId}`);
if (!user) {
let e = await currencyModel.findOne({ userId: userId });
if (!e) {
e = await currencyModel.create({
userId: userId,
bankSpace: 1000,
coinsInBank: 0,
coinsInWallet: 0
});
}
user = e;
this.cache.set(`${userId}`, user);
}
return user;
}
/**
*
* @param {string} userId - A discord user ID.
* @param {number} amount - Amount of coins to give.
*/
static async giveCoins(userId, amount) {
if (!userId) throw new TypeError("You didn't provide a user 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 });
if (!user) {
const newData = new currencyModel({
userId: userId,
fridgeSpace: 1000,
fridge: 0,
nuggets: parseInt(amount)
});
await newData.save()
.catch(err => console.log(err));
return amount;
}
user.nuggets += parseInt(amount);
await user.save()
.catch(err => console.log(err));
return amount;
}
/**
*
* @param {string} userId - A discord user ID.
* @param {string} amount - Amount of coins to deduct.
*/
static async deductCoins(userId, amount) {
if (!userId) throw new TypeError("You didn't provide a user 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 });
if (!user) {
const newData = new currencyModel({
userId: userId,
fridgeSpace: 1000,
fridge: 0,
nuggets: 0
});
await newData.save()
.catch(err => console.log(err));
return amount;
}
if (amount > user.nuggets) {
user.nuggets -= user.nuggets;
await user.save()
.catch(err => console.log(err));
return amount;
}
user.nuggets -= parseInt(amount);
await user.save()
.catch(err => console.log(err));
return amount;
}
/**
*
* @param {string} userId - A discord user ID.
* @param {string} amount - Amount of bank space to give.
*/
static async giveBankSpace(userId, amount) {
if (!userId) throw new TypeError("You didn't provide a user 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 });
if (!user) {
let newData = new currencyModel({
userId: userId,
fridgeSpace: 1000 + parseInt(amount),
fridge: 0,
nuggets: 0
});
await newData.save()
.catch(err => console.log(err));
return amount;
}
user.fridge += parseInt(amount);
await user.save()
.catch(err => console.log(err));
return amount;
}
/**
*
* @param {string} userId - A discord user ID.
*/
static async createUser(userId) {
if (!userId) throw new TypeError("Please provide a user ID.");
let user = await currencyModel.findOne({ userId: userId });
if (user) return false;
let newData = new currencyModel({
userId: userId,
fridgeSpace: 1000,
fridge: 0,
nuggets: 0
});
await newData.save()
.catch(err => console.log(err));
}
/**
*
* @param {string} userId - A discord user ID.
*/
static async deleteUser(userId) {
if (!userId) throw new TypeError("Please provide a user ID.");
let user = await currencyModel.findOne({ userId: userId });
if (!user) return false;
await currencyModel.findOneAndRemove({ userId: userId });
await user.save()
.catch(err => console.log(err));
}
/**
*
* @param {String} userId
* @param {Object} item
* @param {Number} amount
*/
static async addItem(userId, item, amount) {
if(!item) throw new TypeError('Please provide an item');
if(!amount) throw new TypeError('Please provide an amount');
if(isNaN(amount)) throw new TypeError('Please provide a valid amount');
let user = await currencyModel.findOne({ userId: userId});
if(!user) return false;
let useritem = user.inventory.find(i => i.name === item)
if(useritem) useritem.amount += amount;
else useritem = user.inventory.push(item);
user.save();
}
static async addBadge(userId, badge) {
if(!userId) throw new TypeError('Please provide a user ID');
if(!badge) throw new TypeError('Please provide a badge');
let user = await currencyModel.findOne({ userId: userId });
if(!user) return false;
const Badge = user.badges.find(b => b === badge);
if(!Badge) user.badges.push(badge)
else return false;
user.save();
}
/**
*
* @param {String} userId
* @private
* @returns {Promise<mongoose.Document[]>} Promise<mongoose.Document[]>
* @description Gives all the data saved in database
*/
static async _get() {
let user = await currencyModel.find();
if(!user) return false;
return user;
}
/**
*
* @param {String} userId
* @param {mongoose.Document} data
* @private
* @description Use the _get method to get the data, modify it and save/update it using this method.
*/
static async _update(userId, data) {
if(!userId) throw new TypeError('Please provide a valid user ID.');
if(!data) throw new TypeError('Please provide data to update.');
let user = await currencyModel.findOne({ userId: userId });
if(!user) return false;
user.updateOne(data);
}
}
module.exports = mongoCurrency;