Skip to content

feat: Add balance and leaderboard Discord slash commands#92

Closed
anumukul wants to merge 3 commits intoTheSoftwareDevGuild:mainfrom
anumukul:feat/discord-balance-leaderboard
Closed

feat: Add balance and leaderboard Discord slash commands#92
anumukul wants to merge 3 commits intoTheSoftwareDevGuild:mainfrom
anumukul:feat/discord-balance-leaderboard

Conversation

@anumukul
Copy link

@anumukul anumukul commented Oct 4, 2025

Description

Implements /balance and /leaderboard slash commands for Discord bot activity tracking as specified in #89.
Implementation
Commands:

/balance [user] - Display activity points for user (ephemeral)
/leaderboard [limit] - Show top members by points (public, limit: 5-25, default: 10)

Database:

Added guild_id column to activity_events table
Implemented getUserBalance(guildId, userId) query
Implemented getLeaderboard(guildId, limit) query with aggregation
Created indexes for performance optimization

Features:

Slash command registration on bot startup
Rate limiting: 10 requests/minute per user
Guild-only command validation
Member state handling (left members marked with "(left)")
Number formatting with thousands separators
Comprehensive error handling and logging

Architecture:

src/commands/balance.ts - Balance command implementation
src/commands/leaderboard.ts - Leaderboard command with embed formatting
src/commands/index.ts - Command registration and collection
src/utils/rateLimiter.ts - Rate limiting utility class
migrations/002_add_guild_id_to_activity_events.sql - Database schema update

Testing
All acceptance criteria verified:

Balance command returns correct totals (ephemeral)
Leaderboard displays ranked members with proper ordering
Input validation enforces limit constraints (5-25)
Rate limiting prevents spam
Error states handled appropriately
Edge cases covered (left members, empty states, long names)

Files Changed
discord-bot/
├── migrations/002_add_guild_id_to_activity_events.sql (new)
├── src/commands/
│ ├── balance.ts (new)
│ ├── leaderboard.ts (new)
│ └── index.ts (new)
├── src/utils/rateLimiter.ts (new)
├── src/db.ts (modified)
├── src/index.ts (modified)
└── src/listeners/messageCreate.ts (modified)
Closes #89

- Implement /balance command with optional user parameter
- Implement /leaderboard command with configurable limit (5-25, default 10)
- Add getUserBalance and getLeaderboard database queries
- Create command rate limiter (10 requests/min per user)
- Add guild_id column to activity_events table
- Register slash commands on bot startup
- Handle member left states in leaderboard display
- Format numbers with thousands separators
- Include comprehensive error handling

Closes TheSoftwareDevGuild#89
Copy link
Contributor

@joelamouche joelamouche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay looks great! Tested in our discord and it works.
At this points it just needs a few updates in the readme and maybe use a tool for db migrations

docker-compose up postgres -d
# Run migration
psql postgresql://guild_user:guild_password@localhost:5433/guild_genesis -f migrations/001_create_activity_events.sql
Copy link
Contributor

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

## Database Schema
The bot creates an `activity_events` table with the following structure:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and guild_id should be added here

- `processed_status`: Whether the event has been processed for minting
- `created_at`: Record creation timestamp
## Architecture
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new folder here

"dev": "npm run build && node -r dotenv/config dist/index.js",
"build": "tsc",
"start": "node -r dotenv/config dist/index.js",
"migrate": "psql $DATABASE_URL -f migrations/001_create_activity_events.sql",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this should be updated too. Maybe we could use a package to make it cleaner

@joelamouche
Copy link
Contributor

close due to inactivity

@joelamouche joelamouche closed this Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discord Bot: add a command to ask for balances

2 participants

Comments