Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

289
Vistas
Discord JS: bot reply via message button

I'm currently learning how to use Discords Buttons but I ran into a problem. The following code should create a button with the command /startgame, when you press this button it should just say "hello".

const { SlashCommandBuilder } = require("@discordjs/builders")
const { MessageButton, MessageActionRow } = require("discord.js")

module.exports ={
    data: new SlashCommandBuilder()
    .setName("startgame")
    .setDescription("Startet das Spiel"),
    async execute(interactionCreate) {
        if (interactionCreate.isCommand()) {
            const row1 = new MessageActionRow()
                .addComponents(
                    new MessageButton()
                        .setCustomId("start")
                        .setLabel("   Start   ")
                        .setStyle(3)
                        .setDisabled(false),
            )
            await interactionCreate.reply({content: " ", components: [row1]})
        }
        
        else if(interactionCreate.isButton()) {
            switch (interactionCreate.customId) {
                case "start": {
                    return interactionCreate.reply("hello")
                }
            }
        }
    }
}

It creates the button, but when you press it, it only says: This interaction failed I appreciate any help.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

OK so one of these parts should be under your event listener interactionCreate rather than in a command. So if you have that section in your main bot.js file it would look like this:

Command File

const {
    SlashCommandBuilder
} = require("@discordjs/builders")
const {
    MessageButton,
    MessageActionRow
} = require("discord.js")

module.exports = {
    data: new SlashCommandBuilder()
        .setName("startgame")
        .setDescription("Startet das Spiel"),
    async execute(interaction) {
        const row1 = new MessageActionRow()
            .addComponents(
                new MessageButton()
                .setCustomId("start")
                .setLabel("   Start   ")
                .setStyle(3)
            )
        return interaction.reply({
            components: [row1]
        })
    }
}

bot.js file - could be named whatever you named it

client.on('interactionCreate', async interaction => {
    if (interaction.isButton()) {
        const buttonID = interaction.customId
        if (buttonID === 'start') {
            interaction.reply({
                content: 'Hello'
            })
        }
    }
})

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda