MiddlewareFunction
Middleware functions allow you to run code before your interaction handlers. They are useful for logging, authentication, database setup, and more.
Definition
Section titled “Definition”type MiddlewareFunction<Context extends BaseInteractionContext = BaseInteractionContext> = ( context: Context, next: () => Promise<void>) => Promise<Response | void> | Response | void;Example
Section titled “Example”const myMiddleware: MiddlewareFunction<MyContext> = async (c, next) => { console.log(`Received interaction: ${c.req.method} ${c.req.path}`); await next();};
bot.use(myMiddleware);- Middleware functions must call
await next()to continue execution to the next middleware or handler. - If a middleware returns a
Response(e.g., for error handling), the execution chain stops there. - Honocord’s
usemethod accepts multiple middleware functions.