我不知道如何让斜杠命令为我的骰子命令工作

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

我正在尝试使用发布骰子图片的命令处理程序制作骰子斜线命令,但它显示 url1 undefined。当我尝试直接发布图片时,它显示 交互失败:此交互已被确认

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})
    }
}

const 是块范围的,因此 url1url2 仅在 if 语句中可用。在这些块之外,这些变量是 undefined.

尽管如此,您可能不应该使用那么多 if 语句。只需将图像存储在数组或地图中,然后选择其中之一:

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}` });
  },
};

您还可以将骰子及其 URL 和值存储在一个对象中,并像这样简化它:

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}` });
  },
};

关于你问题的第二部分;抛出错误是因为您尝试两次回复交互。一次交互只能响应一次。您可以发送单个响应(请参阅我的第一个片段)或对第二条消息使用 followUp() 方法(请参阅第二个片段)。