1

Hi I try to make sure that as soon as my discord bot joins a server it writes the es of the server in the JSON file and then the first channel of the discord server below but it does not work.

client.on("guildCreate", (guild) => { // This event triggers when the bot joins a guild. 
    let rawdata = fs.readFileSync('guilds.json');
    let guilds = JSON.parse(rawdata);
    console.log(`Joined new guild: ${guild.name}`);
    var chx = guild.channels.cache.filter(chx => chx.type === "text").find(x => x.position === 0);
    client.newGuild [chx.guild.id] = {
        MemberAdd: chx.id,
        MemberRemove: chx.id
    }
    fs.writeFile("./guilds.json", JSON.stringify (client.newGuild, null, 4), err => {
        if (err) throw err;
        console.log('Server succefull add')
    })
});

I get this error enter image description here

I try to do this:

{
    "801502579829112852":{
       "MemberAdd":"801514313340289034",
       "MemberRemove":"802125586923257886"
    },
    "802163006698946570":{
       "MemberAdd":"802163007173951521",
       "MemberRemove":"802163007173951521"
    }
 }

Thank you for your help !

6
  • In what way does it not work? What goes wrong? Commented Jan 24, 2021 at 2:53
  • @VeryGoodDog look the edit Commented Jan 24, 2021 at 9:30
  • What gives console.log(chx.id) ? because my guess is that var chx = guild.channels and not guild so maybe chx.id is undefined Commented Jan 24, 2021 at 18:08
  • @nazimboudeffa this gives me the server id Commented Jan 24, 2021 at 19:11
  • send me the initial content of your guilds.json Commented Jan 25, 2021 at 7:23

1 Answer 1

1

Replace your code with this one :

    guilds.newGuild [chx.guild.id] = {
        MemberAdd: chx.id,
        MemberRemove: chx.id
    }
    fs.writeFile("./guilds.json", JSON.stringify (guilds, null, 2), err => {
        if (err) throw err;
        console.log('Server successfully add')
    })

plus replace your guilds.json with :

{"newGuild":{}}
Sign up to request clarification or add additional context in comments.

Comments

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.