Discord.js未将数据写入.json文件

问题描述 投票:0回答:1

这是我的ban命令,我试图导出代码以在命令处理程序中工作。 iv对此命令有很多问题,但最终我几乎可以正常工作。该代码可以正常运行,直到应将其写入.json文件为止(我更改了.json目录以确保已找到它,并且抛出错误,因此上一行代码肯定正在运行,并且找到了。 json)。任何帮助将不胜感激,谢谢

我也尝试过替换此行

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

with

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

仍然导致没有数据写入.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');
      }
    }
  }

};

discord discord.js args
1个回答
0
投票

require('../../bannedIDs.json').ids || []我猜这为什么不起作用是因为,如果require(...)不存在,您将无法访问.id,这在js中很常见,您通常可以执行obj && obj.property或使用Babel obj?.property

这是我个人的做法

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));
© www.soinside.com 2019 - 2024. All rights reserved.