-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
135 lines (116 loc) · 4.61 KB
/
app.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
const config = require('./config');
const Discord = require('discord.js');
const fs = require('fs');
const Web = require('./web/app');
const { random } = require('./utils/utils');
const Embeds = require('./utils/embeds');
const Logger = require('./utils/logger');
const Cooldown = require('./utils/cooldown');
const package = require('./package.json');
const client = new Discord.Client();
const { Users, CurrencyShop, HouseShop, UserItems, Enemies, PetShop } = require('./dbObjects');
const Currency = new Discord.Collection();
const Cooldowns = new Discord.Collection();
const Commands = new Discord.Collection();
const PREFIX = config.prefix;
const TOKEN = config.token;
const VERSION = package.version;
const SHOP_RANDOMIZATION = false;
const WEB_ENABLED = false;
Reflect.defineProperty(Currency, 'add', {
/* eslint-disable-next-line func-name-matching */
value: async function add(id, amount) {
const user = Currency.get(id);
if (user) {
user.balance += Number(amount);
return user.save();
}
const newUser = await Users.create({ user_id: id, balance: amount });
Currency.set(id, newUser);
return newUser;
},
});
Reflect.defineProperty(Currency, 'getBalance', {
/* eslint-disable-next-line func-name-matching */
value: function getBalance(id) {
const user = Currency.get(id);
return user ? user.balance : 0;
},
});
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
Commands.set(file.slice(0, -3), command);
}
client.once('ready', async () => {
const allUsers = await Users.findAll();
allUsers.forEach(b => Currency.set(b.user_id, b));
if(WEB_ENABLED) Web.start();
Logger.log(`Logged in as ${client.user.tag}!`);
if(SHOP_RANDOMIZATION) {
/**
* Shop stock refresh funciton
*/
// eslint-disable-next-line no-unused-vars
const stockRefresh = setInterval(async function() {
Logger.log('Restocking stock!');
const currencyShopItems = await CurrencyShop.findAll();
currencyShopItems.forEach(i => {
if(i.buyable === 1 && i.category === 'Food') {
i.stock = random(5, 10);
i.save();
}
});
const houseShopItems = await HouseShop.findAll();
houseShopItems.forEach(i => {
if(i.buyable == 1) {
i.stock = random(5, 10);
i.save();
}
});
}, 1000 * 60 * 1);
// Five minutes
}
});
client.on('message', async message => {
// If message came from bot - ignore it
if (message.author.bot) return;
// Making sure user has account
Currency.add(message.author.id, 0);
// If message doesn't start with prefix - ignore it
if (!message.content.startsWith(PREFIX)) return;
// Get command and arguments from the message
const input = message.content.slice(PREFIX.length).trim();
if (!input.length) return;
const [, commandInput, commandArgs] = input.match(/(\w+)\s*([\s\S]*)/);
const command = Commands.get(commandInput) || Commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandInput));
const commandName = Commands.get(commandInput)?.name || Commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandInput))?.name;
// If command isn't found - ignore it
if(!command) return;
// If command isn't in cooldowns - add it
if (!Cooldowns.has(commandName)) {
Cooldowns.set(commandName, new Discord.Collection());
}
// Get current time, and set default cooldown time to 3 seconds
const now = Date.now();
const timestamps = Cooldowns.get(commandName);
const cooldownAmount = Cooldown.getTime(commandName);
// If user has cooldown for command - cancel execution and inform user
if(Cooldown.check(timestamps, cooldownAmount, now, commandName, message)) return;
try {
Logger.cmd(`${message.author.tag} executed '${commandName}' command`);
command.execute(message, commandArgs, Users, Enemies, UserItems, Currency, HouseShop, CurrencyShop, PetShop, PREFIX, VERSION, timestamps, now, cooldownAmount, client);
}
catch(error) {
Logger.error(`Caught error while executing '${commandName}' command: ${error}`);
message.channel.send(Embeds.error());
}
return;
});
try {
Logger.log('Logging into Discord API');
client.login(TOKEN);
}
catch (error) {
Logger.error(`Caught error while logging into Discord API: ${error}`);
}