Add slash command /user_stat to display user statistics

Add a new slash command `/user_stat` to fetch and display user statistics.

* **bot.py**
  - Add a new slash command `/user_stat` to fetch and display the current input token, output token, and model for the user.
  - Retrieve the user's history to calculate the input and output tokens.
  - Fetch the model from the database.
  - Update the `help_command` to include the new `/user_stat` command.

* **README.md**
  - Add documentation for the new `/user_stat` command.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Coder-Vippro/ChatGPT-Discord-Bot?shareId=XXXX-XXXX-XXXX-XXXX).
This commit is contained in:
Vu Quoc Anh
2025-01-07 22:32:44 +07:00
parent 2ea996c365
commit 67e806a901
2 changed files with 24 additions and 1 deletions

23
bot.py
View File

@@ -427,6 +427,27 @@ async def remaining_turns(interaction: discord.Interaction):
await interaction.response.send_message("\n".join(remaining_turns_info), ephemeral=True)
# Slash command for user statistics (/user_stat)
@tree.command(name="user_stat", description="Get your current input token, output token, and model.")
async def user_stat(interaction: discord.Interaction):
"""Fetches and displays the current input token, output token, and model for the user."""
user_id = interaction.user.id
history = get_history(user_id)
model = get_user_model(user_id)
# Calculate input and output tokens
input_tokens = sum(len(str(item['content'])) for item in history if item['role'] == 'user')
output_tokens = sum(len(str(item['content'])) for item in history if item['role'] == 'assistant')
stat_message = (
f"**User Statistics:**\n"
f"Model: `{model}`\n"
f"Input Tokens: `{input_tokens}`\n"
f"Output Tokens: `{output_tokens}`\n"
)
await interaction.response.send_message(stat_message, ephemeral=True)
# Slash command for help (/help)
@tree.command(name="help", description="Display a list of available commands.")
async def help_command(interaction: discord.Interaction):
@@ -439,6 +460,7 @@ async def help_command(interaction: discord.Interaction):
"/generate `<prompt>` - Generate an image from a text prompt.\n"
"/reset - Reset your conversation history.\n"
"/remaining_turns - Check the remaining chat turns for each model.\n"
"/user_stat - Get your current input token, output token, and model.\n"
"/help - Display this help message.\n"
"**Các lệnh có sẵn:**\n"
"/choose_model - Chọn mô hình AI để sử dụng cho phản hồi (gpt-4o, gpt-4o-mini, o1-preview, o1-mini).\n"
@@ -447,6 +469,7 @@ async def help_command(interaction: discord.Interaction):
"/generate `<gợi ý>` - Tạo hình ảnh từ gợi ý văn bản.\n"
"/reset - Đặt lại lịch sử trò chuyện của bạn.\n"
"/remaining_turns - Kiểm tra số lượt trò chuyện còn lại cho mỗi mô hình.\n"
"/user_stat - Nhận thông tin về token đầu vào, token đầu ra và mô hình hiện tại của bạn.\n"
"/help - Hiển thị tin nhắn trợ giúp này.\n"
)
await interaction.response.send_message(help_message, ephemeral=True)