简体   繁体   中英

How I make a Blacklist with JSON file?

I want make a blacklist with a JSON file. I want put the id in JSON file and when the member join a guild with the bot, the member is banned.

My code:

bot.on("guildMemberAdd", (member) => {
    let blacklist = JSON.parse(fs.readFileSync("./blacklist.json", "utf8"));
        bot.guilds.forEach((guild) => {
          if (!blacklist[member.id]) return
          if(blacklist[member.id].state === true) {
            member.ban()
          }
        })
    })

Json File

{
    "id":"664514598086508605"
}

I would use the .includes() method but it will require some changes to your json file so that it is like this

{
  "123456789123456789",
  "987654321987654321"
}

then your code will become

const blacklist = require('blacklisted.json')

// ...

bot.on("guildMemberAdd", (member) => {
  if (blacklisted.includes(member.id)) {
    member.{ reason: 'Automatic ban, blacklisted user' }
  }
})

this code will automatically ban anyone who is blacklisted from joining a server that the bot is in

using your original code

const blacklist = require('blacklisted.json')

// ...

bot.on("guildMemberAdd", (member) => {
  bot.guilds.forEach((guild) => {
    if (blacklisted.includes(member.id)) {
      member.{ reason: 'Automatic ban, blacklisted user' }
    }
  });
});

This will ban the user from all servers when the user joins one of them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM