【发布时间】:2021-01-20 18:43:41
【问题描述】:
如果我在我的机器上运行我的代码,当我使用我的代码将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!")```
【问题讨论】:
-
Heroku dynos 没有持久文件系统,请参阅 this answer
标签: python-3.x heroku discord.py