【问题标题】:Reading a config.json from a nodejs discord bot从 nodejs discord 机器人读取 config.json
【发布时间】:2020-06-03 20:30:36
【问题描述】:

我想知道是否有其他方法可以从 config.json 文件中读取我的代码?

例如,这是在我的 config.json 中

{
            "prefix": "!",
            "Settings":
            [
            {"bot_secret_token": "11111111111111"},
            {"PushToken": "11111111111111"},
            {"PushREF": "11111111111111"}, 
            {"Push_EventID": "11111111111111"},
            {"Home_IP": "11111111111111"},
            {"Home_port": "11111111111111"},
            {"Homes2_Port": "11111111111111"},
            {"GROUPNAME":"Settings"},
            {"OwnerID":"11111111111111"},
            {"StatusCHannelID":"11111111111111"},
            {"Channel2Way":"11111111111111"},
            {"PushAccount1":"11111111111111"},
            {"PushAccount2":"11111111111111"},
            {"GuildID":"11111111111111"},
            {"HumGroup":"11111111111111"},
]
}

当我想读出例如 Ownerid 时,我需要使用此代码 所以我总是需要数数[8]

let owner =  client.users.get(config.Settings[8].OwnerID);

有没有办法摆脱它并以更好的方式做到这一点?

问候

【问题讨论】:

    标签: arrays node.js discord.js


    【解决方案1】:

    你可以的

    const config = require('./config');
    
    const found = config.Settings.find((elem) => Object.keys(elem).some((key) => key == 'OwnerID'));
    
    // found is equal to { OwnerID: '11111111111111' }
    let owner = client.users.get(found.OwnerID);
    

    【讨论】:

    • 非常感谢,但遗憾的是它不适用于我的代码。我需要在它之前使用 client.users.get,如果它只能返回 111111111111 值它可能会解决问题
    • 谢谢,但我可以将它用于多个项目吗?像 key == 'OwnerID' & 'DeviceID' 还是我必须为它创建另一行?问候
    • 您可以从中创建一个函数,其中参数将是您要查找的键。
    • 它不会对多个项目起作用,我建议你创建一个函数并多次重复使用它。
    猜你喜欢
    • 2018-06-18
    • 2022-12-03
    • 2019-01-15
    • 2019-05-08
    • 2021-04-01
    • 2021-02-02
    • 2018-06-26
    • 2021-07-01
    • 2021-01-01
    相关资源
    最近更新 更多