WieeRd / ClockBot

Most interesting yet least useful bot on Discord
Mozilla Public License 2.0
6 stars 1 forks source link

feat: automated webhook management and caching #13

Open WieeRd opened 8 months ago

WieeRd commented 8 months ago

Continuation of the original GMacLak.wsend() method in ClockBot v3.

Tasks

What?

Upon invoking ctx.wsend(content, ...), automatically fetch and cache the webhook, and send a webhook message to the target channel with given arguments.

The goal is to make the task of sending a webhook message as trivial as normal .send() calls.

Why?

ClockBot and its cogs frequently need to send webhook messages in various forms. It's tedious to deal with above every time, and without caching it can take up to a few seconds.

How?

wsend()

  1. Try searching the cache (channel: webhook mapping)
  2. Try fetching existing channel's webhook owned by the bot
  3. Try creating a new webhook
  4. Cache the result, or report failure with ctx.send_error()
  5. Send the webhook message with given arguments

Webhookable

https://discordpy.readthedocs.io/en/stable/search.html?q=channel.webhooks

The goal is to restrict channels that can be used with wsend() through type hints.

@typing.runtime_checkable
class Webhookable(typing.Protocol): ...

# or

Webhookable = ForumChannel | StageChannel | TextChannel | VoiceChannel`

Caching

Simple. It's just dict[Webhookable, Webhook]. But where do I store this, and how do I pass it around?

WieeRd commented 8 months ago

I might make this feature a protocol-mixin (trait at home) like SendExt in #4. To do that, Webhookable (or WebhookExt?) will have to be a Protocol. It's more future-proof to more channels types being added in the future as well.