Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions commands/README-recent.md
Original file line number Diff line number Diff line change
@@ -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
83 changes: 83 additions & 0 deletions commands/recent.js
Original file line number Diff line number Diff line change
@@ -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`;
}