A Discord bot that creates short URLs and stores them in MongoDB. Built with Discord.js, Express, and Mongoose.
- Short URL Generation: Creates short URLs from long URLs using nanoid
- MongoDB Storage: Stores URLs with user data in MongoDB database
- Web Redirect Server: Express server handles redirects for short URLs
- Discord Integration: Easy-to-use Discord commands
- Environment Configuration: Secure token and database management
- Node.js (v16 or higher)
- MongoDB (local or MongoDB Atlas)
- A Discord bot token from the Discord Developer Portal
-
Clone or download this repository
-
Install dependencies:
npm install
-
Set up environment variables:
- Create a
.envfile in the project root - Add your Discord bot token:
DISCORD_TOKEN=your_discord_bot_token_here
- Create a
-
Set up MongoDB:
- Install MongoDB locally or use MongoDB Atlas
- Ensure MongoDB is running on
mongodb://127.0.0.1:27017/discord-shorturl
-
Configure your Discord bot:
- Go to the Discord Developer Portal
- Create a new application or use an existing one
- Go to the "Bot" section
- Copy the bot token and paste it in your
.envfile - Enable the required intents:
- Message Content Intent
- Server Members Intent (if needed)
-
Invite the bot to your server:
- In the Developer Portal, go to "OAuth2" → "URL Generator"
- Select "bot" scope
- Select the permissions you want to grant
- Use the generated URL to invite the bot to your server
npm run devThis starts both the Discord bot and the redirect server simultaneously.
# Terminal 1 - Discord Bot
npm start
# Terminal 2 - Redirect Server
npm run server- Create short URL:
create https://example.com- Bot responds with:
Short URL is: http://localhost:8001/url/abc123
- Bot responds with:
- General message: Bot responds with "Hey from Bot 🤖👋"
When someone visits http://localhost:8001/url/abc123, they will be redirected to the original URL.
discord-bot/
├── index.js # Main Discord bot file
├── server.js # Express redirect server
├── connect.js # MongoDB connection utility
├── models/
│ └── url.js # URL schema and model
├── package.json # Dependencies and scripts
├── .env # Environment variables (create this)
└── README.md # This file
discord.js- Discord API wrapperexpress- Web server for redirectsmongoose- MongoDB ODMnanoid- Short ID generationdotenv- Environment variable managementnodemon- Development server with auto-restartconcurrently- Run multiple commands simultaneously
The bot creates a urls collection with documents containing:
{
shortID: "abc123",
redirectURL: "https://example.com",
shortURL: "http://localhost:8001/url/abc123",
createdAt: Date,
updatedAt: Date
}- The bot uses
nodemonfor development - Both servers restart automatically when files change
- MongoDB connection is handled in
connect.js
- Never commit your
.envfile - it contains sensitive information - Add
.envto your.gitignorefile - Keep your bot token secure and private
- Consider using environment variables for MongoDB connection string in production
- "localhost refused to connect": Make sure the redirect server is running (
npm run server) - MongoDB connection issues: Ensure MongoDB is running and accessible
- Bot not responding: Check your Discord bot token and permissions
Feel free to submit issues and enhancement requests!