Skip to content

Commit

Permalink
Add new metrics command to replace >system
Browse files Browse the repository at this point in the history
  • Loading branch information
joinemm committed May 30, 2024
1 parent 7f2ec20 commit 501aa8d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
72 changes: 34 additions & 38 deletions cogs/information.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
# https://git.joinemm.dev/miso-bot

import copy
import math
import os
import time
from typing import Optional

import arrow
import discord
import humanize
import orjson
import psutil
from discord.ext import commands

from modules import emojis, exceptions, util
Expand All @@ -24,6 +22,7 @@ class Information(commands.Cog):
def __init__(self, bot):
self.bot: MisoBot = bot
self.icon = "ℹ️"
self.donator_cache = {}

@commands.command()
async def invite(self, ctx: commands.Context):
Expand Down Expand Up @@ -89,11 +88,16 @@ async def donators(self, ctx: commands.Context):
for user_id in patrons:
user = self.bot.get_user(user_id)
if user is None:
user = await self.bot.fetch_user(user_id)
if user is None or user.name.startswith("Deleted User "):
continue
user = self.donator_cache.get(user_id, 0)
if user == 0:
user = await self.bot.fetch_user(user_id)
if user is None or user.name.startswith("Deleted User "):
self.donator_cache[user_id] = None
continue
self.donator_cache[user_id] = str(user)

donators.append(f"**{user}**")
if user:
donators.append(f"**{user}**")

n = 20
chunks = [
Expand Down Expand Up @@ -172,31 +176,6 @@ async def ping(self, ctx: commands.Context):
)
await test_message.edit(content="", embed=content)

@commands.command(aliases=["status"])
async def system(self, ctx: commands.Context):
"""Get status of the host system"""
process_uptime = time.time() - self.bot.start_time
system_uptime = time.time() - psutil.boot_time()
mem = psutil.virtual_memory()
pid = os.getpid()
memory_use = psutil.Process(pid).memory_info()[0]

data = [
("Bot booted up in", util.stringfromtime(self.bot.boot_up_time)),
("Process uptime", util.stringfromtime(process_uptime, 2)),
("Process memory", f"{memory_use / math.pow(1024, 2):.2f}MB"),
("System uptime", util.stringfromtime(system_uptime, 2)),
("CPU Usage", f"{psutil.cpu_percent()}%"),
("RAM Usage", f"{mem.percent}%"),
]

content = discord.Embed(
title=":computer: System status",
colour=int("5dadec", 16),
description="\n".join(f"**{x[0]}** {x[1]}" for x in data),
)
await ctx.send(embed=content)

@commands.command(aliases=["shards"])
async def shardinfo(self, ctx: commands.Context):
"""Get information about the current shards"""
Expand All @@ -221,13 +200,30 @@ async def shardinfo(self, ctx: commands.Context):
await ctx.send(embed=content)

@commands.command()
async def shardof(self, ctx: commands.Context, guild_id: int):
"""Find the shard ID of given guild ID"""
guild = self.bot.get_guild(guild_id)
if guild is None:
raise exceptions.CommandWarning(f"Guild `{guild_id}` not found")
async def metrics(self, ctx: commands.Context):
"""Show some internal bot metrics"""
content = discord.Embed(
title="Metrics",
colour=int("E46A92", 16),
)
content.timestamp = arrow.now().datetime
uptime = time.time() - self.bot.start_time
prom = self.bot.get_cog("Prometheus")

await ctx.send(f"**{guild}** is on shard `{guild.shard_id}`")
content.description = "\n".join(
[
f"**Ping**: `{int(prom.ping._value.get() * 1000)}ms`",
f"**Uptime**: {humanize.naturaldelta(uptime)}",
f"**Loading time**: {humanize.naturaldelta(self.bot.boot_up_time)}",
f"**Users total**: `{int(prom.users_total._value.get())}`",
f"**Users cached**: `{int(prom.users_cached._value.get())}`",
f"**Guilds total**: `{int(prom.guilds_total._value.get())}`",
f"**Guilds cached**: `{int(prom.guilds_cached._value.get())}`",
f"**Median member count**: `{int(prom.median_member_count._value.get())}`",
]
)

await ctx.send(embed=content)

@commands.command()
async def changelog(self, ctx: commands.Context, author="joinemm", repo="miso-bot"):
Expand Down
14 changes: 13 additions & 1 deletion cogs/owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from discord.ext import commands
from loguru import logger

from modules import util
from modules import exceptions, util
from modules.misobot import MisoBot


Expand All @@ -24,6 +24,15 @@ async def cog_check(self, ctx: commands.Context):
"""Check if command author is Owner"""
return await self.bot.is_owner(ctx.author)

@commands.command()
async def shardof(self, ctx: commands.Context, guild_id: int):
"""Find the shard ID of given guild ID"""
guild = self.bot.get_guild(guild_id)
if guild is None:
raise exceptions.CommandWarning(f"Guild `{guild_id}` not found")

await ctx.send(f"**{guild}** is on shard `{guild.shard_id}`")

@commands.command(rest_is_raw=True)
async def say(
self,
Expand Down Expand Up @@ -94,6 +103,9 @@ async def userguilds(self, ctx: commands.Context, user: discord.User):
f"[`{guild.id}`] **{guild.member_count}** members : **{guild.name}**"
)

if not rows:
exceptions.CommandWarning("User not found in any currently loaded guilds!")

content = discord.Embed(
title=f"User **{user}** found in **{len(rows)}** guilds"
)
Expand Down

0 comments on commit 501aa8d

Please sign in to comment.