有没有一种方法可以将数组放入JSON文件并在JS文件中使用它?

时间:2020-11-12 14:18:36

标签: javascript node.js json discord.js

我正在制作一个不和谐的机器人,该机器人只是向您显示鸟类的随机图像,并且我尝试将这些链接的数组放入单独的JSON文件中,并在主JS文件中使用它,但是由于某些原因,它并没有这样做。工作。我在另一个机器人中使用了该方法,并且效果很好。下面的代码

const fs = require("fs")
const config = require("./PD_C/config.json")
const Discord = require('discord.js')
const BIRD_LINKS = require("./Bird links.json")

let BIRD_LIST = [BIRD_LINKS]

const client = new Discord.Client()
client.commands = new Discord.Collection()

const PD_C = fs.readdirSync("./PD_C").filter(file => file.endsWith(".js"));

for(const file of PD_C) {
    const command = require(`./PD_C/${file}`)
    client.commands.set(command.name, command)
}
let Lpass
client.on('ready', () => {
    console.log("Project ∆ active, awaiting input...")
    client.user.setActivity("Stratzenblitz | s.help")
    
    // just a comment
    client.guilds.fetch(config.STRATZ_SERVER_ID).then((stratzGuild) => {
        Lpass = stratzGuild.roles.cache.find(Lpass => Lpass.id === "438553700492115968");
    });
})

    if(command === "bird" || command === "birb") {
        message.channel.send(`birb\r${BIRD_LIST[Math.floor(Math.random() * BIRD_LIST.length)]}`)
    }

在此示例中,代码在调用命令后给出[object Object]响应,但在下一个示例中,它可以正常工作

const Discord = require('discord.js');
const cat_links = require("./Cat_links.json")
const dog_links = require("./Dog_links.json")

const client = new Discord.Client();

let catlinks = [cat_links];
let doglinks = [dog_links]

client.once('ready', () => {
   console.log('EvBot is online!')
   client.user.setActivity("with my code")
});

   if(command === "cat") {
      message.channel.send(`cat \r${catlinks[Math.floor(Math.random() * catlinks.length)]}`)
   }
   if(command === "dog") {
      message.channel.send(`doggo \r${doglinks[Math.floor(Math.random() * doglinks.length)]}`);
   }

在此示例中,它工作正常,我看不到区别。

编辑:抱歉,忘记包含JSON文件的内容 工作中的人

{
    "doglinks": [
        "https://images.pexels.com/photos/1108099/pexels-photo-1108099.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/1870301/pexels-photo-1870301.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/133069/pexels-photo-133069.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/3687770/pexels-photo-3687770.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/3631659/pexels-photo-3631659.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/1839753/pexels-photo-1839753.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/4681107/pexels-photo-4681107.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/2623968/pexels-photo-2623968.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/2820134/pexels-photo-2820134.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/3009441/pexels-photo-3009441.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/57627/pexels-photo-57627.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/933498/pexels-photo-933498.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/2007/animal-dog-pet-cute.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/1562983/pexels-photo-1562983.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/97082/weimaraner-puppy-dog-snout-97082.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/731022/pexels-photo-731022.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/2253275/pexels-photo-2253275.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/1805164/pexels-photo-1805164.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/1390784/pexels-photo-1390784.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/21259/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/46505/swiss-shepherd-dog-dog-pet-portrait-46505.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/981062/pexels-photo-981062.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/422220/pexels-photo-422220.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/247522/pexels-photo-247522.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/235805/pexels-photo-235805.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/1390361/pexels-photo-1390361.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/169524/pexels-photo-169524.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/686094/pexels-photo-686094.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/164186/pexels-photo-164186.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/1938126/pexels-photo-1938126.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/128817/pexels-photo-128817.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/257540/pexels-photo-257540.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/895259/pexels-photo-895259.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/545063/pexels-photo-545063.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/374906/pexels-photo-374906.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/1629781/pexels-photo-1629781.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/434090/pexels-photo-434090.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/59523/pexels-photo-59523.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/733416/pexels-photo-733416.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/1490908/pexels-photo-1490908.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/1485637/pexels-photo-1485637.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/33053/dog-young-dog-small-dog-maltese.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/58997/pexels-photo-58997.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/160846/french-bulldog-summer-smile-joy-160846.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
        "https://images.pexels.com/photos/374898/pexels-photo-374898.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
    ]
}

不工作的人

{
"BIRD_LINKS": [
    "https://cdn.discordapp.com/attachments/773929540527194133/773929672021901342/owl-05.jpg",
    "https://cdn.discordapp.com/attachments/773929540527194133/773929672463351818/image-3.gif", 
    "https://cdn.discordapp.com/attachments/773929540527194133/773929753374883870/91943b9f4fb275b2cd147e12bb307581.jpg",
    "https://cdn.discordapp.com/attachments/773929540527194133/773929804373557269/bird_fast.gif",
    "https://cdn.discordapp.com/attachments/773929540527194133/773929883779858512/unknown.png",
    "https://cdn.discordapp.com/attachments/773929540527194133/773930431774589008/giphy.gif",
    "https://cdn.discordapp.com/attachments/773929540527194133/773930432064258048/47d89e49583bcba6dd3ee5f147775367.jpg",
    "https://cdn.discordapp.com/attachments/773929540527194133/773930432349077544/MOxPZ4F.jpg",
    "https://cdn.discordapp.com/attachments/773929540527194133/773930432592871424/IMG_20190217_152641.jpg",
    "https://cdn.discordapp.com/attachments/773929540527194133/773930432805863434/P4ScrZK.png",
    "https://cdn.discordapp.com/attachments/773929540527194133/773930433104052275/pollero.png",
    "https://cdn.discordapp.com/attachments/773929540527194133/773930433830322176/abirdofprey.gif",
    "https://cdn.discordapp.com/attachments/773929540527194133/773931104565985290/ElofXFtX0AIF00S.png",
    "https://cdn.discordapp.com/attachments/773929540527194133/773931104813318144/IMG_20201101_194017.jpg",
    "https://cdn.discordapp.com/attachments/773929540527194133/773931105086341120/IMG_20201031_222458.jpg",
    "https://cdn.discordapp.com/attachments/773929540527194133/773931105359233095/ec6e3934d75deff575774d208a919c6d.jpg",
    "https://cdn.discordapp.com/attachments/773929540527194133/773931105635663903/image0-20.png",
    "https://cdn.discordapp.com/attachments/773929540527194133/773931105937391666/FB_IMG_1585676154854.jpg",
    "https://cdn.discordapp.com/attachments/733968268867403796/774279931735441468/unknown.png",
    "https://cdn.discordapp.com/attachments/733968268867403796/774280579155492894/unknown.png",
    "https://cdn.discordapp.com/attachments/733968268867403796/774281662217453578/unknown.png",
    "https://cdn.discordapp.com/attachments/733968268867403796/774284048733306920/unknown.png",
    "https://cdn.discordapp.com/attachments/527012837055266837/774284540297609276/unknown.png",
    "https://cdn.discordapp.com/attachments/527012837055266837/774286061597753344/unknown.png",
    "https://cdn.discordapp.com/attachments/527012837055266837/774286852382523452/manbjv45rii21.png",
    "https://cdn.discordapp.com/attachments/527012837055266837/774303395355557908/unknown.png",
    "https://cdn.discordapp.com/attachments/527012837055266837/774302418526535710/unknown.png",
    "https://cdn.discordapp.com/attachments/733968268867403796/775734273704853554/unknown.png",
    "https://cdn.discordapp.com/attachments/733968268867403796/775734747297087498/unknown.png",
    "https://cdn.discordapp.com/attachments/733968268867403796/775735211148836874/unknown.png",
    "https://cdn.discordapp.com/attachments/733968268867403796/775735412429160459/unknown.png",
    "https://cdn.discordapp.com/attachments/733968268867403796/775736292360060948/unknown.png",
    "https://cdn.discordapp.com/attachments/733968268867403796/775736522778869790/unknown.png"
    ]
}

0 个答案:

没有答案