forked from ChristopherBThai/Discord-OwO-Bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
weaponshards.js
260 lines (222 loc) · 7.36 KB
/
weaponshards.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
/*
* OwO Bot for Discord
* Copyright (C) 2019 Christopher Thai
* This software is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
* For more information, see README.md and LICENSE
*/
const CommandInterface = require('../../CommandInterface.js');
const ranks = [
['common', 'cw', 'commonweapons', 'commonweapon'],
['uncommon', 'uw', 'uncommonweapons', 'uncommonweapon'],
['rare', 'rw', 'rareweapon', 'rareweapons'],
['epic', 'ew', 'epicweapons', 'epicweapon'],
[
'mythic',
'mythical',
'mw',
'mythicalweapons',
'mythicalweapon',
'mythicweapons',
'mythicweapon',
],
['legendary', 'lw', 'legendaryweapons', 'legendaryweapon'],
['fabled', 'fable', 'fw', 'fabledweapons', 'fabledweapon', 'fableweapons', 'fableweapon'],
];
const shardEmoji = '<:weaponshard:655902978712272917>';
const dismantleEmoji = '🔨';
const weaponUtil = require('./util/weaponUtil.js');
const WeaponInterface = require('./WeaponInterface.js');
module.exports = new CommandInterface({
alias: ['weaponshard', 'ws', 'weaponshards', 'dismantle'],
args: '',
desc: '',
example: [''],
related: ['owo weapon'],
permissions: ['sendMessages', 'embedLinks', 'addReactions'],
group: ['animals'],
cooldown: 5000,
half: 80,
six: 500,
bot: true,
execute: async function (p) {
if (!p.args.length) {
await displayWeaponShards(p);
} else {
let arg = p.args[0].toLowerCase();
for (let i in ranks) {
if (ranks[i].includes(arg)) {
await dismantleRank(p, i);
return;
}
}
await dismantleId(p, arg);
}
},
});
async function displayWeaponShards(p) {
let sql = `SELECT shards.count FROM shards INNER JOIN user ON shards.uid = user.uid WHERE id = ${p.msg.author.id};`;
let result = await p.query(sql);
let shards = 0;
if (result && result[0]) shards = result[0].count;
shards = p.global.toFancyNum(shards);
p.replyMsg(shardEmoji, `, you currently have **${shards}** Weapon Shards!`);
}
async function dismantleRank(p, rankLoc) {
// (min,max]
let min = 0,
max = 0;
for (let i = 0; i <= rankLoc; i++) {
let rank = WeaponInterface.ranks[i];
min = max;
max += rank[0];
}
min *= 100;
max *= 100;
let lastRank = rankLoc == WeaponInterface.ranks.length - 1;
/* Grab the item we will sell */
let sql = `SELECT
user.uid,
a.uwid, a.wid, a.stat, a.rrcount, a.rrattempt, a.wear,
b.pcount, b.wpid, b.stat as pstat,
c.uwid as tt, c.kills
FROM user
LEFT JOIN user_weapon a ON user.uid = a.uid
LEFT JOIN user_weapon_passive b ON a.uwid = b.uwid
LEFT JOIN user_weapon_kills c ON a.uwid = c.uwid
WHERE user.id = ${p.msg.author.id} AND avg >${min === 0 ? '=' : ''} ${min} ${
lastRank ? '' : `AND avg <= ${max}`
} AND a.pid IS NULL AND a.favorite != 1 LIMIT 500;`;
let result = await p.query(sql);
/* not a real weapon! */
if (!result[0]) {
p.errorMsg(', you do not have any weapons with this rank!', 3000);
return;
}
/* Parse emoji and uwid */
let weapon = weaponUtil.parseWeaponQuery(result);
let weapons = [];
let weaponsSQL = [];
let price = weaponUtil.shardPrices[WeaponInterface.ranks[rankLoc][1]];
let rank = WeaponInterface.ranks[rankLoc][2] + ' **' + WeaponInterface.ranks[rankLoc][1] + '**';
for (var key in weapon) {
let tempWeapon = weaponUtil.parseWeapon(weapon[key]);
if (!tempWeapon.unsellable) {
weapons.push(tempWeapon.emoji);
weaponsSQL.push(tempWeapon.ruwid);
}
}
weaponsSQL = '(' + weaponsSQL.join(',') + ')';
if (weapons.length <= 0) {
p.errorMsg(', you do not have any weapons with this rank!', 3000);
return;
}
if (!price) {
p.errorMsg(', Something went terribly wrong...');
return;
}
let uid = await p.global.getUid(p.msg.author.id);
sql = `DELETE user_weapon_passive FROM user
LEFT JOIN user_weapon ON user.uid = user_weapon.uid
LEFT JOIN user_weapon_passive ON user_weapon.uwid = user_weapon_passive.uwid
WHERE id = ${p.msg.author.id}
AND user_weapon_passive.uwid IN ${weaponsSQL}
AND user_weapon.pid IS NULL;`;
sql += `DELETE user_weapon_kills FROM user
LEFT JOIN user_weapon ON user.uid = user_weapon.uid
LEFT JOIN user_weapon_kills ON user_weapon.uwid = user_weapon_kills.uwid
WHERE id = ${p.msg.author.id}
AND user_weapon_kills.uwid IN ${weaponsSQL}
AND user_weapon.pid IS NULL;`;
sql += `DELETE user_weapon FROM user
LEFT JOIN user_weapon ON user.uid = user_weapon.uid
WHERE id = ${p.msg.author.id}
AND user_weapon.uwid IN ${weaponsSQL}
AND user_weapon.pid IS NULL;`;
result = await p.query(sql);
/* Check if deleted */
if (result[1].affectedRows == 0) {
p.errorMsg(', you do not have a weapon with this id!', 3000);
return;
}
/* calculate rewards */
price *= result[1].affectedRows;
sql = `INSERT INTO shards (uid,count) VALUES (${uid},${price}) ON DUPLICATE KEY UPDATE count = count + ${price};`;
result = await p.query(sql);
p.replyMsg(
dismantleEmoji,
`, You dismantled all of your ${rank} weapons for **${price}** ${shardEmoji} WeaponShards!\n${
p.config.emoji.blank
} **| Dismantled:** ${weapons.join('')}`
);
p.logger.incr('shards', price, { type: 'dismantle' }, p.msg);
}
async function dismantleId(p, uwid) {
uwid = weaponUtil.expandUWID(uwid);
if (!uwid) {
p.errorMsg(', you do not have a weapon with this id!', 3000);
return;
}
/* Grab the item we will sell */
const weapon = await weaponUtil.getWeapon(uwid, p.msg.author.id);
/* not a real weapon! */
if (!weapon) {
p.errorMsg(', you do not have a weapon with this id!', 3000);
return;
}
/* If an animal is using the weapon */
if (weapon.animal?.name) {
p.errorMsg(', please unequip the weapon to dismantle it!', 3000);
return;
}
/* Is this weapon sellable? */
if (weapon.unsellable) {
p.errorMsg(', This weapon cannot be dismantled!');
return;
}
if (weapon.favorite) {
p.errorMsg(', unfavorite this weapon to sell!');
return;
}
/* Get weapon price */
let price = weaponUtil.shardPrices[weapon.rank.name];
if (!price) {
p.errorMsg(', Something went terribly wrong...');
return;
}
let uid = await p.global.getUid(p.msg.author.id);
let sql = `DELETE user_weapon_passive FROM user
LEFT JOIN user_weapon ON user.uid = user_weapon.uid
LEFT JOIN user_weapon_passive ON user_weapon.uwid = user_weapon_passive.uwid
WHERE id = ${p.msg.author.id}
AND user_weapon_passive.uwid = ${uwid}
AND user_weapon.pid IS NULL;`;
if (weapon.hasTakedownTracker) {
sql += `DELETE user_weapon_kills FROM user
LEFT JOIN user_weapon ON user.uid = user_weapon.uid
LEFT JOIN user_weapon_kills ON user_weapon.uwid = user_weapon_kills.uwid
WHERE id = ${p.msg.author.id}
AND user_weapon_kills.uwid = ${uwid}
AND user_weapon.pid IS NULL;`;
}
sql += `DELETE user_weapon FROM user
LEFT JOIN user_weapon ON user.uid = user_weapon.uid
WHERE id = ${p.msg.author.id}
AND user_weapon.uwid = ${uwid}
AND user_weapon.pid IS NULL;`;
let result = await p.query(sql);
/* Check if deleted */
if (result[1].affectedRows == 0) {
p.errorMsg(', you do not have a weapon with this id!', 3000);
return;
}
/* Give shards*/
sql = `INSERT INTO shards (uid,count) VALUES (${uid},${price}) ON DUPLICATE KEY UPDATE count = count + ${price};`;
result = await p.query(sql);
p.replyMsg(
dismantleEmoji,
`, You dismantled a(n) **${weapon.rank.name} ${weapon.name}** ${weapon.rank.emoji}${
weapon.emoji
} for **${p.global.toFancyNum(price)}** ${shardEmoji} Weapon Shard${price == 1 ? '' : 's'}!`
);
p.logger.incr('shards', price, { type: 'dismantle' }, p.msg);
}