-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (89 loc) · 2.93 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
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildEmojisAndStickers,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
],
});
require("dotenv/config");
const { MongoClient } = require("mongodb");
const fs = require("fs");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v10");
const token = process.env.TOKEN;
client.slashCommands = new Map();
client.prefixCommands = new Map();
client.aliases = new Map();
const rest = new REST({ version: "10" }).setToken(token);
// prefix command handler
const prefixCommandsArr = [];
const prefixFolders = fs.readdirSync("./src/commands/prefix");
for (const folder of prefixFolders) {
const commandFiles = fs.readdirSync(`./src/commands/prefix/${folder}`);
for (const file of commandFiles) {
const command = require(`./src/commands/prefix/${folder}/${file}`);
client.prefixCommands.set(command.name, command);
prefixCommandsArr.push(command.name, command);
if (command.aliases && Array.isArray(command.aliases)) {
command.aliases.forEach((alias) => {
client.aliases.set(alias, command.name);
});
}
}
}
// slash command handler
const slashCommandsArr = [];
const slashFolders = fs.readdirSync("./src/commands/slash");
for (const folder of slashFolders) {
const commandFiles = fs.readdirSync(`./src/commands/slash/${folder}`);
for (const file of commandFiles) {
const command = require(`./src/commands/slash/${folder}/${file}`);
let jsonData;
try {
jsonData = JSON.parse(command.data);
} catch (e) {}
if (jsonData) {
slashCommandsArr.push(command.data.toJSON());
} else {
slashCommandsArr.push(command.data);
}
client.slashCommands.set(command.data.name, command);
}
}
async function events() {
const client = await MongoClient.connect(process.env.MONGO);
console.log("Successfully connected to MongoDB!");
const database = client.db("CLUSTER_NAME");
return database;
}
events().then((database) => {
module.exports = {
// this is to to be able to include @param in other files
database,
};
// event handler
fs.readdirSync("./src/handlers").forEach(async (file) => {
const event = await require(`./src/handlers/${file}`);
client.on(event.name, (...args) => event.execute(...args, database, slashCommandsArr));
});
});
// nodejs events
process.on("unhandledRejection", (e) => {
console.error(e);
});
process.on("uncaughtException", (e) => {
console.error(e);
});
process.on("uncaughtExceptionMonitor", (e) => {
console.error(e);
});
client.login(token);
// made by discord user yoru#9267
// open to suggestions on how to improve the code!