|
| 1 | +# Discord XP & Leveling Bot |
| 2 | + |
| 3 | +A feature-rich Discord bot built with TypeScript and Discord.js v14, featuring an advanced XP/leveling system with role rewards, customizable rank cards, and comprehensive guild management. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🎮 **XP System**: Earn XP by sending messages with configurable gains and cooldowns |
| 8 | +- 📊 **Rank Cards**: Beautiful, canvas-generated rank cards showing level progress |
| 9 | +- 🏆 **Leaderboards**: Server-wide leaderboards to see top members |
| 10 | +- 🎁 **Role Rewards**: Automatic role assignment at specific levels |
| 11 | +- 🎨 **Customizable**: Configure XP rates, messages, and notification channels per guild |
| 12 | +- 📈 **Progress Tracking**: Detailed statistics and XP logs |
| 13 | +- 🏷️ **Smart Nicknames**: Auto-update nicknames with highest earned role |
| 14 | +- 💾 **Database Persistence**: All data stored in Supabase (PostgreSQL) |
| 15 | + |
| 16 | +## Commands |
| 17 | + |
| 18 | +| Command | Description | |
| 19 | +|---------|-------------| |
| 20 | +| `/rank [user]` | View your or another user's rank card | |
| 21 | +| `/leaderboard` | Display the server leaderboard | |
| 22 | +| `/configxp` | Configure XP system settings (Admin only) | |
| 23 | +| `/rewards` | Manage level-based role rewards (Admin only) | |
| 24 | +| `/resetxp <user>` | Reset a user's XP (Admin only) | |
| 25 | +| `/levelupmessage` | Customize level-up messages (Admin only) | |
| 26 | +| `/badges` | View available badges and achievements | |
| 27 | +| `/help` | Display help information | |
| 28 | + |
| 29 | +## Prerequisites |
| 30 | + |
| 31 | +- Node.js 18.x or higher |
| 32 | +- pnpm (or npm/yarn) |
| 33 | +- Discord Bot Token |
| 34 | +- Supabase account with PostgreSQL database |
| 35 | + |
| 36 | +## Installation |
| 37 | + |
| 38 | +### 1. Clone the Repository |
| 39 | + |
| 40 | +```bash |
| 41 | +git clone <repository-url> |
| 42 | +cd apps/bot |
| 43 | +``` |
| 44 | + |
| 45 | +### 2. Install Dependencies |
| 46 | + |
| 47 | +```bash |
| 48 | +pnpm install |
| 49 | +``` |
| 50 | + |
| 51 | +### 3. Configure Environment Variables |
| 52 | + |
| 53 | +Create a `.env` file in the `apps/bot` directory: |
| 54 | + |
| 55 | +```bash |
| 56 | +cp .env.example .env |
| 57 | +``` |
| 58 | + |
| 59 | +Edit `.env` and add your credentials: |
| 60 | + |
| 61 | +```env |
| 62 | +# Discord Configuration |
| 63 | +DISCORD_TOKEN=your_discord_bot_token_here |
| 64 | +DISCORD_CLIENT_ID=your_discord_application_id_here |
| 65 | +
|
| 66 | +# Supabase Configuration |
| 67 | +SUPABASE_URL=https://your-project.supabase.co |
| 68 | +SUPABASE_SERVICE_ROLE_KEY=your_service_role_key_here |
| 69 | +``` |
| 70 | + |
| 71 | +### 4. Database Setup |
| 72 | + |
| 73 | +#### Create the following tables in your Supabase database: |
| 74 | + |
| 75 | +```sql |
| 76 | +-- Users table |
| 77 | +CREATE TABLE users_discord ( |
| 78 | + id TEXT PRIMARY KEY, |
| 79 | + username TEXT NOT NULL, |
| 80 | + display_name TEXT, |
| 81 | + avatar_url TEXT, |
| 82 | + discriminator TEXT, |
| 83 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 84 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() |
| 85 | +); |
| 86 | + |
| 87 | +-- Guilds table |
| 88 | +CREATE TABLE guilds_discord ( |
| 89 | + id TEXT PRIMARY KEY, |
| 90 | + name TEXT NOT NULL, |
| 91 | + icon_url TEXT, |
| 92 | + owner_id TEXT, |
| 93 | + active BOOLEAN DEFAULT TRUE, |
| 94 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 95 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() |
| 96 | +); |
| 97 | + |
| 98 | +-- User stats per guild |
| 99 | +CREATE TABLE user_stats_discord ( |
| 100 | + id BIGSERIAL PRIMARY KEY, |
| 101 | + guild_id TEXT NOT NULL REFERENCES guilds_discord(id) ON DELETE CASCADE, |
| 102 | + user_id TEXT NOT NULL REFERENCES users_discord(id) ON DELETE CASCADE, |
| 103 | + xp INTEGER DEFAULT 0, |
| 104 | + level INTEGER DEFAULT 0, |
| 105 | + total_messages INTEGER DEFAULT 0, |
| 106 | + last_message_at TIMESTAMP WITH TIME ZONE, |
| 107 | + active BOOLEAN DEFAULT TRUE, |
| 108 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 109 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 110 | + UNIQUE(guild_id, user_id) |
| 111 | +); |
| 112 | + |
| 113 | +-- XP configuration per guild |
| 114 | +CREATE TABLE xp_config_discord ( |
| 115 | + id BIGSERIAL PRIMARY KEY, |
| 116 | + guild_id TEXT NOT NULL UNIQUE REFERENCES guilds_discord(id) ON DELETE CASCADE, |
| 117 | + min_xp INTEGER DEFAULT 5, |
| 118 | + max_xp INTEGER DEFAULT 15, |
| 119 | + cooldown INTEGER DEFAULT 60, |
| 120 | + levelup_channel TEXT, |
| 121 | + levelup_message TEXT, |
| 122 | + xp_gain_channel TEXT, |
| 123 | + xp_gain_message TEXT, |
| 124 | + notify_on_xp_gain BOOLEAN DEFAULT FALSE, |
| 125 | + reward_notification_channel TEXT, |
| 126 | + reward_notification_message TEXT, |
| 127 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 128 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() |
| 129 | +); |
| 130 | + |
| 131 | +-- Level rewards (roles, badges, etc.) |
| 132 | +CREATE TABLE level_rewards_discord ( |
| 133 | + id BIGSERIAL PRIMARY KEY, |
| 134 | + guild_id TEXT NOT NULL REFERENCES guilds_discord(id) ON DELETE CASCADE, |
| 135 | + level INTEGER NOT NULL, |
| 136 | + reward_type TEXT NOT NULL, -- 'role', 'badge', etc. |
| 137 | + reward_value TEXT NOT NULL, -- role ID, badge name, etc. |
| 138 | + description TEXT, |
| 139 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 140 | + UNIQUE(guild_id, level, reward_value) |
| 141 | +); |
| 142 | + |
| 143 | +-- User rewards tracking |
| 144 | +CREATE TABLE user_rewards_discord ( |
| 145 | + id BIGSERIAL PRIMARY KEY, |
| 146 | + guild_id TEXT NOT NULL REFERENCES guilds_discord(id) ON DELETE CASCADE, |
| 147 | + user_id TEXT NOT NULL REFERENCES users_discord(id) ON DELETE CASCADE, |
| 148 | + reward_id BIGINT NOT NULL REFERENCES level_rewards_discord(id) ON DELETE CASCADE, |
| 149 | + level_earned INTEGER NOT NULL, |
| 150 | + granted_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 151 | + UNIQUE(guild_id, user_id, reward_id) |
| 152 | +); |
| 153 | + |
| 154 | +-- XP logs for auditing |
| 155 | +CREATE TABLE xp_logs_discord ( |
| 156 | + id BIGSERIAL PRIMARY KEY, |
| 157 | + guild_id TEXT NOT NULL REFERENCES guilds_discord(id) ON DELETE CASCADE, |
| 158 | + user_id TEXT NOT NULL REFERENCES users_discord(id) ON DELETE CASCADE, |
| 159 | + message_id TEXT, |
| 160 | + xp_earned INTEGER NOT NULL, |
| 161 | + reason TEXT, |
| 162 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() |
| 163 | +); |
| 164 | + |
| 165 | +-- Indexes for performance |
| 166 | +CREATE INDEX idx_user_stats_guild ON user_stats_discord(guild_id); |
| 167 | +CREATE INDEX idx_user_stats_user ON user_stats_discord(user_id); |
| 168 | +CREATE INDEX idx_user_stats_level ON user_stats_discord(level DESC, xp DESC); |
| 169 | +CREATE INDEX idx_level_rewards_guild ON level_rewards_discord(guild_id); |
| 170 | +CREATE INDEX idx_xp_logs_guild ON xp_logs_discord(guild_id); |
| 171 | +``` |
| 172 | + |
| 173 | +### 5. Discord Bot Setup |
| 174 | + |
| 175 | +1. Go to [Discord Developer Portal](https://discord.com/developers/applications) |
| 176 | +2. Create a new application |
| 177 | +3. Go to "Bot" section and create a bot |
| 178 | +4. Copy the bot token and add to `.env` |
| 179 | +5. Enable the following **Privileged Gateway Intents**: |
| 180 | + - Server Members Intent |
| 181 | + - Message Content Intent |
| 182 | +6. Go to "OAuth2 > URL Generator" |
| 183 | +7. Select scopes: `bot`, `applications.commands` |
| 184 | +8. Select bot permissions: |
| 185 | + - Read Messages/View Channels |
| 186 | + - Send Messages |
| 187 | + - Manage Roles |
| 188 | + - Manage Nicknames |
| 189 | +9. Copy the generated URL and invite the bot to your server |
| 190 | + |
| 191 | +## Development |
| 192 | + |
| 193 | +### Run in Development Mode |
| 194 | + |
| 195 | +```bash |
| 196 | +pnpm dev |
| 197 | +``` |
| 198 | + |
| 199 | +### Build for Production |
| 200 | + |
| 201 | +```bash |
| 202 | +pnpm build |
| 203 | +``` |
| 204 | + |
| 205 | +### Start Production Build |
| 206 | + |
| 207 | +```bash |
| 208 | +pnpm start |
| 209 | +``` |
| 210 | + |
| 211 | +## Configuration |
| 212 | + |
| 213 | +### XP System |
| 214 | + |
| 215 | +Configure XP gains per guild using `/configxp`: |
| 216 | +- **Min/Max XP**: XP range earned per message (default: 5-15) |
| 217 | +- **Cooldown**: Seconds between XP gains per user (default: 60s) |
| 218 | +- **Level-up Channel**: Where to send level-up notifications |
| 219 | +- **XP Gain Channel**: Where to send XP gain notifications (optional) |
| 220 | + |
| 221 | +### Custom Messages |
| 222 | + |
| 223 | +Use placeholders in your custom messages: |
| 224 | +- `{user}` - Mentions the user |
| 225 | +- `{level}` - Current level |
| 226 | +- `{xp}` - XP gained |
| 227 | +- `{total_xp}` - Total XP |
| 228 | +- `{roles}` - Earned roles (for reward notifications) |
| 229 | + |
| 230 | +Example: |
| 231 | +``` |
| 232 | +🎉 Congratulations {user}! You've reached **Level {level}**! |
| 233 | +``` |
| 234 | + |
| 235 | +### Role Rewards |
| 236 | + |
| 237 | +Set up automatic role rewards using `/rewards`: |
| 238 | +1. Choose a level threshold |
| 239 | +2. Select a role to grant |
| 240 | +3. Bot will automatically assign the role when users reach that level |
| 241 | + |
| 242 | +## Project Structure |
| 243 | + |
| 244 | +``` |
| 245 | +apps/bot/ |
| 246 | +├── commands/ # Slash command handlers |
| 247 | +│ ├── badges.ts |
| 248 | +│ ├── configXP.ts |
| 249 | +│ ├── help.ts |
| 250 | +│ ├── leaderboard.ts |
| 251 | +│ ├── levelUpMessage.ts |
| 252 | +│ ├── rankCard.ts |
| 253 | +│ ├── resetXP.ts |
| 254 | +│ └── rewards.ts |
| 255 | +├── events/ # Discord event handlers |
| 256 | +│ ├── autoRoleAssignment.ts |
| 257 | +│ ├── guildCreate.ts |
| 258 | +│ ├── guildDelete.ts |
| 259 | +│ ├── guildMemberRemove.ts |
| 260 | +│ └── messageCreate.ts |
| 261 | +├── utils/ # Utility functions |
| 262 | +│ ├── configStore.ts |
| 263 | +│ ├── rankCardGen.ts # Canvas rank card generation |
| 264 | +│ ├── supabase.bot.ts # Database client |
| 265 | +│ ├── xpConstants.ts # XP calculation constants |
| 266 | +│ ├── xpHelper.ts # XP calculation helpers |
| 267 | +│ └── xpSystem.ts # Core XP logic |
| 268 | +├── constants/ # Constants and configurations |
| 269 | +├── types/ # TypeScript type definitions |
| 270 | +├── index.ts # Bot entry point |
| 271 | +├── package.json |
| 272 | +└── tsconfig.json |
| 273 | +``` |
| 274 | + |
| 275 | +## Troubleshooting |
| 276 | + |
| 277 | +### Bot not responding to commands |
| 278 | +- Verify bot has proper permissions in your Discord server |
| 279 | +- Check that application commands are registered (happens on bot startup) |
| 280 | +- Ensure Message Content Intent is enabled |
| 281 | + |
| 282 | +### XP not being awarded |
| 283 | +- Check cooldown settings (default 60 seconds) |
| 284 | +- Verify database connection in bot logs |
| 285 | +- Ensure user has proper permissions to send messages |
| 286 | + |
| 287 | +### Role rewards not working |
| 288 | +- Verify bot role is higher than the roles it's trying to assign |
| 289 | +- Check Manage Roles permission is enabled |
| 290 | +- Review bot logs for permission errors |
| 291 | + |
| 292 | +## Security Notes |
| 293 | + |
| 294 | +⚠️ **Never commit `.env` file to version control** |
| 295 | + |
| 296 | +The bot uses Supabase Service Role key which has full database access. Ensure: |
| 297 | +- Environment variables are properly secured |
| 298 | +- Bot is hosted in a secure environment |
| 299 | +- Regular security updates are applied |
| 300 | + |
| 301 | +## Contributing |
| 302 | + |
| 303 | +Contributions are welcome! Please follow these guidelines: |
| 304 | +1. Create a feature branch |
| 305 | +2. Make your changes |
| 306 | +3. Test thoroughly |
| 307 | +4. Submit a pull request |
| 308 | + |
| 309 | +## License |
| 310 | + |
| 311 | +[Your License Here] |
| 312 | + |
| 313 | +## Support |
| 314 | + |
| 315 | +For issues or questions: |
| 316 | +- Open an issue on GitHub |
| 317 | +- Contact the development team |
| 318 | + |
| 319 | +--- |
| 320 | + |
| 321 | +Built with ❤️ using Discord.js v14 and TypeScript |
0 commit comments