Skip to main content

MenuContext

The single typed object passed to every menu callback — render methods, lifecycle hooks, button actions, select handlers, and modal submissions all receive ctx.

import type { MenuContext } from '@flowcord/core';

Generics

MenuContext<TState, TSessionState, TOptions>
GenericDefaultDescription
TStateRecord<string, unknown>Menu-local state shape
TSessionStateRecord<string, unknown>Session-wide state shape
TOptionsRecord<string, unknown>Slash command options shape

These are inferred from the MenuBuilder generics — you rarely need to annotate ctx directly.


State

ctx.state

state: StateAccessor<TState>

Menu-scoped state. Created fresh when the menu opens, reset on navigation unless .setPreserveStateOnReturn() is configured.

MethodDescription
ctx.state.get('key')Get a typed value
ctx.state.set('key', value)Set a typed value
ctx.state.merge({ key: value })Shallow-merge a partial state object
ctx.state.reset(newState)Replace the entire state object
ctx.state.currentReadonly snapshot of the full state

See State Management.


ctx.sessionState

sessionState: StateStore<TSessionState>

Session-scoped state. Shared across all menus in the session, persists until the session ends.

MethodDescription
ctx.sessionState.get('key')Get a value (undefined if not set)
ctx.sessionState.set('key', value)Set a value
ctx.sessionState.has('key')Check if a key exists
ctx.sessionState.delete('key')Remove a key (returns true if it existed)
ctx.sessionState.clear()Remove all keys
ctx.sessionState.keys()IterableIterator<string> of all keys
ctx.sessionState.sizeNumber of stored entries

ctx.goTo(menuId, options?)

goTo(menuId: string, options?: Record<string, unknown>): Promise<void>

Navigates to the specified menu. options are passed as ctx.options in the target menu.


ctx.goBack(result?)

goBack(result?: unknown): Promise<void>

Pops the navigation stack. If the stack is empty and a fallback menu is configured, navigates there instead. If no fallback, closes the session.


ctx.close()

close(): Promise<void>

Ends the session entirely.


ctx.hardRefresh()

hardRefresh(): Promise<void>

Re-runs the menu factory from scratch — re-executes setup() and re-registers all callbacks. Use when the menu's structure (not just its content) needs to change based on data. Typically needed in fewer than 10% of cases.


ctx.openSubMenu(menuId, opts)

openSubMenu(menuId: string, opts: SubMenuOptions): Promise<void>

Opens a sub-menu with a completion callback. onComplete fires when the sub-menu calls ctx.complete().

interface SubMenuOptions {
onComplete: (ctx: MenuContext, result?: unknown) => Awaitable<void>;
[key: string]: unknown; // additional options passed to the sub-menu
}

See Sub-Menus.


ctx.complete(result?)

complete(result?: unknown): Promise<void>

Marks the current sub-menu as complete and calls goBack(). The parent's onComplete callback fires with result. Has no effect if called outside a sub-menu context.


Discord

ctx.client

client: Client<true>

The logged-in Discord.js client.


ctx.interaction

interaction: Interaction

The current interaction. On first render this is the ChatInputCommandInteraction that opened the session. It updates to the most recent component interaction as the user interacts with the menu.


ctx.options

options: TOptions

Options passed to handleInteraction() or goTo(). Type-safe when TOptions is provided as a generic.


ctx.menu

menu: MenuInstanceLike
PropertyTypeDescription
ctx.menu.namestringThe menu's registered name
ctx.menu.mode'embeds' | 'layout'The render mode

ctx.session

session: MenuSessionLike
PropertyTypeDescription
ctx.session.idstringUnique session identifier
ctx.session.isCancelledbooleanWhether the session was cancelled
ctx.session.isCompletedbooleanWhether the session has ended (closed, timed out, or cancelled)
ctx.session.canGoBackbooleanWhether the navigation stack has entries
ctx.session.sessionStateStateStoreSame as ctx.sessionState

ctx.pagination

pagination: PaginationState | null

null when no pagination is active. Populated during render when list pagination or button pagination is active.

PropertyTypeDescription
currentPagenumberZero-based current page index
totalPagesnumberTotal number of pages
itemsPerPagenumberItems per page
totalItemsnumberTotal item count
startIndexnumberInclusive start offset for the current page
endIndexnumberExclusive end offset for the current page

See Pagination.