Skip to content

PyDrocsid.command_edit

handle_delete async

handle_delete(bot: Bot, channel_id: int, message_id: int) -> None

Delete linked bot responses of a command message.

Source code in PyDrocsid/command_edit.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
async def handle_delete(bot: Bot, channel_id: int, message_id: int) -> None:
    """Delete linked bot responses of a command message."""

    responses = await redis.lrange(key := f"bot_response:channel={channel_id},msg={message_id}", 0, -1)
    await redis.delete(key)

    async def delete_message(chn_id: int, msg_id: int) -> None:
        if not isinstance(chn := await _get_channel(bot, chn_id), (TextChannel, Thread, DMChannel)):
            logger.warning("could not delete message %s in unknown channel %s", msg_id, chn_id)
            return

        try:
            message: Message = await chn.fetch_message(int(msg_id))
            await message.delete()
        except (NotFound, Forbidden, HTTPException):
            logger.warning("could not delete message %s in #%s (%s)", msg_id, chn.name, chn.id)

    await asyncio.gather(*[delete_message(*map(int, r.split(":"))) for r in responses])

handle_edit async

handle_edit(bot: Bot, message: Message) -> None

Delete linked bot responses of a command message and execute new command.

Source code in PyDrocsid/command_edit.py
34
35
36
37
38
39
40
41
42
43
async def handle_edit(bot: Bot, message: Message) -> None:
    """Delete linked bot responses of a command message and execute new command."""

    if message.author.bot:
        return

    await handle_delete(bot, message.channel.id, message.id)
    for reaction in message.reactions:
        if reaction.me:
            await reaction.remove(cast(Snowflake, bot.user))
link_response(msg: Message | Context[Bot], *response_messages: Message) -> None

Create a link from message to a given list of bot responses and add it to redis.

Source code in PyDrocsid/command_edit.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
async def link_response(msg: Message | Context[Bot], *response_messages: Message) -> None:
    """Create a link from message to a given list of bot responses and add it to redis."""

    if not response_messages:
        return

    if isinstance(msg, Context):
        msg = msg.message

    # save channel:message pairs in redis
    await redis.lpush(
        key := f"bot_response:channel={msg.channel.id},msg={msg.id}",
        *[f"{msg.channel.id}:{msg.id}" for msg in response_messages],
    )
    await redis.expire(key, RESPONSE_LINK_TTL)