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

270
Vistas
I don't know how to get slash commands working for my dice command

I am trying to make a dice slash command with a command handler that posts a picture of the dice, but it says url1 undefined. When I try just posting the pictures directly, it says interaction failed: this interaction has already been acknowledged.

module.exports= {
    name: "diceslash",
    description:"dice slashcommand",
    execute(interaction){
        const diceroll = Math.floor(Math.random() * 6) + 1
        const dices = Math.floor(Math.random() * 6) + 1

        const ans = diceroll + dices;

        if (diceroll == 1){
            const url1 = 'https://imgur.com/a/YdAmiRe';
        }
        if (diceroll == 2){
            const url1 = 'https://imgur.com/a/w35gKMR';
        }
        if (diceroll == 3){
            const url1 = 'https://imgur.com/a/MkDVhQS';
        }
        if (diceroll == 4){  
            const url1 = 'https://imgur.com/a/WCuaCbL';         
        }
        if (diceroll == 5){
            const url1 = 'https://imgur.com/a/0xyPIkx';
        }
        if (diceroll == 6){
            const url1 = 'https://imgur.com/a/urv1H42';
        }
        if (dices == 1){
            const url2 = 'https://imgur.com/a/YdAmiRe';
        }
        if (dices == 2){
            const url2 = 'https://imgur.com/a/w35gKMR';
        }
        if (dices == 3){
            const url2 = 'https://imgur.com/a/MkDVhQS';
        }
        if (dices == 4){  
            const url2 = 'https://imgur.com/a/WCuaCbL';         
        }
        if (dices == 5){
            const url2 = 'https://imgur.com/a/0xyPIkx';
        }
        if (dices == 6){
            const url2 = 'https://imgur.com/a/urv1H42';
        }
     
        interaction.reply ({content: url1+" "+url2})
        interaction.reply({content: "you got "+ ans})
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const is block scoped, so url1 and url2 are only available inside the if statements. Outside of those blocks, these variables are undefined.

You probably shouldn't use so many if statements though. Just store the images in an array or a map and pick one of those like this:

module.exports = {
  name: 'diceslash',
  description: 'dice slashcommand',
  execute(interaction) {
    const dices = [
      'https://imgur.com/a/YdAmiRe',
      'https://imgur.com/a/w35gKMR',
      'https://imgur.com/a/MkDVhQS',
      'https://imgur.com/a/WCuaCbL',
      'https://imgur.com/a/0xyPIkx',
      'https://imgur.com/a/urv1H42',
    ];
    const dice1 = Math.floor(Math.random() * 6) + 1;
    const dice2 = Math.floor(Math.random() * 6) + 1;
    const ans = dice1 + dice2;
    // array's index starts at zero
    const url1 = dices[dice1 - 1];
    const url2 = dices[dice2 - 1];

    interaction.reply({ content: `${url1} ${url2} \n you got ${ans}` });
  },
};

You could also store the dices in an object with their URLs and values and simplify it like this:

module.exports = {
  name: 'diceslash',
  description: 'dice slashcommand',
  execute(interaction) {
    const dices = [
      { url: 'https://imgur.com/a/YdAmiRe', value: 1 },
      { url: 'https://imgur.com/a/w35gKMR', value: 2 },
      { url: 'https://imgur.com/a/MkDVhQS', value: 3 },
      { url: 'https://imgur.com/a/WCuaCbL', value: 4 },
      { url: 'https://imgur.com/a/0xyPIkx', value: 5 },
      { url: 'https://imgur.com/a/urv1H42', value: 6 },
    ];
    // helper function to pick a random item from an array
    const pick = (arr) => arr[Math.floor(Math.random() * arr.length)];
    const dice1 = pick(dices);
    const dice2 = pick(dices);
    const ans = dice1.value + dice2.value;

    interaction.reply({ content: `${dice1.url} ${dice2.url}` });
    interaction.followUp({ content: `you got ${ans}` });
  },
};

As for the second part of your question; the error is thrown because you try to reply to an interaction twice. An interaction can only be responded to once. You could either send a single response (see my first snippet) or use the followUp() method for the second message (see the second snippet).

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