Skip to content

Extending CommandContext

You may have encountered a situation where you want to receive custom data within the CommandContext of Seyfert.

To achieve this, simply set the context option when defining the Seyfert client. In this option, you need to use a function called extendContext, where you can handle the interaction or message of your command and return the custom data you want to add to the context. For example:

import {
class Client<Ready extends boolean = boolean>
Client
,
function extendContext<T extends {}>(cb: (interaction: Parameters<NonNullable<BaseClientOptions["context"]>>[0]) => T): (interaction: Parameters<NonNullable<BaseClientOptions["context"]>>[0]) => T

Extends the context of a command interaction.

@paramcb - The callback function to extend the context.

@returnsThe extended context.

@example const customContext = extendContext((interaction) => { return { owner: '123456789012345678', // Add your custom properties here }; });

extendContext
} from 'seyfert';
const
const context: (interaction: Parameters<NonNullable<BaseClientOptions["context"]>>[0]) => {
myCoolProp: string;
}
context
=
extendContext<{
myCoolProp: string;
}>(cb: (interaction: Parameters<NonNullable<BaseClientOptions["context"]>>[0]) => {
myCoolProp: string;
}): (interaction: Parameters<NonNullable<BaseClientOptions["context"]>>[0]) => {
myCoolProp: string;
}

Extends the context of a command interaction.

@paramcb - The callback function to extend the context.

@returnsThe extended context.

@example const customContext = extendContext((interaction) => { return { owner: '123456789012345678', // Add your custom properties here }; });

extendContext
((
interaction: ChatInputCommandInteraction<boolean> | UserCommandInteraction<boolean> | MessageCommandInteraction<boolean> | ComponentInteraction<...> | ModalSubmitInteraction<...> | EntryPointInteraction<...>
interaction
) => {
// Here you can add
return {
myCoolProp: string
myCoolProp
: 'seyfert>>'
};
});
const
const client: Client<boolean>
client
= new
new Client<boolean>(options?: ClientOptions): Client<boolean>
Client
({
BaseClientOptions.context?: (interaction: ChatInputCommandInteraction<boolean> | UserCommandInteraction<boolean> | MessageCommandInteraction<boolean> | ComponentInteraction | ModalSubmitInteraction | EntryPointInteraction<boolean> | When<InferWithPrefix, MessageStructure, never>) => Record<string, unknown>
context
});

Even though you’ve extended the context, the properties you’ve added are not typed in the CommandContext. To type them, you will need to declare the Seyfert module. Check the module declaration guide for more information.