Files
commands
utils
.gitignore
index.js
package.json
README.md
Code
22 1,700
index.js
const { Client, GatewayIntentBits, Collection, REST, Routes, ActivityType } = require('discord.js');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildBans,
GatewayIntentBits.GuildWebhooks,
GatewayIntentBits.GuildModeration
]
});
client.commands = new Collection();
client.cooldowns = new Collection();
const dataDir = path.join(__dirname, 'data');
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
const backupsDir = path.join(dataDir, 'backups');
if (!fs.existsSync(backupsDir)) {
fs.mkdirSync(backupsDir, { recursive: true });
}
const permissionsFile = path.join(dataDir, 'permissions.json');
if (!fs.existsSync(permissionsFile)) {
fs.writeFileSync(permissionsFile, JSON.stringify({}), 'utf8');
}
const configFile = path.join(dataDir, 'config.json');
if (!fs.existsSync(configFile)) {
fs.writeFileSync(configFile, JSON.stringify({}), 'utf8');
}
const commandFiles = fs.readdirSync(path.join(__dirname, 'commands')).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
if (command.data && command.execute) {
client.commands.set(command.data.name, command);
}
}
const { startBackupScheduler } = require('./utils/backupScheduler');
client.once('ready', async () => {
console.log(`✅ Bot is ready! Logged in as ${client.user.tag}`);
client.user.setPresence({
activities: [{ name: 'RearMC Server', type: ActivityType.Watching }],
status: 'online'
});
const commands = Array.from(client.commands.values()).map(cmd => cmd.data.toJSON());
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
try {
console.log('🔄 Started refreshing application (/) commands.');
await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{ body: commands }
);
console.log('✅ Successfully reloaded application (/) commands.');
} catch (error) {
console.error('❌ Error refreshing commands:', error);
}
startBackupScheduler(client);
});
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
const { checkPermission } = require('./utils/permissionChecker');
if (!checkPermission(interaction.user.id, interaction.member, interaction.commandName, interaction.guild.id)) {
return interaction.reply({
content: '❌ You do not have permission to use this command.',
ephemeral: true
});
}
try {
await command.execute(interaction, client);
} catch (error) {
console.error('Error executing command:', error);
const errorMessage = { content: '❌ There was an error executing this command!', ephemeral: true };
if (interaction.replied || interaction.deferred) {
await interaction.followUp(errorMessage);
} else {
await interaction.reply(errorMessage);
}
}
});
client.on('guildMemberUpdate', async (oldMember, newMember) => {
const { getBoosterAlertChannel } = require('./utils/configManager');
const wasBooster = oldMember.premiumSince;
const isBooster = newMember.premiumSince;
if (!wasBooster && isBooster) {
const channelId = getBoosterAlertChannel(newMember.guild.id);
if (channelId) {
const channel = newMember.guild.channels.cache.get(channelId);
if (channel) {
await channel.send({
content: `🎉 **${newMember.user.tag}** just boosted the server! Thank you for your support! 💜`,
allowedMentions: { parse: [] }
});
}
}
}
});
client.on('error', error => {
console.error('Discord client error:', error);
});
process.on('unhandledRejection', error => {
console.error('Unhandled promise rejection:', error);
});
client.login(process.env.DISCORD_TOKEN);