rashi-discord-bot-lib
Version:
š Powerful Discord bot framework with built-in database, event handling, and utilities
428 lines (330 loc) ⢠12 kB
Markdown
# š 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.