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
- 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.
The backend is built on Bun, a fast all-in-one JavaScript runtime. Here's a breakdown of the main functionalities:
-
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.
-
Discord Bot Integration
- Utilizes discord.js to create and manage bot instances.
- Implements functions to fetch guild info, members, channels, and messages.
-
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.
-
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.
-
CORS Support
- Implements CORS headers to allow cross-origin requests from the frontend.
- The React frontend makes API calls to the Bun backend.
- The backend processes these requests, authenticating the session.
- For data requests, the backend interacts with the Discord API using the stored bot instance.
- The backend returns the fetched data to the frontend, which then renders it for the user.
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
[Previous sections remain unchanged]
-
Clone and install
git clone https://github.com/yourusername/membersense.git cd membersense/backend bun install
-
Configure environment
Update thebackend/.env
file. The .gitignore prevents deploying this file from being deployed.DISCORD_BOT_TOKEN=your_token_here
-
Discord Bot Permissions
Ensure your bot has the following permissions:
- View Channels
- Send Messages
- Read Message History
- View Server Insights
-
Run the server
bun run server.js
Server runs on
http://localhost:3000
. -
Development mode
bun --hot run server.js
-
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. - POST
-
Dockerization
- Create optimized Docker images for both the frontend and backend.
- Use multi-stage builds to minimize image size and improve security.
-
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.
-
Local Development
- Utilize Docker Compose for a seamless local development experience.
- Implement hot-reloading for both frontend and backend during development.
- 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.