-
Notifications
You must be signed in to change notification settings - Fork 16
feat: Add balance and leaderboard Discord slash commands #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
anumukul
wants to merge
3
commits into
TheSoftwareDevGuild:main
from
anumukul:feat/discord-balance-leaderboard
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,29 @@ | ||
| FROM node:20-alpine | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Copy package files | ||
| COPY package*.json ./ | ||
|
|
||
| # Install dependencies | ||
| RUN npm ci --only=production | ||
|
|
||
| # Copy source code | ||
| COPY . . | ||
|
|
||
| # Build TypeScript | ||
| RUN npm run build | ||
|
|
||
| # Create non-root user | ||
| RUN addgroup -g 1001 -S nodejs | ||
| RUN adduser -S discord-bot -u 1001 | ||
|
|
||
| # Change ownership of the app directory | ||
| RUN chown -R discord-bot:nodejs /app | ||
| USER discord-bot | ||
|
|
||
| # Expose port (for health checks if needed) | ||
| EXPOSE 3003 | ||
|
|
||
| # Start the bot | ||
| CMD ["npm", "start"] | ||
| FROM node:20-alpine | ||
| WORKDIR /app | ||
| # Copy package files | ||
| COPY package*.json ./ | ||
| # Install dependencies | ||
| RUN npm ci --only=production | ||
| # Copy source code | ||
| COPY . . | ||
| # Build TypeScript | ||
| RUN npm run build | ||
| # Create non-root user | ||
| RUN addgroup -g 1001 -S nodejs | ||
| RUN adduser -S discord-bot -u 1001 | ||
| # Change ownership of the app directory | ||
| RUN chown -R discord-bot:nodejs /app | ||
| USER discord-bot | ||
| # Expose port (for health checks if needed) | ||
| EXPOSE 3003 | ||
| # Start the bot | ||
| CMD ["npm", "start"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,95 +1,95 @@ | ||
| # Discord Bot | ||
|
|
||
| A Discord bot that tracks user activity and awards points for messages sent in Discord servers. | ||
|
|
||
| ## Features | ||
|
|
||
| - Tracks user messages and awards activity points | ||
| - Stores activity events in PostgreSQL database | ||
| - Rate limiting to prevent spam | ||
| - Configurable guild filtering | ||
| - Structured logging with Pino | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ### 1. Environment Setup | ||
|
|
||
| Create a `.env` file in the `discord-bot` directory: | ||
|
|
||
| ```bash | ||
| # Discord Bot Configuration | ||
| DISCORD_BOT_TOKEN=your_bot_token_here | ||
| DISCORD_APP_ID=your_app_id_here | ||
| DISCORD_GUILD_ID=your_guild_id_here | ||
|
|
||
| # Database Configuration | ||
| DATABASE_URL=postgresql://guild_user:guild_password@localhost:5433/guild_genesis | ||
|
|
||
| # Bot Behavior | ||
| POINTS_PER_MESSAGE=1 | ||
| LOG_LEVEL=info | ||
| MAX_MESSAGES_PER_MINUTE=10 | ||
| ``` | ||
|
|
||
| ### 2. Database Migration | ||
|
|
||
| Start PostgreSQL and run the migration: | ||
|
|
||
| ```bash | ||
| # Start PostgreSQL (if using Docker) | ||
| docker-compose up postgres -d | ||
|
|
||
| # Run migration | ||
| psql postgresql://guild_user:guild_password@localhost:5433/guild_genesis -f migrations/001_create_activity_events.sql | ||
| ``` | ||
|
|
||
| ### 3. Run the Bot | ||
|
|
||
| ```bash | ||
| # Development mode | ||
| npm run dev | ||
|
|
||
| # Production mode | ||
| npm run build && npm start | ||
| ``` | ||
|
|
||
| ### 4. Docker Deployment | ||
|
|
||
| ```bash | ||
| # Build and run with docker-compose | ||
| docker-compose up discord-bot | ||
| ``` | ||
|
|
||
| ## Database Schema | ||
|
|
||
| The bot creates an `activity_events` table with the following structure: | ||
|
|
||
| - `id`: UUID primary key | ||
| - `user_id`: Discord user ID (stable identifier) | ||
| - `user_name`: Discord username (may change) | ||
| - `amount`: Points awarded (default: 1) | ||
| - `event_type`: Type of event (currently only 'message') | ||
| - `date`: Timestamp of the event | ||
| - `processed_status`: Whether the event has been processed for minting | ||
| - `created_at`: Record creation timestamp | ||
|
|
||
| ## Architecture | ||
|
|
||
| - **src/index.ts**: Main bot logic and Discord.js client | ||
| - **src/env.ts**: Environment variable validation with Zod | ||
| - **src/db.ts**: Database connection and activity event operations | ||
| - **src/logger.ts**: Structured logging configuration | ||
| - **migrations/**: SQL migration files | ||
|
|
||
| ## Rate Limiting | ||
|
|
||
| The bot implements basic rate limiting to prevent spam: | ||
| - Maximum 10 messages per user per minute | ||
| - In-memory tracking (resets on bot restart) | ||
|
|
||
| ## Future Enhancements | ||
|
|
||
| - Additional event types (votes, reactions, etc.) | ||
| - Persistent rate limiting with Redis | ||
| - Health check endpoint | ||
| - Metrics and monitoring | ||
| # Discord Bot | ||
| A Discord bot that tracks user activity and awards points for messages sent in Discord servers. | ||
| ## Features | ||
| - Tracks user messages and awards activity points | ||
| - Stores activity events in PostgreSQL database | ||
| - Rate limiting to prevent spam | ||
| - Configurable guild filtering | ||
| - Structured logging with Pino | ||
| ## Quick Start | ||
| ### 1. Environment Setup | ||
| Create a `.env` file in the `discord-bot` directory: | ||
| ```bash | ||
| # Discord Bot Configuration | ||
| DISCORD_BOT_TOKEN=your_bot_token_here | ||
| DISCORD_APP_ID=your_app_id_here | ||
| DISCORD_GUILD_ID=your_guild_id_here | ||
| # Database Configuration | ||
| DATABASE_URL=postgresql://guild_user:guild_password@localhost:5433/guild_genesis | ||
| # Bot Behavior | ||
| POINTS_PER_MESSAGE=1 | ||
| LOG_LEVEL=info | ||
| MAX_MESSAGES_PER_MINUTE=10 | ||
| ``` | ||
| ### 2. Database Migration | ||
| Start PostgreSQL and run the migration: | ||
| ```bash | ||
| # Start PostgreSQL (if using Docker) | ||
| docker-compose up postgres -d | ||
| # Run migration | ||
| psql postgresql://guild_user:guild_password@localhost:5433/guild_genesis -f migrations/001_create_activity_events.sql | ||
| ``` | ||
| ### 3. Run the Bot | ||
| ```bash | ||
| # Development mode | ||
| npm run dev | ||
| # Production mode | ||
| npm run build && npm start | ||
| ``` | ||
| ### 4. Docker Deployment | ||
| ```bash | ||
| # Build and run with docker-compose | ||
| docker-compose up discord-bot | ||
| ``` | ||
| ## Database Schema | ||
| The bot creates an `activity_events` table with the following structure: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and guild_id should be added here |
||
| - `id`: UUID primary key | ||
| - `user_id`: Discord user ID (stable identifier) | ||
| - `user_name`: Discord username (may change) | ||
| - `amount`: Points awarded (default: 1) | ||
| - `event_type`: Type of event (currently only 'message') | ||
| - `date`: Timestamp of the event | ||
| - `processed_status`: Whether the event has been processed for minting | ||
| - `created_at`: Record creation timestamp | ||
| ## Architecture | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add new folder here |
||
| - **src/index.ts**: Main bot logic and Discord.js client | ||
| - **src/env.ts**: Environment variable validation with Zod | ||
| - **src/db.ts**: Database connection and activity event operations | ||
| - **src/logger.ts**: Structured logging configuration | ||
| - **migrations/**: SQL migration files | ||
| ## Rate Limiting | ||
| The bot implements basic rate limiting to prevent spam: | ||
| - Maximum 10 messages per user per minute | ||
| - In-memory tracking (resets on bot restart) | ||
| ## Future Enhancements | ||
| - Additional event types (votes, reactions, etc.) | ||
| - Persistent rate limiting with Redis | ||
| - Health check endpoint | ||
| - Metrics and monitoring | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| # Discord Bot Environment Variables | ||
|
|
||
| # Discord Bot Configuration | ||
| DISCORD_BOT_TOKEN=your_bot_token_here | ||
| DISCORD_APP_ID=your_app_id_here | ||
| DISCORD_GUILD_ID=your_guild_id_here | ||
|
|
||
| # Database Configuration | ||
| DATABASE_URL=postgresql://guild_user:guild_password@localhost:5433/guild_genesis | ||
|
|
||
| # Bot Behavior | ||
| POINTS_PER_MESSAGE=1 | ||
| LOG_LEVEL=info | ||
|
|
||
| # Optional: Rate Limiting | ||
| MAX_MESSAGES_PER_MINUTE=10 | ||
| # Discord Bot Environment Variables | ||
| # Discord Bot Configuration | ||
| DISCORD_BOT_TOKEN=your_bot_token_here | ||
| DISCORD_APP_ID=your_app_id_here | ||
| DISCORD_GUILD_ID=your_guild_id_here | ||
| # Database Configuration | ||
| DATABASE_URL=postgresql://guild_user:guild_password@localhost:5433/guild_genesis | ||
| # Bot Behavior | ||
| POINTS_PER_MESSAGE=1 | ||
| LOG_LEVEL=info | ||
| # Optional: Rate Limiting | ||
| MAX_MESSAGES_PER_MINUTE=10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,27 @@ | ||
| -- Create activity_events table for Discord bot | ||
| -- This table stores activity points awarded to users for various events | ||
|
|
||
| CREATE TABLE IF NOT EXISTS activity_events ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| user_id TEXT NOT NULL, | ||
| user_name TEXT NOT NULL, | ||
| amount INTEGER NOT NULL DEFAULT 1, | ||
| event_type TEXT NOT NULL CHECK (event_type IN ('message')), | ||
| date TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
| processed_status BOOLEAN NOT NULL DEFAULT FALSE, | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
| ); | ||
|
|
||
| -- Create indexes for better query performance | ||
| CREATE INDEX IF NOT EXISTS idx_activity_events_user_id ON activity_events(user_id); | ||
| CREATE INDEX IF NOT EXISTS idx_activity_events_processed ON activity_events(processed_status); | ||
| CREATE INDEX IF NOT EXISTS idx_activity_events_date ON activity_events(date); | ||
| CREATE INDEX IF NOT EXISTS idx_activity_events_event_type ON activity_events(event_type); | ||
|
|
||
| -- Add comment for documentation | ||
| COMMENT ON TABLE activity_events IS 'Stores activity points awarded to Discord users for various events'; | ||
| COMMENT ON COLUMN activity_events.user_id IS 'Discord user ID (stable identifier)'; | ||
| COMMENT ON COLUMN activity_events.user_name IS 'Discord username (may change over time)'; | ||
| COMMENT ON COLUMN activity_events.amount IS 'Number of points awarded for this event'; | ||
| COMMENT ON COLUMN activity_events.event_type IS 'Type of event that triggered the points (message, vote, etc.)'; | ||
| COMMENT ON COLUMN activity_events.processed_status IS 'Whether this event has been processed for token minting'; | ||
| -- Create activity_events table for Discord bot | ||
| -- This table stores activity points awarded to users for various events | ||
| CREATE TABLE IF NOT EXISTS activity_events ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| user_id TEXT NOT NULL, | ||
| user_name TEXT NOT NULL, | ||
| amount INTEGER NOT NULL DEFAULT 1, | ||
| event_type TEXT NOT NULL CHECK (event_type IN ('message')), | ||
| date TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
| processed_status BOOLEAN NOT NULL DEFAULT FALSE, | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
| ); | ||
| -- Create indexes for better query performance | ||
| CREATE INDEX IF NOT EXISTS idx_activity_events_user_id ON activity_events(user_id); | ||
| CREATE INDEX IF NOT EXISTS idx_activity_events_processed ON activity_events(processed_status); | ||
| CREATE INDEX IF NOT EXISTS idx_activity_events_date ON activity_events(date); | ||
| CREATE INDEX IF NOT EXISTS idx_activity_events_event_type ON activity_events(event_type); | ||
| -- Add comment for documentation | ||
| COMMENT ON TABLE activity_events IS 'Stores activity points awarded to Discord users for various events'; | ||
| COMMENT ON COLUMN activity_events.user_id IS 'Discord user ID (stable identifier)'; | ||
| COMMENT ON COLUMN activity_events.user_name IS 'Discord username (may change over time)'; | ||
| COMMENT ON COLUMN activity_events.amount IS 'Number of points awarded for this event'; | ||
| COMMENT ON COLUMN activity_events.event_type IS 'Type of event that triggered the points (message, vote, etc.)'; | ||
| COMMENT ON COLUMN activity_events.processed_status IS 'Whether this event has been processed for token minting'; |
7 changes: 7 additions & 0 deletions
7
discord-bot/migrations/002_add_guild_id_to_activity_events.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ALTER TABLE activity_events | ||
| ADD COLUMN IF NOT EXISTS guild_id TEXT; | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_activity_events_guild_id ON activity_events(guild_id); | ||
| CREATE INDEX IF NOT EXISTS idx_activity_events_guild_user ON activity_events(guild_id, user_id); | ||
|
|
||
| COMMENT ON COLUMN activity_events.guild_id IS 'Discord guild (server) ID where the activity occurred'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this should be updated