Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!