首页 > 解决方案 > 为什么 Heroku 不保存我的代码写入文件的更改?

问题描述

如果我在我的机器上运行我的代码,当我使用我的代码将内部的数字增加一时,我使用我的代码写入文件的更改count.txt将成功写入count.txt. 但是,当我的应用程序在 heroku 上运行时,对文件的更改既不会进行也不会保存。我打开终端,执行 bash,cat count.txt并在我的服务器中的用户使用机器人时读取内容count.txt:文件没有更改。我手动输入文件的数字仍然存在,没有改变。发生这种情况是否有原因,是否有解决方案?

import discord
from discord.ext import commands
import re


class Counting(commands.Cog):
    """ Users take turns incrementing a number. """

    def __init__(self, client):
        self.client = client
        self.count_channel = 761981805862846525
        self.num_regex = re.compile(r'(^\d+)')
        self.cum_num = None
        self.filename = "count.txt"

    @commands.Cog.listener()
    async def on_message(self, message):
        with open(self.filename) as f:
            read_data = f.read()
            self.cum_num = int(read_data)

        if message.channel.id == self.count_channel:
            try:
                match_obj = self.num_regex.search(message.content)
                if int(match_obj.group(1)) != self.cum_num + 1:
                    await message.author.send(f'Your message must start with **{self.cum_num + 1}**!')
                    await message.delete()
                else:
                    self.cum_num += 1
                    with open(self.filename, 'w') as f:
                        f.write(str(self.cum_num))
            except AttributeError:
                if message.author.id != self.client.user.id:
                    await message.delete()
                    await message.author.send(f"Your message must be a number!")```

标签: python-3.xherokudiscord.py

解决方案


Heroku 不支持这样的事情。请改用在线数据库。(例如,我使用的是MongoDB)。

祝你好运!


推荐阅读