Skip to content
GitHub

bindTheme

function in meocord/common Since 4.1.0

bindTheme<F extends (...args: any[]) => unknown>(fn: F): F

Makes a function run in the handler's theme, rebuilt for the server and user each call's arguments name.

Wrap a listener a handler registers in it, such as a collector's collect callback or a client.on(...) handler. Such a listener runs in its emitter's context, so without it a useTheme() inside reads the theme outside any call. Timers and promises the handler starts keep its theme without it.

Each call's theme is built as a handler's call is: the app's theme, the @UseTheme layers of the handler that bound it, then @MeoCord's themeFor for the server and the user its arguments name, as an interaction, a message, a reaction, a member, a channel or the server itself does. A value's creator, such as a sticker's or a thread's, never names the user: the function is told no event, so nothing says the value was just created. Every member who clicks a collector's button is answered in their own server's and user's theme. A theme themeFor is still looking up replaces the handler's once found, so respond() waits for it and useTheme() reads the handler's until then. A call whose arguments name neither a server nor a user, such as a collector's end, runs in the whole theme of the call that bound it.

Parameters

NameType
fnF

The function to run in the handler's theme; this and its arguments are passed through.

Returns

F

A function that runs fn in that theme, and returns what fn returns.

Examples

TypeScript
@MessageHandler('poll')
async poll(message: Message) {
  const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button, time: 60_000 })
  collector.on('collect', bindTheme(async (click: ButtonInteraction) => {
    await respond(click).send({ embeds: [{ description: 'Picked' }] }) // the clicker's theme, @UseTheme included
  }))
}