简体   繁体   English

如何从discord.js中的JSON文件中选择随机对象

[英]How to select random object from JSON file in discord.js

I've done some searching around and I found some posts on here but my code doesn't want to work.我已经做了一些搜索,我在这里找到了一些帖子,但我的代码不想工作。 Basically I'm making a discord bot and I want to select an object from a JSON file at random.基本上我正在制作一个不和谐的机器人,我想从一个 JSON 文件中随机选择一个对象。

This is my command:这是我的命令:

const UserData = require('../data/users.js');
const monster = require('../data/monsters.json');

module.exports = {
    name: 'battle',
    aliases: ['fight'],
    cooldown: 0,
    description: 'User fights against a monster alone or in group',
    execute(client, message, args) {
        let enemy = monster[Math.floor(Math.random() * monster.length)]

        UserData.findOne({
            userID: message.author.id
        }, (error, userdata) => {
            if (error) console.log(error);

            if (!userdata) {
                return message.reply(`you don't have an account!`);
            } else {
                console.log(enemy);
                return message.channel.send(`${enemy} spawned!`);
            }
        })

    }
}

And this is my JSON file:这是我的 JSON 文件:

"1" : {
        "name": "Blue Slime",
        "hp": 20,
        "atk": 12,
        "def": 10,
        "spatk": 3,
        "spdef": 12,
        "spd": 100,
        "gold": 10,
        "xp": 50,
        "lvl": 1
    },

    "2": {
        "name": "Red slime",
        "hp": 20,
        "atk": 12,
        "def": 10,
        "spatk": 3,
        "spdef": 12,
        "spd": 100,
        "gold": 10,
        "xp": 50,
        "lvl": 1
    },

    "3": {
        "name": "Green slime",
        "hp": 20,
        "atk": 12,
        "def": 10,
        "spatk": 3,
        "spdef": 12,
        "spd": 100,
        "gold": 10,
        "xp": 50,
        "lvl": 1
    }
}

If I want put the objects in the command manually and then randomly select them it works and if instead of "monster.length" I put a number then it also works but I still get undefined if it should be 3. This way I also always get undefined in console log from monster.length.如果我想手动将对象放入命令中,然后随机选择它们,它可以工作,如果我输入的不是“monster.length”,那么它也可以工作,但如果它应该是 3,我仍然没有定义。这样我也总是从monster.length 在控制台日志中获取未定义。 What am I doing wrong?我究竟做错了什么?

Your monsters.json file contains an object and objects don't have length s.你的monsters.json文件包含一个对象并且对象没有length s。 You can however convert it to an array, using Object.values() that returns an array of the given object's own enumerable property values.但是,您可以使用Object.values()将其转换为数组,该数组返回给定对象自己的可枚举属性值的数组。

Check out the snippet below:看看下面的片段:

 let monsters = { 1: { name: 'Blue Slime', hp: 20, atk: 12, def: 10, spatk: 3, spdef: 12, spd: 100, gold: 10, xp: 50, lvl: 1, }, 2: { name: 'Red slime', hp: 20, atk: 12, def: 10, spatk: 3, spdef: 12, spd: 100, gold: 10, xp: 50, lvl: 1, }, 3: { name: 'Green slime', hp: 20, atk: 12, def: 10, spatk: 3, spdef: 12, spd: 100, gold: 10, xp: 50, lvl: 1, }, }; function randomObject(obj) { let arr = Object.values(obj); return arr[Math.floor(Math.random() * arr.length)]; } let enemy = randomObject(monsters); console.log(enemy);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM