Sans dev
Sans dev

Reputation: 44

Discord blacklist with JSON file

I want to make a blacklist user.id with a json file, when they are using a command, it will send if they're blacklisted or not and if they're blacklisted, it will send the reason

I try this code:

const discord = require('discord.js');
const client = new discord.Client();
const prefix = '+';
const token = 'my bot token';
const blacklist = require('./blacklist.json');

client.on('ready', function () {
  console.log('bot on');
});

client.on('message', function (message) {
  if (message.content.startsWith(prefix + 'check')) {
    if (blacklist.includes(client.user.id)) {
      message.channel.send('you are blacklist, reason: ${reason}');
    } else {
      message.channel.send('heyy');
    }
  }
});

client.login(token);

The JSON file:

{
  "user.id1": "reason",
  "user.id2": "reason"
}

Upvotes: 0

Views: 129

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

If the object keys in your JSON file are user IDs, you can use bracket notation to check if the object contains a key, like this:

if (blacklist[user.id]) {
  // user.id exists on blacklist
}

Also, I don't think you want to check the client.user.id; that's the logged-in client's Discord user. You probably want to check the user who sent the messsage/command, so message.author. The following code should work:

client.on('message', function (message) {
  if (!message.content.startsWith(prefix + 'check')) return;

  if (blacklist[message.author.id])
    return message.channel.send(
      `You are on the blacklist, reason: ${blacklist[message.author.id]}`
    );

  message.channel.send('heyy');
});

Upvotes: 1

Related Questions