forked from Suggester/Suggester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
168 lines (147 loc) · 5.73 KB
/
bot.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
require("dotenv").config();
const Discord = require("discord.js");
const Client = require("./utils/Client");
const chalk = require("chalk");
const { errorLog, commandLog } = require("./utils/logs");
const { fileLoader } = require("./utils/misc.js");
const { connect, connection } = require("mongoose");
const autoIncrement = require("mongoose-sequence");
const { basename, dirname } = require("path");
const fs = require("fs");
if (process.env.SENTRY_DSN) {
const {init} = require("@sentry/node");
if (process.env.NODE_ENV === "production") init({dsn: process.env.SENTRY_DSN});
}
const intents = new Discord.Intents(["GUILDS", "GUILD_EMOJIS", "GUILD_MESSAGES", "GUILD_MESSAGE_REACTIONS", "DIRECT_MESSAGES", "GUILD_MEMBERS"]);
const client = new Client({
ws: { intents: intents },
disableMentions: "everyone",
messageCacheLifetime: 450,
messageSweepInterval: 750,
partials: ["MESSAGE", "REACTION", "USER"]
});
if (!process.env.TOKEN) return console.log(chalk`{yellowBright [{bold MISSING}] Missing {bold process.env.TOKEN}}\n{red {bold Shutting Down}}`);
if (!process.env.MONGO) return console.log(chalk`{yellowBright [{bold MISSING}] Missing {bold process.env.MONGO}}\n{red {bold Shutting Down}}`);
connect(process.env.MONGO, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.catch((err) => {
console.log(chalk`{red [{bold DATABASE}] Connection error: ${err.stack}}`);
});
autoIncrement(connection);
connection.on("open", () => {
console.log(chalk`{gray [{bold DATABASE}] {bold Connected} to {bold MongoDB}!}`);
});
connection.on("error", (err) => {
console.log(chalk`{red [{bold DATABASE}] Error: ${err.stack}}`);
});
(async () => {
let eventFiles = await fileLoader("events");
let events = [];
for await (let file of eventFiles) {
const exclude = [];
if (exclude.includes(basename(file))) {
console.log("Skipping excluded file:", file);
continue;
}
if (!file.endsWith(".js")) continue;
let event = require(file);
let eventName = basename(file).split(".")[0];
client.on(eventName, (...args) => {
try {
event(Discord, client, ...args);
}
catch (err) {
errorLog(client, err, "Event Handler", `Event: ${eventName}`);
}
});
events.push(eventName);
//console.log(chalk`{yellow [{bold EVENT}] Loaded {bold ${eventName}}}`);
}
console.log(chalk`{yellow [{bold EVENT}] Loaded {bold ${events.length} events}}`);
let commandFiles = await fileLoader("commands");
for await (let file of commandFiles) {
if (!file.endsWith(".js")) continue;
let command = require(file);
let commandName = basename(file).split(".")[0];
let dirArray = dirname(file).split("/");
command.controls.module = dirArray[dirArray.length-1];
client.commands.set(commandName, command);
//console.log(chalk`{magenta [{bold COMMAND}] Loaded {bold ${command.controls.name}} ${file}}`);
}
console.log(chalk`{magenta [{bold COMMAND}] Loaded {bold ${client.commands.size} commands}}`);
fs.access("i18n", async function(error) {
if (error) console.log("Locales folder missing, please run `locales pull`");
else {
fs.readdir("i18n", (err, files) => {
files.forEach(file => {
if (!file.endsWith(".json")) return;
const localeCode = file.split(".")[0]; //Command to check against
const locale = require("./i18n/" + localeCode); //Command file
client.locales.set(localeCode, locale);
});
});
}
});
let slashCommandFiles = await fileLoader("slash_commands");
for await (let file of slashCommandFiles) {
if (!file.endsWith(".js")) continue;
let command = require(file);
let commandName = basename(file).split(".")[0];
client.slashcommands.set(commandName, command);
//console.log(chalk`{magenta [{bold COMMAND}] Loaded {bold ${command.controls.name}} ${file}}`);
}
console.log(chalk`{magenta [{bold COMMAND}] Loaded {bold ${client.slashcommands.size} slash commands}}`);
})();
client.ws.on("INTERACTION_CREATE", async interaction => {
try {
if (client.slashcommands.get(interaction.data.name)) {
commandLog(`<:slashcommands:785919558376488990> ${interaction.member.user.username}#${interaction.member.user.discriminator} (\`${interaction.member.user.id}\`) ran slash command \`${interaction.data.name}\` in the \`${interaction.channel_id}\` channel of \`${interaction.guild_id}\``, { client, content: "" });
if (!interaction.member) interaction.member = {user: interaction.user};
await client.slashcommands.get(interaction.data.name)(interaction, client, Discord);
}
} catch (e) {
console.log(e);
}
});
client.login(process.env.TOKEN)
.catch((err) => console.log(chalk`{cyan [{bold DISCORD}] Error logging in: ${err.stack}}`));
client.on("error", (err) => {
errorLog(client, err, "error", "something happened and idk what");
});
client.on("warn", (warning) => {
console.warn(warning);
});
process.on("unhandledRejection", (err) => { // this catches unhandledPromiserejectionWarning and other unhandled rejections
errorLog(client, err, "unhandledRejection", "oof something is broken x.x");
});
/**
* Define the chunk method in the prototype of an array
* that returns an array with arrays of the given size.
*
* @param chunkSize {Integer} Size of every group
*/
Object.defineProperty(Array.prototype, "chunk", {
value: function(chunkSize){
let temporal = [];
for (let i = 0; i < this.length; i+= chunkSize){
temporal.push(this.slice(i,i+chunkSize));
}
return temporal;
}
});
/**
* Define the chunk method in the prototype of an array
* that returns an array with arrays of the given size.
*
* @param chunkSize {Integer} Size of every group
*/
Object.defineProperty(Number.prototype, "toFixed", {
value: function(size){
// eslint-disable-next-line no-useless-escape
let re = new RegExp("^-?\\d+(?:\.\\d{0," + (size || -1) + "})?");
return this.toString().match(re)[0];
}
});