about summary refs log tree commit diff
path: root/src/petthecord/bot.py
blob: 780263fbf0df3e06e9ff7ba7ec56911d812fd6ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from io import BytesIO
from logging import getLogger

from discord import app_commands, File, Interaction, User
from discord.ext import commands

from .defaults import Defaults
from .petter import Petter, HTTPException, NotFound


class PetTheCordCog(commands.Cog):
    def __init__(
        self,

        origin: str = Defaults.Network.ORIGIN,
        petter: Petter | None = None,
    ) -> None:
        self._origin = origin
        self._petter = petter or Petter()
        super().__init__()

        self._logger = getLogger(__name__)

    @app_commands.allowed_installs(users=True)
    @app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
    @app_commands.command(
        name="petpetlink",
        description="Pet some user and take a link",
    )
    @app_commands.describe(
        user="User to pet",
        r="Some random stuff, set it if avatar is not up to date",
    )
    async def petwithlink(
        self,
        interaction: Interaction,

        user: User,
        r: str = "",
    ) -> None:
        self._logger.info(f"Petting {user.id} with link for {interaction.user.id}")
        await interaction.response.send_message(f"{self._origin}/{user.id}.{r}.gif")

    @app_commands.allowed_installs(users=True)
    @app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
    @app_commands.command(
        name="petpet",
        description="Pet some user and take a link"
    )
    @app_commands.describe(
        user="User to pet",
    )
    async def petwithgif(
        self,
        interaction: Interaction,

        user: User,
    ) -> None:
        self._logger.info(f"Petting {user.id} with gif for {interaction.user.id}")
        try:
            await interaction.response.send_message(
                file=File(
                    BytesIO(await self._petter.petpet(user)),
                    filename=f"{user.id}.gif"
                )
            )
        except HTTPException:
            self._logger.error(f"Couldn't pet {user.id}")
            await interaction.response.send_message(
                "Couldn't pet your user :<",
                ephemeral=True,
            )
        except NotFound:
            await interaction.response.send_message(
                "Avatar or user was not found :<",
                ephemeral=True,
            )