amatsagu / tempest

Fast and secure functions to build scalable Discord Applications.
BSD 3-Clause "New" or "Revised" License
60 stars 14 forks source link

Introduce plugins (suggestion) #19

Closed amatsagu closed 11 months ago

amatsagu commented 1 year ago

There's idea to make Tempest lib be extendable by writing own, custom logic - aka plugins. Plan is to let anyone write own, reusable piece of code that could be later easily connected with any Tempest Discord Bot.

It will require some changes to current rest handler & client but once done - you'll have access to all client properties and incoming interaction events.

Example of how it could look like:

type Plugin struct {
  Name string
  // Every plugin can expose whatever values they like for public use.
  // For example various statistics from their work.
  // I don't think it can be well typed so it'll have to stay "any". 
  Values map[string]any 
  OnBeforeCommandInteraction func(ctx CommandInteraction) bool // Return `true` to stop execution logic
  OnBeforeComponentInteraction func(ctx ComponentInteraction) bool
  OnBeforeModalInteraction func(ctx ModalInteraction) bool
  OnAfterCommandInteraction func(ctx CommandInteraction)
  OnAfterComponentInteraction func(ctx ComponentInteraction)
  OnAfterModalInteraction func(ctx ModalInteraction)
}

I would like to hear your opinions about it and suggestions how to best implement it.

amatsagu commented 1 year ago

PS: About execution logic. Every time client receives new interaction event - it'll fire "OnBefore" associated function, then depending on result it may stop or move to their regular handler. For example if we define OnBeforeCommandInteraction function and it'll return true - this command will never be realized, bot won't execute that command. All "OnAfter" callbacks will fire after main logic, idea for them is that you can place some cleaning or syncing logic there.

Edit: In case a command/component/modal logic would fail (and most likely result in panic), it will early terminate goroutine so defined "OnAfter" won't be triggered. You can potentially use that to easily check if slash command or modal finished executing properly.

amatsagu commented 11 months ago

Scrapped idea. Instead I've just added pre & post hook handlers for commands + added interfaces to customize used http server & mux.