【问题标题】:creating a game on a discord bot在不和谐机器人上创建游戏
【发布时间】:2017-12-29 06:00:25
【问题描述】:

所以我正在创建一个不和谐的机器人,我希望它滚动一个随机数(我已经知道如何做到这一点),如果滚动高于 49,用户可以使用命令“购买”。现在这是我无法编码的部分。如何将特定命令附加到条件。此外,如果玩家低于 49,他将无法使用“购买”。

if(command === "shekels"){
    var object = (prizes[Math.floor(Math.random()*prizes.length)])
    var shekels = Math.floor(Math.random()*99 +1 )
    message.channel.send(`You rolled ${shekels} shekels !`)
    if(shekels <49){
        message.channel.send("Oi you dont have enough shekels to buy at my store *rubs hands*")
    }//49 brackets
    else{
        message.channel.send("Hey you do have enough shekels to buy something from my store")
    };



};//command bracket



 if(shekels < 49){
            message.channel.send("Oi you dont have enough shekels to buy at my store *rubs hands*");
        }//49 brackets
        else{
            allowedUsers.push(message.author.username);
    };
    if(command === "buy"){
        if(!allowedUsers.includes(message.author.username)){
            return message.channel.send("You did not roll 49 or above so you cannot use this command.");
        }
        else{

            message.channel.send("here " + objectStore);
        };
    };

上面的命令 ^ 对我不起作用

【问题讨论】:

  • 所以当这个人超过 49 岁后,他可以输入另一个命令,让他买东西,如果他没有得到它,他将无法执行该命令

标签: javascript node.js visual-studio discord


【解决方案1】:

为什么不直接在全局范围内创建一个可以使用该命令的用户数组呢?最好使用他们的 id。因此,当它们超过 49 时,它们会被添加到数组中。然后在使用购买命令时检查它们是否在。

var allowedUsers = []; //Should be above everything, put it right under your imports (that is, under require("discord.js"))

//Your command handler code

if(command === "shekels"){
    var object = (prizes[Math.floor(Math.random()*prizes.length)])
    var shekels = Math.floor(Math.random()*99 +1 )
    message.channel.send(`You rolled ${shekels} shekels !`)

    if(shekels >= 49){
        allowedUsers.push(message.author.id);
    }
}

if(command === "buy"){
    if(!allowedUsers.includes(message.author.id)){
        return message.channel.send("You did not roll 49 or above so you cannot use this command.");
    }
    else{
        message.channel.send("here " + objectStore);
    };
};

【讨论】:

    猜你喜欢
    • 2017-11-15
    • 2017-05-10
    • 2011-09-17
    • 2020-09-02
    • 2020-11-29
    • 2018-03-20
    • 2021-08-13
    • 2020-12-28
    • 2019-12-05
    相关资源
    最近更新 更多