|
| 1 | +import datetime |
| 2 | +import itertools |
| 3 | +import platform |
| 4 | + |
| 5 | +import discord |
| 6 | +import psutil |
| 7 | +import pygit2 |
| 8 | +from discord import app_commands |
| 9 | +from discord.ext import commands |
| 10 | +from discord.utils import format_dt |
| 11 | +from libs.utils import Embed, human_timedelta |
| 12 | + |
| 13 | +from rodhaj import Rodhaj |
| 14 | + |
| 15 | + |
| 16 | +# A cog houses a category of commands |
| 17 | +# Unlike djs, think of commands being stored as a category, |
| 18 | +# which the cog is that category |
| 19 | +class Meta(commands.Cog): |
| 20 | + def __init__(self, bot: Rodhaj) -> None: |
| 21 | + self.bot = bot |
| 22 | + self.process = psutil.Process() |
| 23 | + |
| 24 | + def get_bot_uptime(self, *, brief: bool = False) -> str: |
| 25 | + return human_timedelta( |
| 26 | + self.bot.uptime, accuracy=None, brief=brief, suffix=False |
| 27 | + ) |
| 28 | + |
| 29 | + def format_commit(self, commit: pygit2.Commit) -> str: |
| 30 | + short, _, _ = commit.message.partition("\n") |
| 31 | + short_sha2 = commit.hex[0:6] |
| 32 | + commit_tz = datetime.timezone( |
| 33 | + datetime.timedelta(minutes=commit.commit_time_offset) |
| 34 | + ) |
| 35 | + commit_time = datetime.datetime.fromtimestamp(commit.commit_time).astimezone( |
| 36 | + commit_tz |
| 37 | + ) |
| 38 | + |
| 39 | + # [`hash`](url) message (offset) |
| 40 | + offset = format_dt(commit_time.astimezone(datetime.timezone.utc), "R") |
| 41 | + return f"[`{short_sha2}`](https://github.com/transprogrammer/rodhaj/commit/{commit.hex}) {short} ({offset})" |
| 42 | + |
| 43 | + def get_last_commits(self, count: int = 5): |
| 44 | + repo = pygit2.Repository(".git") |
| 45 | + commits = list( |
| 46 | + itertools.islice( |
| 47 | + repo.walk(repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL), count |
| 48 | + ) |
| 49 | + ) |
| 50 | + return "\n".join(self.format_commit(c) for c in commits) |
| 51 | + |
| 52 | + @app_commands.command(name="about") |
| 53 | + async def about(self, interaction: discord.Interaction) -> None: |
| 54 | + """Shows some stats for Rodhaj""" |
| 55 | + total_members = 0 |
| 56 | + total_unique = len(self.bot.users) |
| 57 | + |
| 58 | + for guild in self.bot.guilds: |
| 59 | + total_members += guild.member_count or 0 |
| 60 | + |
| 61 | + # For Kumiko, it's done differently |
| 62 | + # R. Danny's way of doing it is probably close enough anyways |
| 63 | + memory_usage = self.process.memory_full_info().uss / 1024**2 |
| 64 | + cpu_usage = self.process.cpu_percent() / psutil.cpu_count() |
| 65 | + |
| 66 | + revisions = self.get_last_commits() |
| 67 | + embed = Embed() |
| 68 | + embed.set_author(name=self.bot.user.name, icon_url=self.bot.user.display_avatar.url) # type: ignore |
| 69 | + embed.title = "About Me" |
| 70 | + embed.description = f"Latest Changes:\n {revisions}" |
| 71 | + embed.set_footer( |
| 72 | + text=f"Made with discord.py v{discord.__version__}", |
| 73 | + icon_url="https://cdn.discordapp.com/emojis/596577034537402378.png?size=128", |
| 74 | + ) |
| 75 | + embed.add_field(name="Servers Count", value=len(self.bot.guilds)) |
| 76 | + embed.add_field( |
| 77 | + name="User Count", value=f"{total_members} total\n{total_unique} unique" |
| 78 | + ) |
| 79 | + embed.add_field( |
| 80 | + name="Process", value=f"{memory_usage:.2f} MiB\n{cpu_usage:.2f}% CPU" |
| 81 | + ) |
| 82 | + embed.add_field(name="Python Version", value=platform.python_version()) |
| 83 | + embed.add_field(name="Version", value=str(self.bot.version)) |
| 84 | + embed.add_field(name="Uptime", value=self.get_bot_uptime(brief=True)) |
| 85 | + await interaction.response.send_message(embed=embed) |
| 86 | + |
| 87 | + @app_commands.command(name="uptime") |
| 88 | + async def uptime(self, interaction: discord.Interaction) -> None: |
| 89 | + """Displays the bot's uptime""" |
| 90 | + uptime_message = f"Uptime: {self.get_bot_uptime()}" |
| 91 | + await interaction.response.send_message(uptime_message) |
| 92 | + |
| 93 | + @app_commands.command(name="version") |
| 94 | + async def version(self, interaction: discord.Interaction) -> None: |
| 95 | + """Displays the current build version""" |
| 96 | + version_message = f"Version: {self.bot.version}" |
| 97 | + await interaction.response.send_message(version_message) |
| 98 | + |
| 99 | + |
| 100 | +async def setup(bot: Rodhaj) -> None: |
| 101 | + await bot.add_cog(Meta(bot)) |
0 commit comments