Skip to content

Instantly share code, notes, and snippets.

@kerneljay

kerneljay/bot.js Secret

Created March 31, 2025 20:33
const { Client, IntentsBitField, Collection } = require('discord.js');
const fs = require('fs');
const path = require('path');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
require('dotenv').config();
const token = process.env.DISCORD_TOKEN;
const clientId = process.env.DISCORD_CLIENT_ID;
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.MessageContent,
IntentsBitField.Flags.GuildVoiceStates
],
});
client.commands = new Collection();
const commandFiles = fs.readdirSync(path.join(__dirname, 'commands')).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
const rateLimits = new Map();
const LIMIT = 5; // Max commands a user can run within the time frame
const TIME_FRAME = 10000; // Time frame in milliseconds (10 seconds)
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Better rate-limiting handling for interactions
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
const userId = interaction.user.id;
const now = Date.now();
if (!rateLimits.has(userId)) {
rateLimits.set(userId, []);
}
const timestamps = rateLimits.get(userId);
// Remove old timestamps outside the time frame
while (timestamps.length > 0 && now - timestamps[0] > TIME_FRAME) {
timestamps.shift();
}
if (timestamps.length >= LIMIT) {
return interaction.reply({
content: "⏳ You're sending commands too fast! Please wait a bit before trying again.",
ephemeral: true
});
}
timestamps.push(now); // Add new timestamp
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, client);
} catch (error) {
console.error(`❌ Error executing command: ${interaction.commandName}`, error);
await interaction.reply({ content: "⚠️ There was an error executing this command.", ephemeral: true });
}
});
// Handle rate-limit warnings globally
client.rest.on('rateLimited', (info) => {
console.warn(`⏳ Rate limited! Details:`, info);
// Optionally, you can add a global delay or logging here to track rate-limited requests
});
// Function to delay execution when rate-limited (optional custom logic)
async function rateLimitDelay() {
// If you want to add custom logic for handling rate-limits (e.g., increase delay based on info)
return new Promise(resolve => setTimeout(resolve, 10000)); // Example delay of 10 seconds
}
// Command Registration Handling (Optimized)
async function updateCommands() {
const guildId = '969933798915579934'; // Replace with your actual guild ID
const rest = new REST({ version: '10' }).setToken(token);
try {
// Fetch the existing commands from Discord
const existingCommands = await rest.get(Routes.applicationGuildCommands(clientId, guildId));
const newCommands = client.commands.map(command => command.data.toJSON());
// Compare the existing commands with the new ones and update if necessary
if (JSON.stringify(existingCommands) !== JSON.stringify(newCommands)) {
console.log('✅ Commands have changed, updating...');
await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: newCommands });
console.log('✅ Commands updated successfully!');
} else {
console.log('⚡ No changes in commands. Skipping update.');
}
} catch (error) {
console.error('❌ Error updating commands:', error);
}
}
// Initialize and update commands only once per hour, instead of every 30 seconds
client.on('ready', async () => {
console.log(`✅ Logged in as ${client.user.tag}!`);
// Run the update commands function immediately on bot start
await updateCommands();
// Update commands every hour (60 * 60 * 1000 milliseconds)
setInterval(updateCommands, 60 * 60 * 1000);
});
// Log in to Discord
client.login(token);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment