discord.js如何修复以未定义形式返回的变量

问题描述 投票:2回答:1

我浏览了详尽的问题清单,询问类似的问题,但是在这种情况下,没有一个能够解决这个问题。

Essentially,我正在尝试为discord.js中的机器人构建二十一点命令,到目前为止,流程看起来像这样:它将所有玩家的姓名和ID传递给他们进入处理每个游戏的功能。首先,定义了三个功能。一个可以发现一手牌的价值(卡的总数),另一个可以添加一张新卡。创建了一个RichEmbed,将对其进行多次编辑,并且第一次在游戏中创建它时,它将给每个人他们的手,对其进行评估,然后将其放入嵌入中。

问题是:存储玩家手部和手部值的变量在初始化期间返回为未定义,即使我已经测试过它们可以从代码中的同一位置输出,并且给出了他们的数据也可以工作。是什么导致它们未定义,为什么仅是这两个变量?其他的都很好。

我的代码

async function bj(players_id, players_names) {
            var pc = players_id.length; //player count
            var turn; //whose turn is it? based on id
            var disp; //text to be shown in embed; game status.
            var hands = []; //each player's hand
            var handValues = []; //the sum of the cards in a hand
            var cards = ["A", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]; //all possible cards
            var ing = []; //is player in game? / have they busted?

            turn = players_id[0];
            var disp_dir = "React with 👌 to hit and ✋ to stand.";
            disp = `Currently ${message.member.guild.members.get(turn).displayName}'s turn.\n${disp_dir}`;

            function addCard() {return(cards[Math.floor(Math.random() * cards.length)])}; //simply grabs a card from the list of cards

            function findHandValue(handValue, currentCards) { //takes two inputs, one is the current hand value, the other is a list of card values. finds the total value of the hand and returns it.
                var val = 0;
                for (i = 0; i < currentCards.length; i++) {
                    card = currentCards[i];
                    if (card == "A") {
                        if (handValue > 21) {val += 1;} else {val += 11;};
                    } else if (card == "J" || card == "Q" || card == "K") {
                        val += 10;
                    } else {val += card;};
                };
                return(val);
            };

            function makeEmbed(first) { //constructs the embed that will be used to show the game. param. first tells whether it is the first time the embed is being constrcuted in a game.
                var bjg = new Discord.RichEmbed()
                .setTitle("Blackjack Game")
                .setDescription(`Initiated by ${players_names[0]}`)
                .addField("Status", `${disp}`)
                .setThumbnail("https://cdn.discordapp.com/attachments/563578266984775681/657323254813425674/cards.jpg")
                .setColor("DC134C")
                .setFooter("Valkyrie", client.avatarURL)
                .setTimestamp();

                if (first == true) {
                    for (i = 0; i < pc; i++) { //should be creating each player's hand
                        ing.push(true); //this one works
                        hands.push([addCard(), addCard()]); //but for some reason, this one returns undefined
                        handValues.push(findHandValue(0, hands[i])); //and this one
                        bjg.addField(`${players_names[i]}'s Hand`, `${hands[i]}\n\nValue: ${handValues[i]}`); //spits out undefined in almost every place, including the player_name[i]
                        var bjs = message.channel.send(bjg);
                        return(bjs);
                    };
                };
            };
            var bjs = await makeEmbed(true); //and this stuff works fine
            await bjs.react("👌");
            await bjs.react("✋");
        };
        if (args[0] == "quick") {
            message.reply("You've started a game of solo blackjack against a bot!");
            await bj([message.author.id], [message.member.displayName]);

很长的道歉,但所有必要的操作才能证明它在做什么,并帮助查明错误的根源。

最后,这是结果

The result

javascript node.js discord.js
1个回答
0
投票

MDN doc

等待操作符用于等待Promise。 只能使用在异步函数中。

您在这段代码中正在等待时调用makeEmbed(...),但makeEmbed不是异步函数

function makeEmbed(first) {...};
        var bjs = await makeEmbed(true);
        await bjs.react("👌");
        await bjs.react("✋");

您应替换为

async function makeEmbed(first) {...};
        var bjs = await makeEmbed(true);
        await bjs.react("👌");
        await bjs.react("✋");
© www.soinside.com 2019 - 2024. All rights reserved.