Skip to content

Building components

Having sent the component within a channel it’s about time you want to handle the interaction from the component.

To handle them we have to tell seyfert where will be the components located in our project. We have to do this within our seyfert config file.

seyfert.config.js
1
// @ts-check
2
const { config } = require('seyfert');
3
4
module.exports = config.bot({
5
token: process.env.BOT_TOKEN ?? "",
6
intents: ["Guilds"],
7
locations: {
8
base: "src",
9
output: "dist",
10
commands: "commands",
11
events: "events"
12
components: 'components'
13
}
14
});

First of all we are going to create a file inside the directory we have set for the components.

Then we are going to create a class which extends ComponentCommand, something like we do with simple commands, and then we are going to set the type of the component we want to handle (Buttons or anytype of SelectMenu)

In this example I have created a component to reply Hello World to the interaction. I have set the customId of the button to hello-world.

1
import { ComponentCommand } from 'seyfert';
2
3
export default class HelloWorldButton extends ComponentCommand {
4
componentType = 'Button' as const;
5
}

Filtering component interactions

Now we want the handler to handle only the interactions created by the HelloWorld button so we will use the customId we have to set in all the components.

To filter the interactions we are using a function inherited by the ComponentCommand class in which we have to return a boolean.

1
import { ComponentCommand, type ComponentContext } from 'seyfert';
2
3
export default class HelloWorldButton extends ComponentCommand {
4
componentType = 'Button' as const;
5
6
filter(ctx: ComponentContext<typeof this.componentType>) {
7
//we are checking if the customId of the interaction is the same that the one set in my button
8
9
return ctx.customId === 'hello-world';
10
}
11
}

Running the component handler

If the filter function success and returns true the handler will execute a run function with your code logic.

1
import { ComponentCommand, type ComponentContext } from 'seyfert';
2
3
import { MessageFlags } from 'seyfert/lib/types';
4
5
export default class HelloWorldButton extends ComponentCommand {
6
componentType = 'Button' as const;
7
8
//this can be a promise too.
9
10
filter(ctx: ComponentContext<typeof this.componentType>) {
11
return ctx.customId === 'hello-world';
12
}
13
14
async run(ctx: ComponentContext<typeof this.componentType>) {
15
return ctx.write({
16
content: 'Hello World 👋',
17
flags: MessageFlags.Ephemeral
18
});
19
}
20
}