Skip to content

ModelEarth/MemberSense

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MemberSense

A powerful Bun app for the Discord API (and later Microsoft Outlook) which securely manages private keys in both Github and the .env.local file.

Also see Frontend

Tech Stack

Vite TailwindCSS Bun Discord.js Docker

Component Design

Backend

  • Main Components:
    • server.js: The Bun server that handles incoming requests and interacts with the Discord API.
    • .env: Securely stores environment variables like Discord bot token and guild ID.
    • discordBot.js: Implements the Discord bot functionality using discord.js.

Backend Logic

The backend is built on Bun, a fast all-in-one JavaScript runtime. Here's a breakdown of the main functionalities:

  1. Authentication

    • Uses nanoid to generate unique session IDs for each user login.
    • Stores bot instances and tokens in an in-memory Map for quick access.
  2. Discord Bot Integration

    • Utilizes discord.js to create and manage bot instances.
    • Implements functions to fetch guild info, members, channels, and messages.
  3. API Endpoints

    • /api/auth/login: Creates a new bot instance and returns session info.
    • /api/auth/logout: Destroys the bot instance and clears the session.
    • /api/members: Fetches all members of the Discord server.
    • /api/channels: Retrieves all text channels in the server.
    • /api/messages: Fetches messages from a specific channel.
  4. Error Handling and Logging

    • Implements robust error handling for API requests and Discord interactions.
    • Logs all incoming requests with timestamps and session IDs for debugging.
  5. CORS Support

    • Implements CORS headers to allow cross-origin requests from the frontend.

Data Flow

  1. The React frontend makes API calls to the Bun backend.
  2. The backend processes these requests, authenticating the session.
  3. For data requests, the backend interacts with the Discord API using the stored bot instance.
  4. The backend returns the fetched data to the frontend, which then renders it for the user.

Directory Structure

MemberSense
│
├── backend               # Backend (Bun + Discord bot)
│   ├── server.js         # Main server file
│   ├── .env              # Environment variables (Discord bot token, guild ID)
│   ├── discordBot.js     # Discord bot implementation
│   └── package.json      # Backend dependencies
│
└── README.md             # Project documentation

MemberSense

[Previous sections remain unchanged]

Backend Setup (Development)

  1. Clone and install

    git clone https://github.com/yourusername/membersense.git
    cd membersense/backend
    bun install
  2. Configure environment
    Update the backend/.env file. The .gitignore prevents deploying this file from being deployed.

    DISCORD_BOT_TOKEN=your_token_here
    
  3. Discord Bot Permissions
    Ensure your bot has the following permissions:

  • View Channels
  • Send Messages
  • Read Message History
  • View Server Insights
  1. Run the server

    bun run server.js

    Server runs on http://localhost:3000.

  2. Development mode

    bun --hot run server.js
  3. API Endpoints

    • POST /api/auth/login: Login
    • GET /api/members: Fetch members
    • GET /api/channels: Fetch channels
    • GET /api/messages?channelId=<id>: Fetch messages
    • POST /api/auth/logout: Logout

    Include Authorization header with session ID for authenticated requests.

Deployment Strategy

  1. Dockerization

    • Create optimized Docker images for both the frontend and backend.
    • Use multi-stage builds to minimize image size and improve security.
  2. Cloud Deployment

    • Deploy the backend to a scalable cloud platform (e.g., AWS ECS, Google Cloud Run).
    • Host the frontend on a CDN-backed static hosting service for optimal performance.
  3. Local Development

    • Utilize Docker Compose for a seamless local development experience.
    • Implement hot-reloading for both frontend and backend during development.

Future Improvements

  • Implement real-time updates using WebSockets for live Discord data.
  • Add functionality to manage member roles and permissions.
  • Integrate with Google Sheets API for data export and reporting.
  • Implement a caching layer (e.g., Redis) to optimize frequent data requests.
  • Add support for multiple Discord servers per user account.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published