|
1 | 1 | import asyncio |
2 | 2 | import io |
| 3 | +import time |
3 | 4 | from pathlib import Path |
4 | 5 |
|
5 | 6 | import discord |
6 | 7 | from discord import option, slash_command |
7 | 8 | from PIL import Image, UnidentifiedImageError |
8 | 9 |
|
| 10 | +from src.exceptions import UnexpectedNoneError |
| 11 | + |
9 | 12 | MAX_IMAGE_FILESIZE = 50_000_000 # 50 MB |
10 | 13 | SUPPORTED_IMAGE_FORMATS = {"jpeg", "png", "gif", "webp", "tiff", "bmp"} |
11 | 14 | TRANSPARENT_FORMATS = {"png", "webp", "tiff"} |
|
14 | 17 | class SlashCommands(discord.Cog, name="slash_commands"): |
15 | 18 | def __init__(self, bot: discord.Bot) -> None: |
16 | 19 | self.bot = bot |
| 20 | + self.started_time = time.time() |
| 21 | + |
| 22 | + @slash_command() |
| 23 | + async def info(self, ctx: discord.ApplicationContext) -> None: |
| 24 | + """Display information about the bot.""" |
| 25 | + if self.bot.user is None: |
| 26 | + raise UnexpectedNoneError(self.bot, "user") |
| 27 | + |
| 28 | + container = discord.ui.Container() |
| 29 | + container.add_text(f""" |
| 30 | +{self.bot.user.name} is a bot developed by [Versa Bots](https://github.com/Versa-Bots/) offering utility commands.""") |
| 31 | + container.add_separator() |
| 32 | + container.add_section( |
| 33 | + discord.ui.TextDisplay( |
| 34 | + f"""**Users:** {len(self.bot.users)} |
| 35 | +**Servers:** {len(self.bot.guilds)} |
| 36 | +**API Latency:** {round(self.bot.latency * 1000)}ms |
| 37 | +**Pycord Version:** {discord.__version__} |
| 38 | +**Uptime:** {self.format_uptime(time.time() - self.started_time)} |
| 39 | +**Code:** https://github.com/Versa-Bots/versa/""" |
| 40 | + ), |
| 41 | + accessory=discord.ui.Thumbnail(url=self.bot.user.display_avatar.url), |
| 42 | + ) |
| 43 | + inv_button_row = discord.ui.ActionRow( |
| 44 | + discord.ui.Button( |
| 45 | + label="Invite", url=f"https://discord.com/api/oauth2/authorize?client_id={self.bot.user.id}" |
| 46 | + ) |
| 47 | + ) |
| 48 | + await ctx.respond(view=discord.ui.DesignerView(container, inv_button_row)) |
17 | 49 |
|
18 | 50 | @slash_command() |
19 | 51 | @option("image", discord.Attachment, description="The image to convert") |
@@ -86,6 +118,24 @@ def convert_image(image_bytes: bytes, target_filetype: str) -> io.BytesIO: |
86 | 118 | buffer.seek(0) |
87 | 119 | return buffer |
88 | 120 |
|
| 121 | + @staticmethod |
| 122 | + def format_uptime(seconds: float) -> str: |
| 123 | + seconds = int(seconds) |
| 124 | + days, seconds = divmod(seconds, 86400) |
| 125 | + hours, seconds = divmod(seconds, 3600) |
| 126 | + minutes, seconds = divmod(seconds, 60) |
| 127 | + |
| 128 | + parts = [] |
| 129 | + if days: |
| 130 | + parts.append(f"{days}d") |
| 131 | + if hours: |
| 132 | + parts.append(f"{hours}h") |
| 133 | + if minutes: |
| 134 | + parts.append(f"{minutes}m") |
| 135 | + parts.append(f"{seconds}s") |
| 136 | + |
| 137 | + return " ".join(parts) |
| 138 | + |
89 | 139 |
|
90 | 140 | def setup(bot: discord.Bot) -> None: |
91 | 141 | bot.add_cog(SlashCommands(bot)) |
0 commit comments