使用 tmi.js 的 Twitch Bot 游戏我希望对我的数组有一些帮助(获胜者)

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

据我所知,数组中有一个零?因此,每次出现零作为“获胜者”时,就没有用户名可以调用,它会告诉聊天未定义已获胜?我该如何做到总是有一个获胜者?

    const entries = {};
client.on('message', (channel, user, message, self) => {
    if(self) return;
    const entriesArr = Object.values(entries);
    const randomNum = Math.floor(Math.random() * entriesArr.length);
    const winner = entriesArr [randomNum];
    if(message.toLowerCase() === '!join') {
        entries[user.username] = user.username;
    }
});

这是应显示获胜者的输出消息。 哪个有效,但永远不需要显示未定义的赢家。

setTimeout(() => {
            client.action(TwitchHost, `"Looks like ${winner} walks out with the valuables! What happened to the others..?"`);
        }, 2500);
javascript bots twitch
1个回答
0
投票

问题出在你的身上 const randomNum = Math.floor(Math.random() *entriesArr.length)

SB const randomNum = Math.floor(Math.random() *entriesArr.length +1)

© www.soinside.com 2019 - 2024. All rights reserved.