重置discord bot后python中的不变/常量变量?

时间:2021-01-30 23:33:34

标签: python pycharm discord discord.py

我想知道 Python 中是否有一些函数或模块可以让我创建在重新启动 Discord 机器人时不会变回 0 的变量?我有很多小游戏,你可以通过完成它们获得积分,我现在的解决方案只是在文件中输入用户 ID,然后如果用户想查看他们有多少积分,它只计算 ID 的数量文件。但它看起来很糟糕,所以如果有其他解决方案;例如,如果有一个变量 score = 0,那么我可以只写 score += 1,并且在重新启动机器人后它不会改变。

Here is how it looks in file

代码只是例如它看起来有多糟糕:

# Some code
if score_1 > score_2:   
    await msg.send(msg.author.mention + " **You have won**. You have earned 3 points.")
    tabulka = open("tabulka.txt", "a")
    tabulka.write("\n" + str(msg.author.id))
    tabulka.write("\n" + str(msg.author.id))
    tabulka.write("\n" + str(msg.author.id))
    tabulka.close()
    on_command = 0

@bot.command()
async def me(msg):
    if msg.channel.id != channel:
        return
    message = str(msg.message.content)
    id = msg.author.id
    tabulka = open("tabulka.txt", "r")
    data = tabulka.read()
    result = str(data.count(str(id)))
    await msg.send(msg.author.mention + " Your score is: " + "**" + result + "**")
    tabulka.close()

0 个答案:

没有答案