UNPKG

rashi-discord-bot-lib

Version:

šŸš€ Powerful Discord bot framework with built-in database, event handling, and utilities

428 lines (330 loc) • 12 kB
# šŸš€ Discord Bot Library A powerful, feature-rich **Discord bot framework** built with TypeScript. It provides a complete toolkit for modern bot development, making it easy to build, scale, and maintain production-ready bots. ## ✨ Key Features - šŸ”§ **TypeScript First** – Full type safety, IntelliSense, and modern development practices. - šŸ—ƒļø **VerseDB Integration** – Lightweight database system with support for JSON, YAML, and MongoDB. - šŸ“ **Automatic Loading** – Seamless auto-registration of events and commands. - šŸ”’ **Built-in Encryption** – AES-secured storage for sensitive data. - šŸ‘€ **Hot Reloading** – File watcher for rapid development. - šŸ›”ļø **Crash Protection** – Automatic error handling and reporting. - šŸŽØ **Advanced Logging** – Color-coded logs with timestamps and categories. - šŸ“¦ **Flexible Package Manager Support** – Works with both Yarn and NPM. - šŸ“Š **Bot Analytics** – Integrated statistics and activity tracking. ## šŸ“¦ Installation ```bash # With Yarn (recommended) # šŸ†• Project Initialization yarn init # Initialize a new project yarn add rashi-discord-bot-lib discord.js yarn add -D typescript @types/node # šŸ“¦ Package Management yarn install # Install dependencies yarn add package-name # Add dependency yarn add -D package-name # Add dev dependency yarn remove package-name # Remove dependency yarn upgrade # Update all packages # šŸ”„ Development Workflow yarn dev # šŸ”„ Run with hot reload yarn build # šŸ—ļø Compile TypeScript yarn start # ā–¶ļø Run compiled code yarn pack # šŸ“¦ Package library ``` ### Development Scripts Add these scripts to your `package.json`: ```json { "scripts": { "dev": "tsx watch index.ts", "build": "tsc", "start": "node dist/index.js", "pack": "npm pack" } } ``` ### Environment Setup Create a `.env` file in your project root: ```env BOT_TOKEN=your_discord_bot_token_here MONGO_URI=mongodb://localhost:27017/your_database CRASH_WEBHOOK=https://discord.com/api/webhooks/your_webhook_url ``` ## šŸš€ Getting Started ### Basic Bot Setup Create your main `index.ts` file: ```ts import 'dotenv/config'; import { BotStarter, type BotStarterOptions } from 'rashi-discord-bot-lib'; import { Client, GatewayIntentBits } from 'discord.js'; import path from 'path'; import fs from 'fs'; // --- Ensure environment --- const token = process.env.BOT_TOKEN; if (!token) { console.error('āŒ Missing BOT_TOKEN in .env'); process.exit(1); } // --- Prepare project folders --- const root = process.cwd(); const dataDir = path.resolve(root, 'data'); const eventsDir = path.resolve(root, 'events'); const slashDir = path.resolve(root, 'commands', 'slash'); const prefixDir = path.resolve(root, 'commands', 'prefix'); for (const dir of [dataDir, eventsDir, slashDir, prefixDir]) { fs.mkdirSync(dir, { recursive: true }); } // --- Create Client --- const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, ], }); const starter = new BotStarter(); // --- Configure Options --- const options: BotStarterOptions = { bot: { token, logs: { terminal: true }, Database: { verse: { adapterType: 'json', path: dataDir }, mongo: { mongoURI: process.env.MONGO_URI!, dbName: 'bot' }, }, }, events: { path: eventsDir, recursive: true, }, commands: { slashPath: slashDir, prefixPath: prefixDir, prefix: '!', }, // anticrash: { enable: true, webhookURL: process.env.CRASH_WEBHOOK!, mention: '<@123>' }, }; async function startBot() { try { const res = await starter.start(client, options); // Expose databases for easy access (client as any).db = res.db; (client as any).mongo = res.mongodb; // Example: Save startup timestamp await res.db.set('bot.startedAt', new Date().toISOString()); console.log('āœ… Bot started.'); // Graceful shutdown const shutdown = async () => { console.log('šŸ›‘ Shutting down...'); await starter.shutdown(); process.exit(0); }; process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown); } catch (err) { console.error('āŒ Failed to start bot:', err); process.exit(1); } } startBot(); ``` ## šŸ“‚ Project Structure ``` your-bot/ ā”œā”€ā”€ data/ # Database files (auto-generated) ā”œā”€ā”€ events/ # Event handlers │ ā”œā”€ā”€ ready.ts │ └── messageCreate.ts ā”œā”€ā”€ commands/ │ ā”œā”€ā”€ slash/ # Slash commands │ │ ā”œā”€ā”€ ping.ts │ │ └── user.ts │ └── prefix/ # Prefix commands │ ā”œā”€ā”€ help.ts │ └── stats.ts ā”œā”€ā”€ .env # Environment variables ā”œā”€ā”€ index.ts # Main bot file ā”œā”€ā”€ package.json # Dependencies and scripts └── tsconfig.json # TypeScript configuration ``` ## šŸŽÆ Creating Commands ### Slash Commands Create a slash command in `commands/slash/ping.ts`: ```typescript import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js'; export default { data: new SlashCommandBuilder() .setName('ping') .setDescription('Replies with Pong!'), async execute(interaction: ChatInputCommandInteraction) { const ping = Date.now() - interaction.createdTimestamp; await interaction.reply(`šŸ“ Pong! Latency: ${ping}ms`); }, }; ``` Advanced slash command with options (`commands/slash/user.ts`): ```typescript import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js'; export default { data: new SlashCommandBuilder() .setName('user') .setDescription('Get user information') .addUserOption(option => option.setName('target') .setDescription('The user to get info about') .setRequired(true)), async execute(interaction: ChatInputCommandInteraction) { const user = interaction.options.getUser('target'); const member = interaction.guild?.members.cache.get(user!.id); await interaction.reply({ content: `šŸ‘¤ **${user!.tag}**\nJoined: ${member?.joinedAt?.toDateString()}`, ephemeral: true }); }, }; ``` ### Prefix Commands Create a prefix command in `commands/prefix/help.ts`: ```typescript import { Message, EmbedBuilder } from 'discord.js'; export default { name: 'help', description: 'Display help information', aliases: ['h', 'commands'], usage: '[command]', async execute(message: Message, args: string[]) { const embed = new EmbedBuilder() .setTitle('šŸ“‹ Bot Commands') .setDescription('Here are my available commands:') .addFields( { name: '!help', value: 'Show this help message', inline: true }, { name: '!ping', value: 'Check bot latency', inline: true }, { name: '!stats', value: 'Show bot statistics', inline: true } ) .setColor(0x00AE86) .setTimestamp(); await message.reply({ embeds: [embed] }); }, }; ``` ## šŸ“” Event Handling ### Ready Event Create `events/ready.ts`: ```typescript import { Client } from 'discord.js'; export default { name: 'ready', once: true, execute(client: Client) { console.log(`🟢 ${client.user?.tag} is now online!`); console.log(`šŸ“Š Serving ${client.guilds.cache.size} guilds`); // Set bot activity client.user?.setActivity('with Discord.js', { type: 'PLAYING' }); }, }; ``` ## šŸ—„ļø Database Usage ### VerseDB (JSON/YAML) ```typescript // In your commands or events const client = interaction.client; // or message.client // Set data await client.db.set('user.123456789.coins', 100); await client.db.set('guild.987654321.settings', { prefix: '!', welcomeChannel: '123456789', moderationLogs: true }); // Get data with default values const coins = await client.db.get('user.123456789.coins') || 0; const settings = await client.db.get('guild.987654321.settings') || {}; // Increment values const currentCoins = await client.db.get('user.123456789.coins') || 0; await client.db.set('user.123456789.coins', currentCoins + 50); // Check if data exists const hasProfile = await client.db.has('user.123456789.profile'); // Delete data await client.db.delete('user.123456789.tempData'); // Get all data (be careful with large datasets) const allData = await client.db.all(); // Advanced: Working with objects const userData = await client.db.get('user.123456789') || {}; userData.lastSeen = new Date().toISOString(); userData.messageCount = (userData.messageCount || 0) + 1; await client.db.set('user.123456789', userData); ``` ### MongoDB (if configured) ```typescript // Access MongoDB collections const users = client.mongo.db.collection('users'); const guilds = client.mongo.db.collection('guilds'); // Create user profile await users.insertOne({ userId: '123456789', username: 'JohnDoe', coins: 100, joinedAt: new Date(), stats: { messagesCount: 0, commandsUsed: 0 } }); // Find user const user = await users.findOne({ userId: '123456789' }); // Update user data await users.updateOne( { userId: '123456789' }, { $inc: { coins: 50, 'stats.commandsUsed': 1 }, $set: { lastActive: new Date() } } ); // Find multiple users const topUsers = await users.find({}) .sort({ coins: -1 }) .limit(10) .toArray(); // Aggregation example const userStats = await users.aggregate([ { $group: { _id: null, totalCoins: { $sum: '$coins' } } } ]).toArray(); ``` ## 🚨 Error Handling & Anti-crash ### Automatic Error Recovery ```typescript const options: BotStarterOptions = { // ... other options anticrash: { enable: true, webhookURL: process.env.CRASH_WEBHOOK, mention: '<@YOUR_USER_ID>', logErrors: true } }; ``` ## šŸ“– Documentation - [Events System](docs/events.md) – Learn how to create and manage event listeners. - [Command Handling](docs/commands.md) – Full guide on slash & prefix command setup. - [Database Management](docs/database.md) – Using VerseDB and MongoDB integrations. - [Crash Protection](docs/anticrash.md) – Handling unexpected runtime errors. ## šŸ› ļø Development Tools - **Hot Reloading** – Automatically refresh commands/events on save. - **Type Checking** – Strong TypeScript definitions throughout the library. - **Logging System** – Debug, info, warning, and error levels with colors. - **Extensible API** – Easily integrate third-party services or extend with custom modules. ## šŸ†˜ Support & Community - šŸ› **Issues**: [GitHub Issues](https://github.com/your-username/rashi-discord-bot-lib/issues) - šŸ’¬ **Discord**: Join our community server - šŸ“§ **Email**: support@your-domain.com - šŸ“– **Documentation**: Full docs coming soon ## šŸŽÆ Roadmap - [ ] 🌐 Web dashboard for bot management - [ ] šŸ”Œ Plugin system for extensions - [ ] šŸ” Advanced permission system - [ ] šŸŒ Multi-language support - [ ] šŸ“Š Built-in analytics dashboard - [ ] šŸ”„ Database migration tools - [ ] šŸ“± Mobile companion app - [ ] šŸ¤– AI-powered command suggestions ## šŸ¤ Contributing Contributions are welcome! Please fork the repo, create a feature branch, and submit a PR. Make sure to follow TypeScript coding conventions and include tests where possible. ## šŸ“œ License MIT License Ā© 2025 – Built with ā¤ļø for the Discord developer community.