forked from brandonapt/roblox-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
302 lines (281 loc) · 13.6 KB
/
index.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import Discord from 'discord.js';
import roblox = require('noblox.js');
import bodyParser = require('body-parser');
import fs from 'fs';
import express from 'express';
import config from './config';
import BotClient from './utils/classes/BotClient';
import CommandHelpers from './utils/classes/CommandHelpers';
import GroupHandler from './utils/classes/GroupHandler';
import UniverseHandler from './utils/classes/UniverseHandler';
import BetterConsole from './utils/classes/BetterConsole';
import CommandFile from './utils/interfaces/CommandFile';
import CommandInstance from './utils/interfaces/CommandInstance';
import UserEntry from './utils/interfaces/UserEntry';
import VerificationResult from './utils/interfaces/VerificationResult';
import checkBans from './utils/events/checkBans';
import checkAudits from './utils/events/checkAuditLog';
import checkSuspensions from './utils/events/checkSuspensions';
import checkCooldowns from './utils/events/checkCooldowns';
import checkAbuse from './utils/events/checkAbuse';
import checkSales from './utils/events/checkSales';
import checkLoginStatus from './utils/events/checkLoginStatus';
import checkMemberCount from './utils/events/checkMemberCount';
import checkJobIDs from './utils/events/checkJobIDs';
const client = new BotClient(config);
const app = express();
app.use(bodyParser.json());
export const commands: CommandInstance[] = [];
export const registeredCommands: CommandInstance[] = [];
app.get("/", async (request, response) => {
response.status(200).send("OK");
});
let listener = app.listen(process.env.PORT, () => {
BetterConsole.log(`Your app is currently listening on port: ${(listener.address() as any).port}`, true);
});
async function readCommands(path?: string) {
if(!path) path = "./commands";
let files = await fs.promises.readdir(path);
for(let i = 0; i < files.length; i++) {
let file = files[i];
if(!file.includes(".")) {
await readCommands(`${path}/${file}`);
} else {
file = file.replace(".ts", ".js"); // This is here because when it compiles to JS, it saves to the build directory, and it starts as build/index.js, so it's reading files in build/commands, hence the string change
let commandFile = require(`${path}/${file}`).default as CommandFile; // .default cause when you call "export default <x>" it adds a default property to it (idk why)
try {
let command = {
file: commandFile,
name: file.split('.')[0],
slashData: commandFile.slashData,
commandData: commandFile.commandData
}
commands.push(command);
} catch(e) {
console.error(`Couldn't load the command data for the ${file.split('.')[0]} command with error: ${e}`);
}
}
}
}
export async function registerSlashCommands(reload?: boolean) {
if(reload) {
commands.length = 0;
await readCommands();
}
let slashCommands = [];
if(client.config.groupIds.length === 0) client.config.lockedCommands = client.config.lockedCommands.concat(CommandHelpers.getGroupCommands());
if(client.config.universes.length === 0) client.config.lockedCommands = client.config.lockedCommands.concat(CommandHelpers.getGameCommands());
if(!client.config.xpSystem.enabled) client.config.lockedCommands = client.config.lockedCommands.concat(CommandHelpers.getXPCommands());
if(!client.config.counting.enabled) client.config.lockedCommands.push("setgoal");
for(let i = 0; i < commands.length; i++) {
let lockedCommandsIndex = client.config.lockedCommands.findIndex(c => c.toLowerCase() === commands[i].name);
let allowedCommandsIndex = CommandHelpers.allowedCommands.findIndex(c => c.toLowerCase() === commands[i].name);
if(lockedCommandsIndex !== -1 && allowedCommandsIndex === -1) {
BetterConsole.log(`Skipped registering the ${commands[i].name} command because it's locked and not part of the default allowed commands list`);
continue;
}
registeredCommands.push(commands[i]);
let commandData;
try {
commandData = commands[i].slashData.toJSON()
slashCommands.push(commandData);
} catch(e) {
console.error(`Couldn't load the slash command data for the ${commands[i].name} command with error: ${e}`);
}
}
let rest = new Discord.REST().setToken(client.config.DISCORD_TOKEN);
try {
await rest.put(Discord.Routes.applicationCommands(client.user.id), {body: slashCommands});
} catch(e) {
console.error(`There was an error while registering slash commands: ${e}`);
}
}
async function deleteGuildCommands() {
let rest = new Discord.REST().setToken(client.config.DISCORD_TOKEN);
let guilds = await client.guilds.fetch({limit: 200});
for(let i = 0; i < guilds.size; i++) {
let guild = guilds.at(i);
try {
await rest.put(Discord.Routes.applicationGuildCommands(client.user.id, guild.id), {body: []});
} catch(e) {
console.error(`There was an error while trying to delete guild commmands: ${e}`);
}
}
}
export async function loginToRoblox(robloxCookie: string) {
try {
client.robloxInfo = await roblox.setCookie(robloxCookie);
} catch {
console.error("Unable to login to Roblox");
client.user.setActivity("Logged Into Roblox? ❌");
client.isLoggedIn = false;
return;
}
BetterConsole.log(`Logged into the Roblox account - ${client.robloxInfo.UserName}`, true);
client.isLoggedIn = true;
for(let i = 0; i < client.config.groupIds.length; i++) {
let groupID = client.config.groupIds[i];
await checkAudits(groupID, client);
await checkAbuse(groupID, client);
await checkSales(groupID, client);
await checkMemberCount(groupID, client);
}
await checkBans(client);
await checkSuspensions(client);
await checkLoginStatus(client);
}
client.once('ready', async() => {
BetterConsole.log(`Logged into the Discord account - ${client.user.tag}`, true);
if(client.application.botPublic) {
console.warn("BOT IS PUBLIC | SHUTTING DOWN");
return process.exit();
}
checkCooldowns(client);
await roblox.setAPIKey(client.config.ROBLOX_API_KEY);
if(client.config.groupIds.length !== 0) {
await loginToRoblox(client.config.ROBLOX_COOKIE);
await GroupHandler.loadGroups();
}
if(client.config.universes.length !== 0) {
await UniverseHandler.loadUniverses();
await checkJobIDs(client);
}
await readCommands();
await deleteGuildCommands();
await registerSlashCommands();
});
client.on('interactionCreate', async(interaction: Discord.Interaction) => {
if(interaction.type !== Discord.InteractionType.ApplicationCommand) return;
let command = interaction.commandName.toLowerCase();
for(let i = 0; i < commands.length; i++) {
if(commands[i].name === command) {
try {
await interaction.deferReply({ephemeral: commands[i].commandData.isEphemeral});
} catch(e) {
console.error(e);
return; // This error only happens with the plugin command. Idk why
}
let args = CommandHelpers.loadArguments(interaction);
if(args["username"]) {
let usernames = args["username"].replaceAll(" ", "").split(",") as string[];
if(usernames.length > client.config.maximumNumberOfUsers) {
let embed = client.embedMaker({title: "Maximum Number of Users Exceeded", description: "You've inputted more users than the currently allowed maximum, please lower the amount of users in your command and try again", type: "error", author: interaction.user});
await interaction.editReply({embeds: [embed]});
return;
}
}
if(!CommandHelpers.checkPermissions(commands[i].file, interaction.member as Discord.GuildMember)) {
let embed = client.embedMaker({title: "No Permission", description: "You don't have permission to run this command", type: "error", author: interaction.user});
await interaction.editReply({embeds: [embed]});
return;
}
if(commands[i].file.commandData.hasCooldown) {
if(client.isUserOnCooldown(commands[i].file.slashData.name, interaction.user.id)) {
let embed = client.embedMaker({title: "Cooldown", description: "You're currently on cooldown for this command, take a chill pill", type: "error", author: interaction.user});
await interaction.editReply({embeds: [embed]});
return;
}
}
if(commands[i].file.commandData.preformGeneralVerificationChecks) {
let groupID = GroupHandler.getIDFromName(args["group"]);
let robloxID = await client.getRobloxUser(interaction.guild.id, interaction.user.id);
let verificationStatus: VerificationResult;
if(robloxID !== 0) {
verificationStatus = await client.preformVerificationChecks(groupID, robloxID, commands[i].commandData.permissionToCheck);
} else {
verificationStatus = {success: false, err: "User is not verified with Rover"};
}
if(!verificationStatus.success) {
let embed = client.embedMaker({title: "Verification Checks Failed", description: `You've failed the verification checks, reason: ${verificationStatus.err}`, type: "error", author: interaction.user});
await interaction.editReply({embeds: [embed]});
return;
}
}
let res;
try {
res = await commands[i].file.run(interaction, client, args);
} catch(e) {
let embed = client.embedMaker({title: "Error", description: "There was an error while trying to run this command. The error has been logged in the console", type: "error", author: interaction.user});
await interaction.editReply({embeds: [embed]});
console.error(e);
}
if(commands[i] && commands[i].file.commandData.hasCooldown) {
let commandCooldown = client.getCooldownForCommand(commands[i].file.slashData.name);
if(typeof(res) === "number") { // If we return a number, it means the cooldown multipler got calculated
client.commandCooldowns.push({commandName: commands[i].file.slashData.name, userID: interaction.user.id, cooldownExpires: Date.now() + (commandCooldown * res)});
} else if(args["username"]) {
let usernames = args["username"].replaceAll(" ", "").split(",") as string[];
client.commandCooldowns.push({commandName: commands[i].file.slashData.name, userID: interaction.user.id, cooldownExpires: Date.now() + (commandCooldown * usernames.length)});
} else {
client.commandCooldowns.push({commandName: commands[i].file.slashData.name, userID: interaction.user.id, cooldownExpires: Date.now() + commandCooldown});
}
}
}
}
});
client.on("interactionCreate", async(interaction: Discord.Interaction) => {
if(interaction.type !== Discord.InteractionType.ApplicationCommandAutocomplete) return;
let command = interaction.commandName.toLowerCase();
for(let i = 0; i < commands.length; i++) {
if(commands[i].name === command) {
try {
await commands[i].file.autocomplete(interaction, client);
} catch(e) {
console.error(e);
}
}
}
});
client.on("messageCreate", async(message: Discord.Message) => {
if(!client.config.xpSystem.enabled) return;
if(message.author.bot) return;
let xpData = JSON.parse(await fs.promises.readFile(`${process.cwd()}/database/xpdata.json`, "utf-8")) as UserEntry[];
let index = xpData.findIndex(v => v.discordID === message.author.id);
let userData: UserEntry;
if(index !== -1) {
userData = xpData[index];
} else {
userData = {
discordID: message.author.id,
robloxID: 0,
redeemedRewards: [],
xp: 0
}
}
userData.xp += client.config.xpSystem.earnings.messages;
if(index !== -1) {
xpData[index] = userData;
} else {
xpData.push(userData);
}
await fs.promises.writeFile(`${process.cwd()}/database/xpdata.json`, JSON.stringify(xpData));
});
client.on('messageReactionAdd', async(reaction: Discord.MessageReaction, user: Discord.User) => {
if(!client.config.xpSystem.enabled) return;
if(user.bot) return;
let xpData = JSON.parse(await fs.promises.readFile(`${process.cwd()}/database/xpdata.json`, "utf-8")) as UserEntry[];
let index = xpData.findIndex(v => v.discordID === user.id);
let userData: UserEntry;
if(index !== -1) {
userData = xpData[index];
} else {
userData = {
discordID: user.id,
robloxID: 0,
redeemedRewards: [],
xp: 0
}
}
userData.xp += client.config.xpSystem.earnings.reactions;
if(index !== -1) {
xpData[index] = userData;
} else {
xpData.push(userData);
}
await fs.promises.writeFile(`${process.cwd()}/database/xpdata.json`, JSON.stringify(xpData));
})
let oldMethod = console.error;
console.error = function(msg: string) {
if(!msg.toString().includes("ExperimentalWarning")) oldMethod(msg);
}
client.login(client.config.DISCORD_TOKEN);