Cannot read property 'MessageButton' of undefined (discord.js)

951 Views Asked by At

I have a probleme, I don't know why but when I use the command "!button", I have an error, there is my code for check it:

const discord = require('discord.js');
const client = new discord.Client();
const disbut = require('discord-buttons')(client);

client.on("message", async (message) => {
    if (message.content == "!button") { // Use this command only once and only on one channel.
    let buttons = new disbut.MessageButton()
        .setStyle('green') // Button Color
        .setLabel('Test') // Button Name 
        .setID('Button') // Button ID
    message.channel.send('Message Text.', { buttons: [buttons] });
    }
    if (message.content == "!urlbutton") { // Use this command only once and only on one channel.
    let buttons2 = new disbut.MessageButton()
        .setStyle('url') // Button Url
        .setLabel('Discord') // Button Name
        .setURL('https://discord.com') // URL for forwarding
        .setDisabled()
    message.channel.send('Message Text.', { buttons: [buttons2] });
    }
});

the error:

let buttons = new disbut.MessageButton()
                             ^

TypeError: Cannot read property 'MessageButton' of undefined
    at Client.<anonymous> (C:\Users\Sans\Desktop\discord-button-main\index.js:8:30)
    at Client.emit (node:events:394:28)
    at MessageCreateAction.handle (C:\Users\Sans\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Sans\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)

what's the probleme ? I don't really know ^^" (there is not all my code)

1

There are 1 best solutions below

1
Kaspr On

I would take a look at this guide to learn more about buttons and interactions.

This uses discord.js v13

To fix the immediate problem, it will require recoding, as follows:

const { Client, MessageActionRow, MessageButton } = require('discord.js');
const client = new Client();

client.on('messageCreate', async message => {
    if (message.content === '!button') {
        let buttons = new MessageActionRow()
            .addComponents(
                new MessageButton()
                .setStyle('SUCCESS') //Choices are PRIMARY, a blurple button, SECONDARY, a grey button, SUCCESS, a green button, DANGER, a red button, LINK, a button that navigates to a URL.
                .setLabel('Test')
                .setCustomId('button')
         message.channel.send({
             content: 'Message Text',
             components: [buttons]
         })
    } else if (message.content === '!urlbutton') {
        let buttons2 = new MessageActionRow()
            .addComponents(
                new MessageButton()
                .setStyle('LINK')
                .setLabel('Test')
                .setURL('https://discord.com')
         message.channel.send({
             content: 'Message Text',
             components: [buttons2]
         })
    }
})

How the buttons are interacted with would be under a different listener:

client.on('interactionCreate', async interaction => {
    if (interaction.isButton()) {
        const buttonID = interaction.customId
        if (buttonID === 'button') {
        // do something
        return interaction.reply({
            content: 'Say something',
            ephemeral: true //set false or remove if you want everyone to see the response
        }
    }
}