【问题标题】:Discord.js not writing data to .json fileDiscord.js 未将数据写入 .json 文件
【发布时间】:2020-09-16 23:02:57
【问题描述】:

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

我也试过替换这条线

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

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】:

    require('../../bannedIDs.json').ids || []我猜为什么这不起作用是因为如果require(...)不存在,您将无法访问.id,这是js中常见的事情,您通常可以只需执行obj &amp;&amp; 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));
    

    你也可以这样做

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

    获取被禁止用户或 ID 的列表

    【讨论】:

    • 您好,感谢您的宝贵时间!我已尝试实施您的更改,但我没有看到仍在写入的数据。更令人困惑的是,我的所有其他命令都可以与“index.js”完美配合
    • 也许它正在将其写入差异路径,当我在fs 中执行路径时不时发生在我身上,所以我会打开你的文件浏览器并检查上层目录
    • 谢谢,我会仔细检查路径并稍后回复您,../../ 将返回 2 个文件夹正确吗?
    • Index.js 位于 /bot/,ban.js 命令位于 /bot/command/moderation/,bannedIDs.json 与 index.js 位于同一位置
    • 如果我将 .json 移动到不同的目录,我会收到一条错误消息,说它没有找到,所以它一定是在找到 .json 文件。问题与 args 无关,因为我的禁令消息显示正确的 userID# 所以如果 userID# 是正确的并且 .json 是合法文件(我已经仔细检查,甚至制作了一个新文件,是的,我的 .json 文件看起来像 { “ID”:[]})。它只留下检查用户ID# 命令和写入命令?处理完这个问题后,我会再次查看并删除该值。
    猜你喜欢
    • 2021-04-22
    • 2018-05-26
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2019-10-01
    • 2012-08-31
    相关资源
    最近更新 更多