Reputation: 31
I am trying to make a click button event in discord.js
v13 but it won't work. Here is my code :
client.on('interactionCreate', async button => {
if(button.CustomId === 'pricestore') {
button.channel.send("test:")
button.defer();
}
})
Upvotes: 0
Views: 2804
Reputation: 957
Buttons are also called Interactions, Discord has multiple types of Interactions, Slash Commands, Buttons, Modals, etc - they are all passed through this event and to create a bot with these types of features you will need to implement something similar to what is described below.
client.on('interactionCreate', async interaction => {
// . . .
});
You will first need to create the listener for the interactionCreate
event, this will allow the bot to listen for people using Interactions.
.isApplicationCommand()
.isAutocomplete()
.isButton()
.isCommand()
.isContextMenu()
.isMessageComponent()
.isMessageContextMenu()
.isModalSubmit()
.isSelectMenu()
.isUserContextMenu()
All of the above functions can be used to determine what type of interaction it is that you are getting. We will be using the isButton
function as that is what is shown in the question.
You will need to check the interaction type inside the event we created before and it will look like this:
client.on('interactionCreate', async interaction => {
if(interaction.isButton()) {
// . . .
}
});
Now that we know we are getting a button event, we will want to check what button it is that is being clicked, doing this allows you to have multiple buttons as you can just add a new check for each of them.
client.on('interactionCreate', async interaction => {
if(interaction.isButton()) {
if(intearction.customId == 'pricestore') {
// . . .
}
}
});
Finally, we will send a message to the channel once the button is clicked.
client.on('interactionCreate', async interaction => {
if(interaction.isButton()) {
if(intearction.customId == 'pricestore') {
interaction.channel.send({ content: 'Test Message!' });
}
}
});
Hope this was of help!
Upvotes: 1