Skip to content

Custom Ids

Buttons and modals need to be given a “custom id” when you create them, so that the correct handler is used. The simplest way to do this is just to use a static custom id.

import { button } from 'jellycommands';
export default button({
id: 'test',
async run({ interaction }) {},
});

Unlike commands, we don’t need to tell Discord about buttons and modals before we use them. They are effectively created every time you reply to an interaction with them. This means that our custom ids can be dynamic! This can simplify some interactions since we can store some information on the id. In order to make this possible the id option on a button component can also be regex or a function.

Regex

This regex will be used to see if we have found a match for the button interaction. It should always start with ^ and $ to ensure it’s matching the whole id rather than just a section of it, and not be global.

import { button } from 'jellycommands';
export default button({
id: /^page_\d+$/,
async run({ interaction }) {
console.log(interaction.customId);
},
});

Matcher Function

This function is passed the custom id, and then should return a boolean that indicates whether a match has been found.

import { modal } from 'jellycommands';
export default modal({
id: (customId) => {
return customId.startsWith('page_');
},
async run({ interaction }) {
console.log(interaction.customId);
},
});

Deferring

By default Discord requires you to respond to an interaction within 3 seconds, otherwise it marks the interaction as failed. Often it’ll take longer than 3 seconds to respond, so you need to “defer” your reply. If you defer your command you need to use followUp instead of reply:

import { button } from 'jellycommands';
export default button({
id: 'test',
defer: true,
async run({ interaction }) {
await interaction.reply('Hello World');
await interaction.followUp('Hello World');
},
});

Read more on deferring.