mirror of
https://gitlab.com/pancakes1234/wdiscordbotserver.git
synced 2025-06-16 07:14:21 -06:00
Update API endpoint for metrics and add MetricsDisplayCog for fetching live bot metrics
This commit is contained in:
parent
e92b652036
commit
a0d15b931c
@ -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
58
cogs/metrics.py
Normal 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))
|
Loading…
x
Reference in New Issue
Block a user