Our Discord bot that helps us with random things, now with Firestore database integration.
- Query and display Firestore database collections and documents in Discord channels
- List all collections available in your Firestore database
- Retrieve specific documents by ID with formatted display
- Automatically format complex data structures into readable Discord embeds
- Export large datasets as JSON attachments
- Built with TypeScript for type safety and maintainability
- Node.js v16 or higher
- pnpm package manager
- Discord bot token (from Discord Developer Portal)
- Firebase project with Firestore database
- Firebase service account credentials
- Visit the Discord Developer Portal
- Click "New Application" and name it
- Go to the "Bot" tab and click "Add Bot"
- Under "Privileged Gateway Intents", enable:
- Server Members Intent
- Message Content Intent
- Copy your bot token (you'll need this for the
.env
file) - Navigate to the "OAuth2" > "URL Generator" section
- Select scopes:
bot
andapplications.commands
- Select bot permissions:
- Read Messages/View Channels
- Send Messages
- Embed Links
- Attach Files
- Read Message History
- Use the generated URL to invite the bot to your server
- Go to the Firebase Console
- Create or select your project
- Set up Firestore database if not already configured
- Go to Project Settings > Service Accounts
- Click "Generate new private key" to download your service account JSON
- Rename this file to
firebase-service-account.json
and place it in the project root
-
Install dependencies:
pnpm install
-
Create a
.env
file with the following variables:# Discord Bot Configuration DISCORD_TOKEN=your_discord_bot_token_here DISCORD_CLIENT_ID=your_discord_application_client_id_here # Firebase Configuration FIREBASE_PROJECT_ID=your_firebase_project_id # Optional configurations # FIREBASE_PRIVATE_KEY_PATH=./path/to/service-account.json # COMMAND_PREFIX=!
-
Build the project:
pnpm build
-
Start the bot:
pnpm start
Command | Description | Usage |
---|---|---|
!help |
Display available commands | !help |
!ping |
Check if bot is responding | !ping |
!collections |
List all Firestore collections | !collections |
!query |
Retrieve documents from a collection | !query <collection> [limit=10] |
!get |
Get a specific document by ID | !get <collection> <documentId> |
For development with auto-reloading:
pnpm dev
src/
├── bot/ # Discord bot client setup
├── commands/ # Command handling logic
├── db/ # Firestore database connection
├── utils/ # Utility functions
├── config.ts # Environment configuration
└── index.ts # Application entry point
To create a new command:
- Open
src/commands/handler.ts
- Define a new command object following the existing pattern:
const newCommand: Command = { name: 'commandname', description: 'Description of what the command does', usage: 'commandname <required_arg> [optional_arg]', execute: async (message: Message, args: string[]): Promise<void> => { // Command implementation } };
- Register the command by adding it to the registerCommand calls:
registerCommand(newCommand);
For production deployment:
- Ensure all environment variables are properly set in your production environment
- Build the TypeScript code:
pnpm build
- Start the production server:
pnpm start
# Install PM2 globally
npm install -g pm2
# Start the bot with PM2
pm2 start dist/index.js --name "discord-firestore-bot"
# Configure PM2 to start on system boot
pm2 startup
pm2 save
Bot doesn't respond to commands:
- Check if the bot is online in your Discord server
- Ensure you're using the correct command prefix (default is
!
) - Verify the bot has proper permissions in the channel
- Check console logs for any errors
Firestore connection issues:
- Verify your
firebase-service-account.json
file is valid and has correct permissions - Check that your Firebase project ID in
.env
matches the service account - Ensure your Firestore database exists and is not in locked mode
TypeScript build errors:
- Run
pnpm build
to see detailed error messages - Make sure all dependencies are installed:
pnpm install
To enable verbose logging for debugging:
NODE_ENV=development pnpm dev
If you encounter issues not covered here, check:
- Discord.js documentation: https://discord.js.org/
- Firebase Admin SDK docs: https://firebase.google.com/docs/admin/setup