当我重新运行我的 bot (discord.py) 时,我如何制作一个不重置的计数器
How can I make a counter which doesn't reset when i rerun my bot (discord.py)
我已经有了一个计数器,但是当我重新运行机器人时,计数器从 0 开始,我怎样才能让它变得更好?代码:
@bot.event
async def on_message(message):
global counter
counter += 1
print(message.content)
您应该可以从单独的文件中读取它。之后当然可以读写了。
您可以使用与令牌相同的文件。
您想将计数器保存在文本文件或数据库中
文本文件:
# Gets the counter
try:
open("counter.txt", "x").close()
counter = 0
except:
with open("counter.txt", "r") as file:
counter = int(file.readlines()[0])
# Updates the counter
with open("counter.txt", "w") as file:
file.write(str(counter))
# ...or if you're going to be updating it a lot
file = open("counter.txt", "w")
file.write(str(counter))
file.close() # Once you're completley done
我已经有了一个计数器,但是当我重新运行机器人时,计数器从 0 开始,我怎样才能让它变得更好?代码:
@bot.event
async def on_message(message):
global counter
counter += 1
print(message.content)
您应该可以从单独的文件中读取它。之后当然可以读写了。
您可以使用与令牌相同的文件。
您想将计数器保存在文本文件或数据库中
文本文件:
# Gets the counter
try:
open("counter.txt", "x").close()
counter = 0
except:
with open("counter.txt", "r") as file:
counter = int(file.readlines()[0])
# Updates the counter
with open("counter.txt", "w") as file:
file.write(str(counter))
# ...or if you're going to be updating it a lot
file = open("counter.txt", "w")
file.write(str(counter))
file.close() # Once you're completley done