-
Notifications
You must be signed in to change notification settings - Fork 0
Client Configuration
The Client class is the main entry point for @waforix/mocha. This guide covers all configuration options and how to use them effectively.
import { Client } from '@waforix/mocha';
const client = new Client({
token: 'your-discord-bot-token',
database: {
type: 'sqlite',
path: './data/stats.db'
}
});Your Discord bot token. Keep this secure and never commit it to version control.
const client = new Client({
token: process.env.DISCORD_TOKEN!,
// ... other options
});Database configuration object. See Database Configuration for detailed options.
// SQLite
database: {
type: 'sqlite',
path: './data/stats.db'
}
// PostgreSQL
database: {
type: 'postgres',
host: 'localhost',
port: 5432,
database: 'mocha_stats',
username: 'user',
password: 'password'
}
// MySQL
database: {
type: 'mysql',
host: 'localhost',
port: 3306,
database: 'mocha_stats',
username: 'user',
password: 'password'
}Configure caching behavior for improved performance.
cache: {
strategy: 'lru',
maxSize: 1000,
ttl: 300000, // 5 minutes in milliseconds
enableHeatmap: true
}Cache Options:
-
strategy: Cache strategy ('lru'or'memory') -
maxSize: Maximum number of cached items -
ttl: Time to live in milliseconds -
enableHeatmap: Enable heatmap generation for analytics
Enable metrics collection for performance monitoring.
enableMetrics: trueEnable the notification system for alerts and events.
enableNotifications: trueEnable rate limiting to prevent API abuse.
enableRateLimit: trueDiscord gateway intents. If not specified, default intents will be used.
import { INTENTS } from '@waforix/mocha';
intents: [
INTENTS.GUILDS,
INTENTS.GUILD_MESSAGES,
INTENTS.GUILD_VOICE_STATES,
INTENTS.GUILD_MESSAGE_REACTIONS
]Sharding configuration for large bots.
// Auto-sharding
shards: 'auto'
// Specific shard count
shards: 4
// Specific shard IDs
shards: [0, 1, 2, 3]import { Client, INTENTS } from '@waforix/mocha';
const client = new Client({
// Required
token: process.env.DISCORD_TOKEN!,
// Database
database: {
type: 'postgres',
host: process.env.DB_HOST || 'localhost',
port: parseInt(process.env.DB_PORT || '5432'),
database: process.env.DB_NAME || 'mocha_stats',
username: process.env.DB_USER || 'mocha_user',
password: process.env.DB_PASSWORD!,
ssl: process.env.NODE_ENV === 'production'
},
// Cache configuration
cache: {
strategy: 'lru',
maxSize: 5000,
ttl: 600000, // 10 minutes
enableHeatmap: true
},
// Feature flags
enableMetrics: true,
enableNotifications: true,
enableRateLimit: true,
// Discord configuration
intents: [
INTENTS.GUILDS,
INTENTS.GUILD_MESSAGES,
INTENTS.GUILD_VOICE_STATES,
INTENTS.GUILD_MESSAGE_REACTIONS,
INTENTS.GUILD_MEMBERS
],
// Sharding for large bots
shards: 'auto'
});Connect to Discord and initialize all systems.
await client.connect();Gracefully disconnect from Discord.
await client.disconnect();Get the autocomplete manager for registering autocomplete handlers.
const autocomplete = client.getAutocompleteManager();
autocomplete.register('command', 'option', handler);Get the command handler manager for registering command handlers.
const commands = client.getCommandHandlerManager();
commands.register('command', handler);Get direct database access (advanced usage).
const db = await client.getDatabase();Get statistics for a specific user.
const stats = await client.getUserStats('guild123', 'user456', 30);
console.log(`Messages: ${stats.messageCount}`);Get statistics for a guild.
const stats = await client.getGuildStats('guild123', 7);
console.log(`Total messages: ${stats.totalMessages}`);getLeaderboard(guildId: string, type: 'messages' | 'voice', limit?: number, days?: number): Promise<LeaderboardEntry[]>
Get leaderboard data for a guild.
const leaderboard = await client.getLeaderboard('guild123', 'messages', 10, 30);
console.log(`Top user: ${leaderboard[0].username}`);Get activity heatmap data.
const heatmap = await client.getActivityHeatmap('guild123', 'user456', 7);
console.log(`Peak activity: ${heatmap.peakHour}`);Export data in various formats.
const data = await client.exportData({
format: 'json',
guildId: 'guild123',
dateRange: {
start: new Date('2024-01-01'),
end: new Date('2024-12-31')
},
includeUsers: true,
includeMessages: true
});The Client extends EventEmitter and emits various events:
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('error', (error) => {
console.error('Client error:', error);
});
client.on('guildCreate', (guild) => {
console.log(`Joined guild: ${guild.name}`);
});
client.on('messageCreate', (message) => {
// Message tracking is automatic
});Use environment variables for different deployment environments:
const client = new Client({
token: process.env.DISCORD_TOKEN!,
database: {
type: process.env.DB_TYPE as 'sqlite' | 'postgres' | 'mysql',
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT || '5432'),
database: process.env.DB_NAME!,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
path: process.env.DB_PATH // For SQLite
},
enableMetrics: process.env.NODE_ENV === 'production',
enableRateLimit: process.env.NODE_ENV === 'production',
cache: {
strategy: 'lru',
maxSize: parseInt(process.env.CACHE_SIZE || '1000'),
ttl: parseInt(process.env.CACHE_TTL || '300000')
}
});- Use Environment Variables - Never hardcode sensitive information
- Enable Features Gradually - Start with basic features and add more as needed
- Monitor Performance - Use metrics in production
- Handle Errors - Always add error event listeners
- Graceful Shutdown - Properly disconnect on process termination
process.on('SIGINT', async () => {
console.log('Shutting down gracefully...');
await client.disconnect();
process.exit(0);
});For more advanced configuration options, see the API Reference.