forked from oldschoolgg/oldschoolbot
/
sell.ts
90 lines (77 loc) · 2.62 KB
/
sell.ts
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
import { KlasaMessage, CommandStore } from 'klasa';
import { Items } from 'oldschooljs';
import { toKMB } from 'oldschooljs/dist/util/util';
import { BotCommand } from '../../lib/BotCommand';
import { UserSettings } from '../../lib/UserSettings';
import { ClientSettings } from '../../lib/ClientSettings';
import itemIsTradeable from '../../lib/util/itemIsTradeable';
import cleanItemName from '../../lib/util/cleanItemName';
const options = {
max: 1,
time: 10000,
errors: ['time']
};
export default class extends BotCommand {
public constructor(store: CommandStore, file: string[], directory: string) {
super(store, file, directory, {
cooldown: 1,
usage: '<quantity:int{1}> <itemname:...string>',
usageDelim: ' ',
oneAtTime: true,
ironCantUse: true
});
}
async run(msg: KlasaMessage, [quantity, itemName]: [number, string]) {
if (msg.author.isIronman) throw `Iron players can't sell items.`;
const osItem = Items.get(cleanItemName(itemName));
if (!osItem) throw `That item doesnt exist.`;
if (!itemIsTradeable(osItem.id)) {
throw `That item isn't tradeable.`;
}
const priceOfItem = await this.client.fetchItemPrice(osItem.id);
let totalPrice = priceOfItem * quantity;
const hasItem = await msg.author.hasItem(osItem.id, quantity);
if (!hasItem) {
throw `You dont have ${quantity}x ${osItem.name}.`;
}
if (totalPrice > 3) {
totalPrice = Math.floor(totalPrice * 0.8);
}
if (!msg.flagArgs.confirm && !msg.flagArgs.cf) {
const sellMsg = await msg.channel.send(
`${msg.author}, say \`confirm\` to sell ${quantity} ${
osItem.name
} for ${totalPrice.toLocaleString()} (${toKMB(totalPrice)}).`
);
try {
await msg.channel.awaitMessages(
_msg =>
_msg.author.id === msg.author.id &&
_msg.content.toLowerCase() === 'confirm',
options
);
} catch (err) {
return sellMsg.edit(`Cancelling sale of ${quantity}x ${osItem.name}.`);
}
}
await msg.author.removeItemFromBank(osItem.id, quantity);
await msg.author.settings.update(
UserSettings.GP,
msg.author.settings.get(UserSettings.GP) + totalPrice
);
const itemSellTaxBank = this.client.settings.get(
ClientSettings.EconomyStats.ItemSellTaxBank
);
const dividedAmount = (priceOfItem * quantity * 0.2) / 1_000_000;
this.client.settings.update(
ClientSettings.EconomyStats.ItemSellTaxBank,
Math.floor(itemSellTaxBank + Math.round(dividedAmount * 100) / 100)
);
msg.author.log(`sold Quantity[${quantity}] ItemID[${osItem.id}] for ${totalPrice}`);
return msg.send(
`Sold ${quantity}x ${osItem.name} for ${totalPrice.toLocaleString()}gp (${toKMB(
totalPrice
)})`
);
}
}