Skip to content

yasheroic/discord-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Discord Short URL Generator Bot

A Discord bot that creates short URLs and stores them in MongoDB. Built with Discord.js, Express, and Mongoose.

Features

  • 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

Prerequisites

Installation

  1. Clone or download this repository

  2. Install dependencies:

    npm install
  3. Set up environment variables:

    • Create a .env file in the project root
    • Add your Discord bot token:
      DISCORD_TOKEN=your_discord_bot_token_here
      
  4. Set up MongoDB:

    • Install MongoDB locally or use MongoDB Atlas
    • Ensure MongoDB is running on mongodb://127.0.0.1:27017/discord-shorturl
  5. 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 .env file
    • Enable the required intents:
      • Message Content Intent
      • Server Members Intent (if needed)
  6. 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

Usage

Start both servers (recommended):

npm run dev

This starts both the Discord bot and the redirect server simultaneously.

Start servers separately:

# Terminal 1 - Discord Bot
npm start

# Terminal 2 - Redirect Server
npm run server

Bot Commands

  • Create short URL: create https://example.com
    • Bot responds with: Short URL is: http://localhost:8001/url/abc123
  • General message: Bot responds with "Hey from Bot 🤖👋"

Accessing Short URLs

When someone visits http://localhost:8001/url/abc123, they will be redirected to the original URL.

Project Structure

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

Dependencies

  • discord.js - Discord API wrapper
  • express - Web server for redirects
  • mongoose - MongoDB ODM
  • nanoid - Short ID generation
  • dotenv - Environment variable management
  • nodemon - Development server with auto-restart
  • concurrently - Run multiple commands simultaneously

Database Schema

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
}

Development

  • The bot uses nodemon for development
  • Both servers restart automatically when files change
  • MongoDB connection is handled in connect.js

Security Notes

  • Never commit your .env file - it contains sensitive information
  • Add .env to your .gitignore file
  • Keep your bot token secure and private
  • Consider using environment variables for MongoDB connection string in production

Troubleshooting

  • "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

Contributing

Feel free to submit issues and enhancement requests!

About

A Discord bot that creates short URLs from long URLs using nanoid. Features MongoDB storage, Express redirect server, and easy Discord commands. Perfect for quick URL sharing in Discord servers.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors