2024-09-02 18:32:03 +01:00
|
|
|
require("dotenv").config();
|
2024-09-02 19:12:59 +01:00
|
|
|
const {
|
|
|
|
Client,
|
|
|
|
GatewayIntentBits,
|
|
|
|
Collection,
|
|
|
|
REST,
|
|
|
|
Routes,
|
2024-10-29 00:38:47 +00:00
|
|
|
PresenceUpdateStatus,
|
2024-09-02 19:12:59 +01:00
|
|
|
} = require("discord.js");
|
|
|
|
const mongoose = require("mongoose");
|
2024-09-02 18:32:03 +01:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2024-09-25 23:34:35 +01:00
|
|
|
const ServerSettings = require("./models/ServerSettings");
|
2024-10-26 03:40:13 +01:00
|
|
|
const seedShopItems = require("./utils/seedShopItems");
|
2024-10-28 23:14:58 +00:00
|
|
|
const seedSpyfallLocations = require("./utils/seedSpyfallLocations");
|
2024-09-02 18:32:03 +01:00
|
|
|
|
|
|
|
const client = new Client({
|
|
|
|
intents: [
|
|
|
|
GatewayIntentBits.Guilds,
|
|
|
|
GatewayIntentBits.GuildMessages,
|
|
|
|
GatewayIntentBits.MessageContent,
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
client.commands = new Collection();
|
|
|
|
|
2024-09-05 03:50:51 +01:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2024-09-02 18:32:03 +01:00
|
|
|
}
|
|
|
|
|
2024-09-05 03:50:51 +01:00
|
|
|
// Load all commands from the commands directory and its subdirectories
|
|
|
|
loadCommands(path.join(__dirname, "commands"));
|
|
|
|
|
2024-10-15 11:06:55 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 19:12:59 +01:00
|
|
|
client.once("ready", async () => {
|
2024-09-02 18:51:45 +01:00
|
|
|
console.log(`\n==============================`);
|
|
|
|
console.log(`🤖 Logged in as ${client.user.tag}`);
|
|
|
|
console.log(`==============================`);
|
|
|
|
|
2024-10-15 11:06:55 +01:00
|
|
|
// Register commands for all existing guilds
|
|
|
|
const guilds = client.guilds.cache.map((guild) => guild.id);
|
2024-10-26 19:33:45 +01:00
|
|
|
|
2024-10-28 23:27:17 +00:00
|
|
|
await Promise.all(
|
|
|
|
guilds.map(async (guildId) => {
|
|
|
|
await seedShopItems(guildId);
|
|
|
|
await seedSpyfallLocations(guildId);
|
|
|
|
await registerCommands(guildId);
|
|
|
|
})
|
|
|
|
);
|
2024-09-02 18:51:45 +01:00
|
|
|
|
2024-09-02 19:20:49 +01:00
|
|
|
// Set bot status and activity
|
|
|
|
client.user.setPresence({
|
|
|
|
activities: [{ name: "Degenerate Gamers!", type: 3 }],
|
2024-10-29 00:38:47 +00:00
|
|
|
status: PresenceUpdateStatus.Online,
|
2024-09-02 19:20:49 +01:00
|
|
|
});
|
|
|
|
|
2024-10-15 11:06:55 +01:00
|
|
|
console.log(`\n==============================\n`);
|
|
|
|
});
|
2024-09-02 19:12:59 +01:00
|
|
|
|
2024-10-15 11:06:55 +01:00
|
|
|
// Listen for new guild joins and register the guild ID in the database
|
|
|
|
client.on("guildCreate", async (guild) => {
|
2024-09-02 19:12:59 +01:00
|
|
|
try {
|
2024-10-15 11:06:55 +01:00
|
|
|
// 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})`);
|
|
|
|
|
2024-10-26 19:33:45 +01:00
|
|
|
// seed items for new guild with guildId
|
|
|
|
await seedShopItems(guild.id);
|
|
|
|
|
2024-10-28 23:14:58 +00:00
|
|
|
// Seed spyfall locations for the new guild
|
|
|
|
await seedSpyfallLocations(guild.id);
|
|
|
|
|
2024-10-15 11:06:55 +01:00
|
|
|
// Register slash commands for the new guild
|
|
|
|
await registerCommands(guild.id);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error registering new server or commands:", error);
|
2024-09-02 19:12:59 +01:00
|
|
|
}
|
2024-09-02 18:32:03 +01:00
|
|
|
});
|
|
|
|
|
2024-09-02 19:12:59 +01:00
|
|
|
// MongoDB connection
|
|
|
|
mongoose
|
|
|
|
.connect(process.env.MONGODB_URI)
|
|
|
|
.then(() => console.log("✅ Connected to MongoDB"))
|
|
|
|
.catch((err) => console.error("❌ Failed to connect to MongoDB", err));
|
|
|
|
|
2024-09-02 18:32:03 +01:00
|
|
|
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);
|
2024-09-02 19:12:59 +01:00
|
|
|
} catch (err) {
|
2024-09-05 03:50:51 +01:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
2024-09-02 18:32:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-15 11:06:55 +01:00
|
|
|
client.on("error", (err) => {
|
2024-09-02 19:12:59 +01:00
|
|
|
console.error("Client error:", err);
|
|
|
|
});
|
|
|
|
|
2024-09-02 18:32:03 +01:00
|
|
|
client.login(process.env.BOT_TOKEN);
|