0

This is my ban command, im trying to export the code to work within a command handler. Iv had numerous issues with this command but eventually i have almost everything working. The code runs perfectly until the point where it should write to .json file (I changed the .json directory to make sure it was being found, and it threw an error so the previous line of code is definitely running, and its finding the .json). Any help would be greatly appreciated, thank you

I have also tried replacing this line

let bannedIDs = require('../../bannedIDs.json').ids || []

with

let file = fs.readFileSync('../../bannedIDs.json')
let bannedIDs = JSON.parse(file).ids || []

still results in no data being written to the .json

const { RichEmbed } = require("discord.js");
const fs = require('fs');

module.exports = {
  config: {
    name: "ban",
    description: "Bans a user from the guild!",
    usage: "!ban",
    category: "moderation",
    accessableby: "Administrators",
    aliases: ["b", "banish", "remove"]
  },
  run: async (bot, message, args) => {

    if (!message.member.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("You do not have permission to perform this command!");

    const user1 = message.mentions.users.first();
    let member = message.mentions.members.first();


    if (member) {

      const member = message.mentions.members.first();

      let reason = args.slice(2).join(' ');
      var user = message.mentions.users.first();

      member.ban({ reason: `${args.slice(2).join(' ')}` }).then(() => {
        let uEmbed = new RichEmbed()
          .setTitle('**' + `Sucessfully Banned ${user1.tag}!` + '**')
          .setThumbnail('https://i.gyazo.com/8988806671312f358509cf0fd69341006.jpg')
          .setImage('https://media3.giphy.com/media/H99r2HtnYs492/giphy.gif?cid=ecf05e47db8ad81dd0dbb6b132bb551add0955f9b92ba021&rid=giphy.gif')
          .setColor(0x320b52)
          .setTimestamp()
          .setFooter('Requested by ' + message.author.tag, 'https://i.gyazo.com/8988806671312f358509cf0fd69341006.jpg');
        message.channel.send(uEmbed);

      }).catch(err => {
        message.channel.send('I was unable to kick the member');
        console.log(err);
      });
    } else {
      const PREFIX = '!';

      let args = message.content.substring(PREFIX.length).split(" ");

      let user = message.mentions.users.first(),
        userID = user ? user.id : args[1];

      if (isNaN(args[1])) return message.channel.send("You need to enter a vlaid @Member or UserID #");

      if (args[1].length <= 17 || args[1].length >= 19) return message.channel.send("UserID # must be 18 Digits");

      if (userID) {
        let bannedIDs = require('../../bannedIDs.json').ids || [];

        if (!bannedIDs.includes(userID)) bannedIDs.push(userID);

        fs.writeFileSync('../../bannedIDs.json', JSON.stringify({ ids: bannedIDs }));
        let reason = args.slice(2).join(' ');

        let uEmbed = new RichEmbed()
          .setTitle('**' + `UserID #${args[1]}\n Will be Banned on Return!` + '**')
          .setThumbnail('https://i.gyazo.com/8988806671312f358509cf0fd69341006.jpg')
          .setImage('https://i.imgur.com/6Sh8csf.gif')
          .setColor(0x320b52)
          .setTimestamp()
          .setFooter('Requested by ' + message.author.tag, 'https://i.gyazo.com/8988806671312f358509cf0fd69341006.jpg');
        message.channel.send(uEmbed);
        let reason1 = args.slice(2).join(' ');
      } else {
        message.channel.send('Error');
      }
    }
  }

};

1 Answer 1

1

require('../../bannedIDs.json').ids || [] I'm guessing why this doesn't work is because if require(...) doesn't exist, you wouldn't be able to access .id, this is a common thing in js which you can usually just do obj && obj.property or with babel obj?.property

This is personally how I would do it

let file = require('../../bannedIDs.json') || { ids: [userID] };
const bannedIDs = file.bannedIDs;

if (!bannedIDs.includes(userID)) bannedIDs.push(userID);

fs.writeFile('../../bannedIDs.json', JSON.stringify(file));

Also you can just do

const collection = await <Guild>.fetchBans();
const ids = collection.map(e => e.user.id).array();

To get a list of banned users or Ids

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

6 Comments

Hi thanks for your time! I have tried implementing your changes but im not seeing an data being written still. Whats more confusing is i have this working perfectly with a 'index.js' with all my other commands
Maybe it's writing it to a diff pathway, happens to me every now and then when I do paths in fs, so I would open up your file browser and check the upper directories
Thanks I will double check paths and get back to you in a moment, ../../ will go 2 folders back correct?
Index.js is located in /bot/ and ban.js command is in /bot/command/moderation/ and bannedIDs.json is same location as index.js
If i move the .json to a different directory i get an error saying it wasnt found, SO it must be finding .json file. The problem cant be related to args because my ban messages displays the correct userID# SO if the userID# is correct and the .json is legitimate file(i have double checked and even made a new one and yes my .json file looks like {"ids":[]} ). It just leaves the check userID# command and the write command? after dealing with this issue il look at reading and removing the value again.
|

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.