Files
src
.gitignore
config.js
package.json
README.md
Code
20 1,243
src/bot.js
import { Client, GatewayIntentBits, ActivityType } from 'discord.js';
import config from '../config.js';
import logger from './utils/logger.js';
import cache from './utils/cache.js';
import * as storage from './utils/storage.js';
import { registerCommands } from './handlers/commandHandler.js';
import { startGlobalUpdateLoop, stopGlobalUpdateLoop } from './handlers/updateHandler.js';
/**
* The main Discord client instance.
* Intents: Guilds for guild/channel events, GuildMessages + MessageContent for prefix commands.
*/
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Discord Events
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
client.once('ready', async () => {
logger.success(`Bot is ready! Logged in as ${client.user.tag}`);
// Set bot presence
client.user.setPresence({
activities: [{ name: 'Minecraft Servers', type: ActivityType.Watching }],
status: 'online',
});
// Register commands from the commands directory
registerCommands(client);
// Load guild settings from JSON file into cache
await loadAllGuilds();
// Start the global status-update interval
startGlobalUpdateLoop(client);
logger.info(`Monitoring ${cache.size()} guild(s)`);
});
client.on('messageCreate', async (message) => {
// Ignore DM messages and bot messages
if (message.author.bot || !message.guild) return;
// Resolve prefix for this guild (cached or default)
const prefix = cache.getPrefix(message.guild.id) || config.defaultPrefix;
if (!message.content.startsWith(prefix)) return;
// Parse command name and arguments
const args = message.content.slice(prefix.length).trim().split(/\s+/);
const commandName = args.shift()?.toLowerCase();
if (!commandName) return;
// Find and execute the command
const command = client.commands.get(commandName);
if (!command) return;
try {
await command.execute(message, args, prefix);
} catch (error) {
logger.error(`Error executing command "${commandName}" in guild ${message.guild.id}:`, error);
// Attempt to notify the user without crashing
message.reply({ content: 'โ An unexpected error occurred while running that command. Please try again later.' }).catch(() => {});
}
});
client.on('error', (error) => {
logger.error('Discord client error:', error);
});
client.on('warn', (warning) => {
logger.warn('Discord client warning:', warning);
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Load Guild Settings Into Cache
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function loadAllGuilds() {
await storage.load();
for (const [guildId, settings] of storage.getAllGuilds()) {
cache.set(guildId, settings);
if (settings.prefix) cache.setPrefix(guildId, settings.prefix);
}
}
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Graceful Shutdown
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
function shutdown() {
logger.info('Shutting down gracefully...');
stopGlobalUpdateLoop();
client.destroy();
process.exit(0);
}
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Start
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function start() {
// Validate token before trying anything
if (!config.token) {
logger.error('DISCORD_TOKEN is not set in .env');
process.exit(1);
}
await client.login(config.token);
}
start();