Update API endpoint for metrics and add MetricsDisplayCog for fetching live bot metrics

This commit is contained in:
pancakes-proxy 2025-05-08 03:52:46 +09:00
parent e92b652036
commit a0d15b931c
2 changed files with 59 additions and 1 deletions

View File

@ -41,7 +41,7 @@ class ApiPushMetricsCog(commands.Cog):
return
# The API endpoint URL (adjust if needed).
url = "https://api.learnhelp.cc/discord/botmetrics.json/"
url = "https://learnhelpapi.onrender.com/discord/botmetrics.json/"
headers = {
"X-API-Key": lh_api_key

58
cogs/metrics.py Normal file
View File

@ -0,0 +1,58 @@
import discord
from discord.ext import commands
from discord import app_commands
import aiohttp
class MetricsDisplayCog(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
@app_commands.command(
name="metrics",
description="Displays live bot metrics fetched from the API."
)
async def metrics(self, interaction: discord.Interaction) -> None:
# URL of your metrics API endpoint.
url = "https://api.learnhelp.cc/discord/botmetrics.json/"
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 200:
data = await response.json()
# Extract bot metrics from the API response.
uptime = data.get("uptime", "N/A")
server_count = data.get("server_count", "N/A")
latency = data.get("latency", "N/A")
timestamp = data.get("timestamp", "N/A")
# Format command usage statistics.
command_usage = data.get("command_usage", {})
if command_usage:
usage_lines = "\n".join(
f"**{cmd}**: {count}" for cmd, count in command_usage.items()
)
else:
usage_lines = "N/A"
# Create the embed.
embed = discord.Embed(
title="Live Bot Metrics",
description="These metrics are fetched directly from the API.",
color=discord.Color.green()
)
embed.add_field(name="Uptime", value=uptime, inline=True)
embed.add_field(name="Server Count", value=str(server_count), inline=True)
embed.add_field(name="Latency", value=latency, inline=True)
embed.add_field(name="Command Usage", value=usage_lines, inline=False)
embed.set_footer(text=f"Last Updated: {timestamp}")
await interaction.response.send_message(embed=embed)
else:
await interaction.response.send_message(
f"Failed to retrieve bot metrics. API returned status: {response.status}",
ephemeral=True
)
async def setup(bot: commands.Bot) -> None:
await bot.add_cog(MetricsDisplayCog(bot))