1

Im trying to develop a little warn system for my Discord Bot. If someone types !warn @mention Reason, it should store the data in a JSON File. It works, but only with one User in one Guild. What I want is, that the JSON File looks like this:

     {
        "superniceguildid": 
          {
            "member": "636302212787601408",
            "warns": 3
        },
         {
            "meber": "7837439745387549"
            "warns": 1
       }
}

Now only this exists:

{
    "627818561947041816": {
        "guild": "636302212787601408",
        "warns": 3
    },
   
}

How can I do it, that the File is generating like above? My current code is this:

module.exports = {
    name: 'warn',
    description: "test",
    execute(message, args){
        const { Client, MessageEmbed } = require("discord.js")
        const client = new Client()

        const fs = require("fs")
        const ms = require("ms")
        warns = JSON.parse(fs.readFileSync("./warns.json", "utf8"))
        client.servers = require ("./servers.json")
        let guild = client.servers[message.guild.id].message
        
        
/*Embeds*/
const oops = new MessageEmbed()
        .setTitle("Error")
        .setColor("RED")
        .setDescription("You cant warn a member. Please ask a Moderator")
        .setAuthor("MemeBot", "this is a link")

const Mod = new MessageEmbed()
        .setTitle("Error")
        .setColor("RED")
        .setDescription("You cant warn a Moderator.")
        .setAuthor("MemeBot", "linkhere xD")
/**Commands */
let wUser = message.mentions.users.first() || message.guild.members.cache.fetch(`${args[0]}`)
if (!wUser) return message.channel.send("Are you sure, that this was a User? I think it wasn't one...")
let wReason = args.join(" ").slice(27)
if (!wReason) return message.channel.send("Please tell me, why you want to warn this person. Because, you know, it's a warn :D");
if(!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send(oops)
if(wUser.hasPermission("KICK_MEMBERS")) return message.channel.send(Mod)


if(!warns[message.guild.id]) warns[message.guild.id] = {
    user: wUser.id,
    warns: 0
}


warns[wUser.id].warns++



fs.writeFile("./warns.json", JSON.stringify(warns, null, 4), err => {
    if(err) console.log(err)
});

let warnEmbed = new MessageEmbed()
.setTitle("Warned")
.setColor("YELLOW")
.addField("Warned User", `${wUser}`)
.addField("Moderator", `${message.author.id}`)
.addField("Reason", `${wReason}`)
.addField("Number of Warnings", warns[wUser.id].warns)
.addField("Warned at", `${message.createdAt}`)

let warnonEmbed = new MessageEmbed()
.setTitle("Warned")
.setColor("YELLOW")
.addField("Warned on", `${message.guild.name}`)
.addField("Moderator", `${message.author}`)
.addField("Reason", `${wReason}`)
.addField("Warned at", `${message.createdAt}`)
        
let logchannel = message.guild.channels.cache.find(c => c.id === 'id');
if(!logchannel) return 

wUser.send(warnonEmbed)
logchannel.send(warnEmbed)

}
}

1 Answer 1

2

That particular layout doesn't make a lot of hierarchical sense. You might want to nest the user inside the guild and any parameters belonging to the user inside that. Something like this...

"superniceguildid": 
{
    "636302212787601408":
    {
        "warns": 3
    },
    "7837439745387549":
    {
        "warns": 1
    }
},

Accessing it then would be as easy as using something like the following:

let guildWarns = warns["superniceguildid"];
let userWarns = guildWarns["636302212787601408"];
let numberOfWarns = userWarns.warns;

you can combine that as well.

let numberOfWarns = warns["superniceguildid"]["636302212787601408"].warns;

Of course, remember that if it doesn't exist it will be undefined.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, and how can I generate that File? The current one I have cant genereate it like in your answer: ``` if(!warns[message.guild.id]) warns[message.guild.id] = { user: wUser.id, warns: 0 }```
You just move the user portion out as if you were accessing the object like I did in the last line... if(!warns[message.guild.id]) warns[message.guild.id][wUser.id] = { warns: 0 } or if(!warns[message.guild.id]) warns[message.guild.id][wUser.id].warns = 0
Got the following Error: if(!warns[message.guild.id]) warns[message.guild.id][wUser.id] = { ^ TypeError: Cannot set property '673322453476442122' of undefined Sorry if im annoying, but im realy new in this xD
@Mr Brickstar I had that backwards, but that is an indication you did not create the guild object first. Try this if(!warns[message.guild.id) { warns[message.guild.id]= {}; } then use warns[message.guild.id][wUser.id] = { warns: 0 };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.