Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

javascript - My discord.js command doesn't return all the contents from a json file

Hi i'm making a command for my bot which lists all "orders" from a orders.json file the orders.json file has these "orders" stored like this

{
    "ysh": {
        "orderID": "ysh",
        "userID": "734532125021307001",
        "guildID": "745621984192364574",
        "guild": "Cybers Taco Stand Server",
        "channelID": "745621984192364578",
        "order": "taco",
        "customer": "ultradeadmeme#7674",
        "status": "Ready",
        "ticketChannelMessageID": "not set",
        "chef": "597167860200243212",
        "chefmention": "ultradeadmeme#7674"
    },
    "lhb": {
        "orderID": "lhb",
        "userID": "734532125021307001",
        "guildID": "745409671430668389",
        "guild": "Cybers Taco Stand Server",
        "channelID": "746423099871985755",
        "order": "a",
        "customer": "Aro#1221",
        "status": "Unclaimed",
        "ticketChannelMessageID": "not set"
    }
}

and now i want the command to lists all the orders in that file the command code:

const fsn = require("fs-nextra");
const Discord = require('discord.js');
module.exports = {
    name: 'list',
    description: 'List of all orders',
    aliases: ['allorders'],
    execute(message) {
        

        fsn.readJSON("./orders.json").then((orderDB) => {
            let orderString;
            for(let x in orderDB) {
                orderString = "`" + x + "`: " + orderDB[x].status + "
";
                // add newline character at the end to display each "order" on a separate line
            }
            const exampleEmbed = new Discord.MessageEmbed()
                .setTitle('Here's a list of the current orders and their status.')
                .setDescription(orderString)
                .setTimestamp()
                .setFooter(message.member.user.tag, message.author.avatarURL());
            message.channel.send(exampleEmbed);
        });
    }    
}

the problem is it only lists one order but i want it to view all the orders any way to fix this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Why aren't you using the normal fs library of node?

and you are not appending to orderString you are replacing it

your code does:

orderstring = "ysh : Ready" and then
orderstring = "lhb : Unclaimed" which replaces the first order, so the right Code would be:

const fsn = require("fs-nextra");
const Discord = require('discord.js');
module.exports = {
    name: 'list',
    description: 'List of all orders',
    aliases: ['allorders'],
    execute(message) {
        

        fsn.readJSON("./orders.json").then((orderDB) => {
            let orderString;
            let amount = 0;
            for (let x in orderDB) {
        if (orderString != undefined) {
            orderString += "`" + x + "`: " + orderDB[x].status + "
";
        } else {
            orderString = "`" + x + "`: " + orderDB[x].status + "
";
        }
        amount++;
        // add newline character at the end to display each "order" on a separate line
    }            const exampleEmbed = new Discord.MessageEmbed()
                .setTitle('Here's a list of the current orders and their status.')
                .setDescription(orderString)
                .setTimestamp()
                .setFooter(message.member.user.tag, message.author.avatarURL());
            message.channel.send(exampleEmbed);
        });
    }    
}

I also added a test if orderString is undefined, because else you would have:undefined`ysh`: Ready


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...