forked from Benson30065/GVRA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (77 loc) · 3.03 KB
/
index.js
File metadata and controls
94 lines (77 loc) · 3.03 KB
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
require("dotenv").config();
const { Client, Collection, GatewayIntentBits, REST, Routes } = require("discord.js");
const fs = require("fs");
const mongoose = require("mongoose");
const { token, mongodb } = process.env;
const prefix = "?";
mongoose.connect(mongodb)
.then(() => console.log("Connected to MongoDB successfully!"))
.catch((err) => console.error("MongoDB connection error:", err));
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});
client.commands = new Collection();
client.prefixCommands = new Collection();
client.commandArray = [];
const handleEvents = async () => {
const eventFiles = fs.readdirSync("./events").filter((file) => file.endsWith(".js"));
for (const file of eventFiles) {
const event = require(`./events/${file}`);
if (event.once) client.once(event.name, (...args) => event.execute(...args, client));
else client.on(event.name, (...args) => event.execute(...args, client));
}
};
const handlePrefixCommands = async () => {
try {
const commandFiles = fs.readdirSync("./commands/prefix").filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/prefix/${file}`);
client.commands.set(command.name, command);
}
} catch (error) {
console.error("Error while handling prefix commands:", error.stack);
}
};
const handleCommands = async () => {
const commandFiles = fs.readdirSync("./commands/slash").filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/slash/${file}`);
client.commands.set(command.data.name, command);
client.commandArray.push(command.data.toJSON());
}
const clientId = "1362496654346813632";
const guildId = "1362459122561188060";
const rest = new REST({ version: "10" }).setToken(token);
try {
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: client.commandArray,
});
console.log("Slash commands uploaded successfully.");
} catch (error) {
console.error("Error uploading slash commands:", error.stack);
}
};
client.handleEvents = handleEvents;
client.handlePrefixCommands = handlePrefixCommands;
client.handleCommands = handleCommands;
client.on("messageCreate", (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName);
if (command) {
try {
command.execute(message, args);
} catch (error) {
console.error("Error executing command:", error.stack);
message.reply("There was an error while executing that command.");
}
}
});
(async () => {
await client.handleEvents();
await client.handlePrefixCommands();
await client.handleCommands();
})();
client.login(token);