From 5ca26e47c1f2d8e01bc5fb6c40a1fb5b73ac081f Mon Sep 17 00:00:00 2001 From: Ashvin Tiwari Date: Mon, 20 Oct 2025 04:35:07 +0530 Subject: [PATCH] Add /recent command to show recent game activity --- commands/README-recent.md | 105 ++++++++++++++++++++++++++++++++++++++ commands/recent.js | 83 ++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 commands/README-recent.md create mode 100644 commands/recent.js diff --git a/commands/README-recent.md b/commands/README-recent.md new file mode 100644 index 0000000..eb9c338 --- /dev/null +++ b/commands/README-recent.md @@ -0,0 +1,105 @@ +# Recent Games Command + +A Discord slash command that displays recent game activity and winners for the Name That Artist bot. + +## Description + +The `/recent` command shows the 10 most recent players who have participated in Name That Artist games, along with their scores, wins, and how long ago they played. + +## Features + +- 📊 Shows last 10 players who played +- 🏆 Displays player scores and win counts +- ⏰ Shows time since last game (e.g., "5m ago", "2h ago") +- 🎨 Beautiful embed with TTC branding +- 🥇 Medal indicators for top 3 recent players +- ⚡ Fast response using cached player data + +## Usage + +Simply type in Discord: +``` +/recent +``` + +The bot will display an embed showing: +- Player username +- Their last score +- Total wins +- Time since they last played + +## Output Example + +``` +🎮 Recent Game Activity + +🥇 Alice + Score: 850 | Wins: 5 | 2m ago + +🥈 Bob + Score: 720 | Wins: 3 | 15m ago + +🥉 Charlie + Score: 690 | Wins: 2 | 1h ago + +4. David + Score: 550 | Wins: 1 | 3h ago +``` + +## Implementation Details + +### Dependencies +- `discord.js` - For slash commands and embeds +- `storage.js` service - Loads player data from local storage + +### Data Source +Reads from `data/players.json` which tracks: +- Player usernames +- Game scores +- Win counts +- Last played timestamps + +### Time Formatting +Automatically formats timestamps to human-readable format: +- Under 1 minute: "30s ago" +- Under 1 hour: "45m ago" +- Under 1 day: "3h ago" +- 1 day or more: "2d ago" + +## Integration + +### Adding to Your Bot + +1. Place this file in the `commands/` directory +2. The command will be auto-loaded by the bot's command handler +3. Run `npm run deploy-commands` to register with Discord +4. Restart the bot + +### Storage Requirements + +Requires the `storage.js` service to track a `lastPlayed` field. Update the storage service to track: + +```javascript +{ + "userId": { + "username": "PlayerName", + "bestScore": 850, + "wins": 5, + "lastPlayed": 1698765432000 // Unix timestamp + } +} +``` + +## Author + +**Ashvin** +- GitHub: [@ashvin2005](https://github.com/ashvin2005) +- LinkedIn: [ashvin-tiwari](https://linkedin.com/in/ashvin-tiwari) + +## Hacktoberfest 2025 + +Created for Hacktoberfest 2025 🎃 + +## License + +MIT License - Same as the Name That Artist project \ No newline at end of file diff --git a/commands/recent.js b/commands/recent.js new file mode 100644 index 0000000..dfd3644 --- /dev/null +++ b/commands/recent.js @@ -0,0 +1,83 @@ +import { SlashCommandBuilder, EmbedBuilder } from 'discord.js'; +import { loadPlayers } from '../services/storage.js'; + +export default { + data: new SlashCommandBuilder() + .setName('recent') + .setDescription('View recent game winners and their scores'), + + async execute(interaction) { + try { + const players = loadPlayers(); + + // Get all player entries with game history + const recentGames = []; + + for (const [userId, playerData] of Object.entries(players)) { + if (playerData.gamesPlayed > 0) { + recentGames.push({ + userId, + username: playerData.username || 'Unknown Player', + lastScore: playerData.bestScore || 0, + wins: playerData.wins || 0, + lastPlayed: playerData.lastPlayed || Date.now() + }); + } + } + + // Sort by most recent play time + recentGames.sort((a, b) => b.lastPlayed - a.lastPlayed); + + // Take top 10 most recent + const topRecent = recentGames.slice(0, 10); + + if (topRecent.length === 0) { + return interaction.reply({ + content: '🎮 No recent games found. Start a new game with `/namethatartist`!', + ephemeral: true + }); + } + + // Create embed + const embed = new EmbedBuilder() + .setColor('#0099ff') + .setTitle('🎮 Recent Game Activity') + .setDescription('Latest players and their performances') + .setTimestamp() + .setFooter({ text: 'TTC Name That Artist' }); + + // Add players to embed + let description = ''; + topRecent.forEach((player, index) => { + const medal = index === 0 ? '🥇' : index === 1 ? '🥈' : index === 2 ? '🥉' : `${index + 1}.`; + const timeAgo = getTimeAgo(player.lastPlayed); + description += `${medal} **${player.username}**\n`; + description += ` Score: ${player.lastScore} | Wins: ${player.wins} | ${timeAgo}\n\n`; + }); + + embed.addFields({ + name: 'Recent Players', + value: description || 'No data available' + }); + + await interaction.reply({ embeds: [embed] }); + + } catch (error) { + console.error('Error in /recent command:', error); + await interaction.reply({ + content: '❌ An error occurred while fetching recent games.', + ephemeral: true + }); + } + } +}; + +// Helper function to format time ago +function getTimeAgo(timestamp) { + const seconds = Math.floor((Date.now() - timestamp) / 1000); + + if (seconds < 60) return `${seconds}s ago`; + if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`; + if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`; + return `${Math.floor(seconds / 86400)}d ago`; +} \ No newline at end of file