-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (118 loc) · 4.18 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require("dotenv").config();
const {
Client,
GatewayIntentBits,
Collection,
REST,
Routes,
PresenceUpdateStatus,
} = require("discord.js");
const mongoose = require("mongoose");
const fs = require("fs");
const path = require("path");
const ServerSettings = require("./models/ServerSettings");
const seedShopItems = require("./utils/seedShopItems");
const seedSpyfallLocations = require("./utils/seedSpyfallLocations");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
client.commands = new Collection();
// Function to recursively read commands from subdirectories
function loadCommands(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
// If it's a directory, recurse into it
loadCommands(filePath);
} else if (file.endsWith(".js")) {
// If it's a JavaScript file, load the command
const command = require(filePath);
client.commands.set(command.data.name, command);
}
}
}
// Load all commands from the commands directory and its subdirectories
loadCommands(path.join(__dirname, "commands"));
async function registerCommands(guildId) {
const commands = client.commands.map((cmd) => cmd.data.toJSON());
const rest = new REST({ version: "10" }).setToken(process.env.BOT_TOKEN);
try {
await rest.put(Routes.applicationGuildCommands(client.user.id, guildId), {
body: commands,
});
console.log(`🔄 Successfully registered commands for guild: ${guildId}`);
} catch (error) {
console.error("Error registering commands:", error);
}
}
client.once("ready", async () => {
console.log(`\n==============================`);
console.log(`🤖 Logged in as ${client.user.tag}`);
console.log(`==============================`);
// Register commands for all existing guilds
const guilds = client.guilds.cache.map((guild) => guild.id);
await Promise.all(
guilds.map(async (guildId) => {
await seedShopItems(guildId);
await seedSpyfallLocations(guildId);
await registerCommands(guildId);
})
);
// Set bot status and activity
client.user.setPresence({
activities: [{ name: "Degenerate Gamers!", type: 3 }],
status: PresenceUpdateStatus.Online,
});
console.log(`\n==============================\n`);
});
// Listen for new guild joins and register the guild ID in the database
client.on("guildCreate", async (guild) => {
try {
// Create a new entry in the ServerSettings collection with just the guildId
await ServerSettings.create({ guildId: guild.id });
console.log(`✅ Registered new server: ${guild.name} (ID: ${guild.id})`);
// seed items for new guild with guildId
await seedShopItems(guild.id);
// Seed spyfall locations for the new guild
await seedSpyfallLocations(guild.id);
// Register slash commands for the new guild
await registerCommands(guild.id);
} catch (error) {
console.error("Error registering new server or commands:", error);
}
});
// MongoDB connection
mongoose
.connect(process.env.MONGODB_URI)
.then(() => console.log("✅ Connected to MongoDB"))
.catch((err) => console.error("❌ Failed to connect to MongoDB", err));
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, client);
} catch (err) {
console.error("Error executing command:", err);
if (interaction.deferred || interaction.ephemeral) {
await interaction.followUp({
content: "There was an error while executing this command!",
ephemeral: true,
});
} else {
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
});
client.on("error", (err) => {
console.error("Client error:", err);
});
client.login(process.env.BOT_TOKEN);