M
M
MoneySMile2021-07-29 17:39:50
Node.js
MoneySMile, 2021-07-29 17:39:50

How to filter json data on discord.js?

I have such code

The code
const { MessageEmbed } = require('discord.js');
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('./rules.json', 'utf8'))
//Каждое новое правило мы добавляем в конец.
//Наше правило, это просто строка
module.exports.properities = {
    name:["rules"]
}

module.exports.run = async (bot,message,member,args) => {
    message.delete()
    if(member.roles.cache.find(r => r.id === "773812025994117122")){
        switch(String(args[0]).toLowerCase()) {
            case 'add':
                let rule = {name: args.slice(1).join(" "), value: '\u200B'};
                if (data.rules.length < 25){
                    //Далее мы добавляем правило в конец.
                    data.rules.push({id: data.rules.length, rule: rule});
                    let add = new MessageEmbed()
                    .setAuthor('ADD')
                    .setColor("RED")
                    .setTimestamp()
                    .setTitle(`Вы успешно добавили новый пункт правил`)
                    message.channel.send(add)
                    .then(m => m.delete({timeout : 2000}))
                }else{
                    let add1 = new MessageEmbed()
                    .setAuthor('ADD')
                    .setColor("RED")
                    .setTimestamp()
                    .setTitle(`Правил слишком много`)
                    message.channel.send(add1)
                    .then(m => m.delete({timeout : 2000}))
                    //Максимальное кол. правил.
                }
                //сохраняем
            break;
            case 'edit':
                    let id = args[1];
                    let new_rule = {name: args.slice(2).join(" "), value: '\u200B'};
                if (id > 0 && id < data.rules.length){//Если указан номальный id, т.е будет такое правило и id > 0.
                    //Мы обращаемся к еллементу массива по id
                    //И изменяем rule на новый объект.
                    let edit = new MessageEmbed()
                    .setAuthor('Helix')
                    .setColor("RED")
                    .setTimestamp()
                    .setTitle(`Вы успешно изменили пункт правил: ${args[1]}`)
                    message.channel.send(edit)
                    data.rules[id - 1].rule = new_rule;
                }else{
                    let edit = new MessageEmbed()
                    .setAuthor('Helix')
                    .setColor("RED")
                    .setTimestamp()
                    .setTitle(`Вы не можете изменить это правило! Его нет!`)
                    message.channel.send(edit)
                }
            break;
        }
    }
fs.writeFile('./rules.json', JSON.stringify(data), (err) => {} );
//Теперь каждое новое правило будет добавлено в конец, достаточно просто указать какое у него будет сообщение.
}

to make a whole database with rules
Database
{"rules":[{"id":0,"rule":{"name":"asdasdasd","value":"​"}},{"id":1,"rule":{"name":"asdkaisdaisodjoad","value":"​"}}]}

The data I receive
[
  { id: 0, rule: { name: 'asdasdasd', value: '​' } },
  { id: 1, rule: { name: 'asdkaisdaisodjoad', value: '​' } }
]

And I need to get such an embed from this data.
Screenshot_10.png?width=1085&height=701
What do I need? I need to make a function that will update this message with the rules after adding, deleting, editing any rules. I would also like to receive explanations on how to filter such data and arrange them by embed

It would be nice to go to the discord (Money Smile#6081)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question