Components
You can make your components sent by your commands trigger a callback in the command itself, instead of having to listen to interactionCreate separately.
This is only recommended for simple non complex components that originate directly from a command.
For other cases, create an interactionCreate subscriber.
Given only these are executable, you can only use this in Standalone Commands, SubCommands and Context Menu Commands.
Usage
In this example we'll use a standalone command, but the same applies to the rest.
Bot extraction
You'll first need your bot instance, since it contains the codec used to de/serialize the customIds.
You can either instantiate your command with it, or get it from the execution metadata:
import { , , type } from '@nyx-discord/djs';
import type { } from 'discord.js';
class extends {
public async (: , : ) {
const bot = ..(, true); }
}To learn more about TypedFields, check the Metadata page.
Component creation
Create your component the same way you'd normally do, but use the bot instance to create the customId:
import { , } from 'discord.js';
import type { } from 'discord.js';
import { , , type } from '@nyx-discord/djs';
class extends {
public async (: , : ) {
const = ..(, true);
const = this.();
// Other properties omitted for brevity
const = new ().();
const = new <>().();
await .({ : [] });
}
}Interaction handling
When the component is clicked, the respective callback will be executed:
handleButtonfor buttonshandleSelectMenufor select menushandleModalfor modals
import { } from '@nyx-discord/djs';
import type { } from 'discord.js';
class extends {
public async (: ) {
await .('You clicked the button!');
}
}Attaching extra data
You can also attach an extra string to the customId, which will be sent back when the component is clicked. The buildCustomId method accepts an optional data parameter, which can be any string.
When the component is clicked, the metadata will contain the data you passed in the customId. It's cached there so you don't need to de-serialize it again.
import { , , type } from '@nyx-discord/djs';
import type { , } from 'discord.js';
class extends {
public async (: , : ) {
const = ..(, true);
const = this.(, 'my-data');
}
public async (: , : ) {
const = ..(, true);
await .() // Replies 'my-data'
}
}Complex data
If you need to store a complex object as extra data you can consider using the @sapphire/string-store package, which is what
the framework already uses for serialization to short strings:
import { , , type } from '@nyx-discord/djs';
import { , } from '@sapphire/string-store';
import { , , type , type } from 'discord.js';
class extends {
protected static readonly = new ()
// Add an schema with a 'name' field stored as a string and an 'age' field stored as a int32:
.(new (0).('name').('age'));
public async (: , : ) {
const = ..(, true);
const = ..(0, { : 25, : 'amgelo563' });
const = this.(, );
const = new ().();
const = new <>().();
await .({ : [] });
}
protected async (: , : ) {
// We can force the get here (`true`) since we know we passed an extra
const = ..(, true);
const = ..().; // { age: 25, name: 'amgelo563' }
await .(`Name: ${.}, Age: ${.}`);
}
}Check their documentation for more information and examples.
Referring to other commands
You can also execute other commands callbacks by creating a customId that refers to them.
In order to do so, you'll need the command instance first. You can read how to obtain it in the Command Location page.
import { , , type } from '@nyx-discord/djs';
import type { } from 'discord.js';
class extends {
public async (: , : ) {
const = ..(, true);
/** Somehow get the command instance using one of the provided methods, eg: */
const = .().().('my-other-command');
if (! || .()) return;
/** Then build the customId: */
const = .();
}
}When the button is clicked, the MyOtherCommand's handleButton callback will be executed.
