-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
128 lines (103 loc) · 3.99 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
require("dotenv").config();
const fs = require("fs");
const Discord = require("discord.js");
const User = require("./models/User");
const { getWelcomeMessage, createNewUser } = require("./game/_GLOBAL_HELPERS");
const { msToHumanTime } = require("./game/_GLOBAL_HELPERS");
const { handleCaptcha } = require("./game/_GLOBAL_HELPERS/captcha");
const token = process.env.DISCORD_TOKEN;
const prefix = process.env.DISCORD_PREFIX;
const client = new Discord.Client();
client.commands = new Discord.Collection();
// reads all .js files from commands folder
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));
// configures commands
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once("ready", () => {
console.info("Ready!");
});
client.on("message", async (message) => {
const { author } = message;
// doesn't have correct prefix or is bot
if (!message.content.startsWith(prefix) || author.bot) return;
// splits the argument to an array eg '!duel @hawkmaster' => ['!','duel','@hawkmaster']
const args = message.content.slice(prefix.length).split(/ +/).map(a => a.toLowerCase());
// removes prefix and sets to lowercase
const commandName = args.shift().toLowerCase();
// looks for command or the alias of a command
const command =
client.commands.get(commandName) ||
client.commands.find((cmd) => cmd.aliases && cmd.aliases.includes(commandName),);
// if not found, do nothing
if (!command) return;
// if no arguments provided when argument is expected. eg '!duel' (should be '!duel @fenrew')
if (command.args && !args.length) {
let reply = `You didn't provide any arguments, ${author}!`;
// returns how to actually use the command
if (command.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
// sends the reply
return message.channel.send(reply);
}
// Goes through the args and checks if any of them are shortcuts
const { shortcuts } = command;
const updatedArgs = shortcuts ? args.map(a => shortcuts[a] || a).map(a => a.match(/\s/g) ? a.split(" ") : a).flat() : args;
if(args[0] === "shortcuts") {
if(shortcuts) {
const msg = Object.keys(shortcuts).map(shortcut => `**${shortcut}**: ${shortcuts[shortcut]}\n`);
return author.send(`__The shortcuts for '${command.name}' is:__\n\n${msg.join("\n")}`);
}
else {
return author.send(`There are no shortcuts for '${command.name}'.`);
}
}
let userProfile;
try{
userProfile = await User.findOne({ "account.userId": author.id });
}
catch (err) {
console.error("error: ", err);
message.reply("Something went wrong when trying to find the user");
}
// creates new user if not exist
if (!userProfile) {
userProfile = await createNewUser(author, message.channel.id);
message.channel.send(getWelcomeMessage(userProfile));
if(!(command.name === "help" || command.name === "info")) {
return;
}
}
// stops banned players
if (userProfile.account.banTime > Date.now()) {
return message.reply(`You are banned from Mega-RPG. You can plead for an unban at our support servers - or wait:\n **${msToHumanTime(userProfile.account.banTime - Date.now())}**`);
}
if (!userProfile.account.testUser && ["hunt", "collect", "raid", "fish"].includes(command.name) && Math.random() <= 0.01) {
return handleCaptcha(message, userProfile, 3, Date.now());
}
// adds command to statistics
if (Object.keys(userProfile.statistics).includes(command.name)) {
userProfile.statistics[command.name] += 1;
}
// saves the server id
if (!userProfile.account.servers.includes(message.channel.id)) {
userProfile.account.servers.push(message.channel.id);
}
// executes the command
try {
command.execute(message, updatedArgs, userProfile);
}
catch (error) {
console.error(error);
message.reply("there was an error trying to execute that command!");
}
});
client.login(token);
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};