bot: make that new guilds get regisetered upon the bot joining the server so the slash commands gets registered

This commit is contained in:
Ayden Jahola 2024-10-15 11:06:55 +01:00
parent 602cdcd03f
commit 7cbf47b530
No known key found for this signature in database
GPG key ID: 71DD90AE4AE92742
2 changed files with 36 additions and 28 deletions

View file

@ -42,17 +42,30 @@ function loadCommands(dir) {
// Load all commands from the commands directory and its subdirectories // Load all commands from the commands directory and its subdirectories
loadCommands(path.join(__dirname, "commands")); 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 () => { client.once("ready", async () => {
console.log(`\n==============================`); console.log(`\n==============================`);
console.log(`🤖 Logged in as ${client.user.tag}`); console.log(`🤖 Logged in as ${client.user.tag}`);
console.log(`==============================`); console.log(`==============================`);
console.log(`📋 Registered Commands:\n`);
client.commands.forEach((command) => {
console.log(`🔹 /${command.data.name} - ${command.data.description}`);
});
console.log(`\n==============================\n`);
const commands = client.commands.map((cmd) => cmd.data.toJSON()); // Register commands for all existing guilds
const guilds = client.guilds.cache.map((guild) => guild.id);
for (const guildId of guilds) {
await registerCommands(guildId);
}
// Set bot status and activity // Set bot status and activity
client.user.setPresence({ client.user.setPresence({
@ -60,25 +73,20 @@ client.once("ready", async () => {
status: "online", status: "online",
}); });
const rest = new REST({ version: "10" }).setToken(process.env.BOT_TOKEN); console.log(`\n==============================\n`);
// Fetching the guild ID from MongoDB
let GUILD_ID;
try {
const serverSettings = await ServerSettings.findOne();
if (serverSettings) {
GUILD_ID = serverSettings.guildId;
} else {
console.error("No server settings found in MongoDB.");
return;
}
await rest.put(Routes.applicationGuildCommands(client.user.id, GUILD_ID), {
body: commands,
}); });
console.log("Successfully registered all application commands.");
} catch (err) { // Listen for new guild joins and register the guild ID in the database
console.error("Error registering application commands:", err); 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})`);
// Register slash commands for the new guild
await registerCommands(guild.id);
} catch (error) {
console.error("Error registering new server or commands:", error);
} }
}); });
@ -113,7 +121,7 @@ client.on("interactionCreate", async (interaction) => {
} }
}); });
client.on("Error", (err) => { client.on("error", (err) => {
console.error("Client error:", err); console.error("Client error:", err);
}); });

View file

@ -2,11 +2,11 @@ const mongoose = require("mongoose");
const ServerSettingsSchema = new mongoose.Schema({ const ServerSettingsSchema = new mongoose.Schema({
guildId: { type: String, required: true, unique: true }, guildId: { type: String, required: true, unique: true },
logChannelId: { type: String, required: true }, logChannelId: { type: String, required: false },
verifiedRoleName: { type: String, required: true }, verifiedRoleName: { type: String, required: false },
verificationChannelId: { type: String, required: true }, verificationChannelId: { type: String, required: false },
generalChannelId: { type: String, required: true }, generalChannelId: { type: String, required: false },
emailDomains: { type: [String], required: true }, emailDomains: { type: [String], required: false },
}); });
const ServerSettings = mongoose.model("ServerSettings", ServerSettingsSchema); const ServerSettings = mongoose.model("ServerSettings", ServerSettingsSchema);