为什么fs.writeFile在GitHub上不起作用?

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

我有一个拥有经济系统的机器人。有一个轮盘赌''!flip'',用户可以打赌他的硬币,它们存储在(../coins.json)中。当我从计算机(节点index.js)启动机器人时,一切正常,机器人本身几秒钟后更改用户的余额,但是当我将其上载到GitHub并运行时,该机器人起初工作正常,余额已保存,但是重新启动后一切都消失了,它没有写入coins.json文件,我该如何解决这个问题?

const Discord = require("discord.js");
let coins = require("../coins.json");
const fs = require('fs');
const prefix = "!";

module.exports.run = async (bot, message, args) => {

  let prefix = "!";

  if (!message.content.startsWith(prefix)) return;

  let random = Math.floor(Math.random() * 100) + 1;

  let bal = parseInt(coins[message.author.id].coins); // Convert user's balance to number type

  if (bal < parseInt(args[0])) return message.channel.send(`${message.author}, Not enough coins!`)

  if (random >= 70) {

  let win = new Discord.MessageEmbed()
  .setDescription(`${message.author}`)
  .addField(`You Won! 🥳`, `Number: ${random}`)
  .setColor("GREEN")
  message.channel.send(win)

    coins[message.author.id] = {
      coins: parseInt(coins[message.author.id].coins) + parseInt(args[0])
    }
    fs.writeFile("../coins.json", JSON.stringify(coins), (err) => {
      if (err) console.log(err)
  })
  };

  if (random < 70) {

    let lose = new Discord.MessageEmbed()
    .setDescription(`${message.author}`)
    .addField(`You Lose! 🥺`, `Number: ${random}`)
    .setColor("#CB2E2E")
    message.channel.send(lose)

    coins[message.author.id] = {
      coins: parseInt(coins[message.author.id].coins) - parseInt(args[0])
    }
    fs.writeFile("../coins.json", JSON.stringify(coins), (err) => {
      if (err) console.log(err)
  })
  }
 };

module.exports.help = {
  name: "flip"
}
node.js discord discord.js
1个回答
0
投票

因为我还无法发表评论,所以我这样问你...您如何从Github运行该项目?由于GitHub不是node.js运行时,我认为您需要将应用程序部署到Heroku或VPS之类的东西。

© www.soinside.com 2019 - 2024. All rights reserved.