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 theme of the call that binds it, wherever it is called from later.

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.

The theme is the binding call's whole theme, its server's and user's themes included, so every later call, whoever it is for, is answered in the starting member's theme.

Parameters

NameType
fnF

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

Returns

F

A function that runs fn in the theme useTheme() returns where bindTheme is called.

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) => {
    const { emojis } = useTheme() // the starting member's theme, @UseTheme included
    await click.reply(`${emojis.success} Picked`)
  }))
}