|
| 1 | +# Standard imports |
| 2 | +import datetime |
| 3 | + |
| 4 | +# import subprocess |
| 5 | +import sys |
| 6 | +from typing import cast |
| 7 | + |
| 8 | +# Third-party imports |
| 9 | +import discord |
| 10 | +from discord.ext import commands |
| 11 | + |
| 12 | + |
| 13 | +class VersionInfo(commands.Cog): |
| 14 | + _SCAICT_UWU_VERSION_NUMBER: str = "0.2.0.dev0" |
| 15 | + """ |
| 16 | + Current hardcoded workaround |
| 17 | + """ |
| 18 | + |
| 19 | + _SCAICT_UWU_VERSION_DATE: str = "2025-11-03" |
| 20 | + """ |
| 21 | + Current hardcoded workaround |
| 22 | + """ |
| 23 | + |
| 24 | + _SCAICT_UWU_VERSION: str = ( |
| 25 | + f"{_SCAICT_UWU_VERSION_NUMBER}\n{_SCAICT_UWU_VERSION_DATE}" |
| 26 | + ) |
| 27 | + """ |
| 28 | + Current hardcoded workaround |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, bot: discord.Bot) -> None: |
| 32 | + self.bot = bot |
| 33 | + |
| 34 | + def embeds_version_info(self) -> list[discord.Embed]: |
| 35 | + """ |
| 36 | + List of version information embeds. |
| 37 | + """ |
| 38 | + |
| 39 | + return [ |
| 40 | + self.embed_version_info_title(), |
| 41 | + self.embed_installed_software(), |
| 42 | + # self.embed_installed_packages(), |
| 43 | + self.embed_version_info_footer(), |
| 44 | + ] |
| 45 | + |
| 46 | + def embed_version_info_title(self) -> discord.Embed: |
| 47 | + """ |
| 48 | + Main title embed of the version information embeds. |
| 49 | + """ |
| 50 | + |
| 51 | + bot_avatar = self.bot.user.avatar |
| 52 | + |
| 53 | + if bot_avatar is None: |
| 54 | + bot_avatar = self.bot.user.default_avatar |
| 55 | + |
| 56 | + return discord.Embed( |
| 57 | + color=0xFF24CF, title=f"版本資訊", thumbnail=bot_avatar.url |
| 58 | + ) |
| 59 | + |
| 60 | + def embed_installed_software(self) -> discord.Embed: |
| 61 | + """ |
| 62 | + == Installed software == |
| 63 | +
|
| 64 | + * scaict_uwu: version_num (git_hash_to-do)\\ndate |
| 65 | + * python |
| 66 | + * pip |
| 67 | + * mysql |
| 68 | + """ |
| 69 | + |
| 70 | + embed = discord.Embed( |
| 71 | + color=0xFF24CF, |
| 72 | + title=f"已安裝的軟體", |
| 73 | + ) |
| 74 | + |
| 75 | + # TODO: Git hash |
| 76 | + # process = subprocess.Popen( |
| 77 | + # ['git', 'rev-parse', 'HEAD'], |
| 78 | + # shell=False, |
| 79 | + # stdout=subprocess.PIPE |
| 80 | + # ) |
| 81 | + # git_head_hash = process.communicate()[0].strip() |
| 82 | + |
| 83 | + embed.add_field( |
| 84 | + name="中電喵", |
| 85 | + value=self._SCAICT_UWU_VERSION, |
| 86 | + inline=False, |
| 87 | + ) |
| 88 | + embed.add_field( |
| 89 | + name="Python", |
| 90 | + value=sys.version, |
| 91 | + inline=False, |
| 92 | + ) |
| 93 | + |
| 94 | + return embed |
| 95 | + |
| 96 | + # def embed_installed_packages(self) -> discord.Embed: |
| 97 | + # """ |
| 98 | + # == Installed packages == |
| 99 | + # py-cord |
| 100 | + # ... |
| 101 | + # """ |
| 102 | + |
| 103 | + # embed = discord.Embed( |
| 104 | + # color=0xFF24CF, |
| 105 | + # title=f"已安裝的套件", |
| 106 | + # ) |
| 107 | + |
| 108 | + # embed.add_field( |
| 109 | + # name="package_name".lower, |
| 110 | + # value="package_version", |
| 111 | + # inline=True, |
| 112 | + # ) |
| 113 | + |
| 114 | + # return embed |
| 115 | + |
| 116 | + def embed_version_info_footer(self) -> discord.Embed: |
| 117 | + """ |
| 118 | + Main footer of the version information embeds. |
| 119 | + """ |
| 120 | + |
| 121 | + return discord.Embed( |
| 122 | + color=0xFF24CF, |
| 123 | + footer=discord.EmbedFooter( |
| 124 | + text=( |
| 125 | + datetime.datetime.now(tz=datetime.timezone.utc).strftime( |
| 126 | + "%Y-%m-%d %H:%M:%S (UTC)" |
| 127 | + ) |
| 128 | + ) |
| 129 | + ), |
| 130 | + ) |
| 131 | + |
| 132 | + @discord.slash_command(name="version_info", description="版本資訊") |
| 133 | + async def version_info(self, interaction) -> None: |
| 134 | + interaction = cast(discord.Interaction, interaction) |
| 135 | + |
| 136 | + assert ( |
| 137 | + interaction.user |
| 138 | + ), "Interaction may be in PING interactions, so that interaction.user is invalid." |
| 139 | + assert interaction.channel, "There are no channel returned from interation." |
| 140 | + |
| 141 | + await interaction.response.send_message(embeds=self.embed_version_info()) |
| 142 | + |
| 143 | + |
| 144 | +def setup(bot: discord.Bot): |
| 145 | + bot.add_cog(VersionInfo(bot)) |
0 commit comments