当我的机器人运行时,它会自行清除数据文件。

huangapple go评论68阅读模式
英文:

when my bot run, it clear data file by itself

问题

I don't want to bot clear data txt file.

英文:

I'am trying to Write a bot that check fivem(It Just a Game) log and if item name and item count equals to data txt file then do something.
But when on_message function start working by send a message in discord server, data file be cleared.
My Code:

@client.event
    async def on_message(message):
        ms_id = message.channel.id
        n_log = message.content.split()
        print(f"[INFO] : {message.content.split()}")
        Craft_File_Read = open("Craft_Request.txt", "r")
        Craft_File_Read_Data = Craft_File_Read.read()
        Craft_item_File_Write = open("Craft_Request.txt", "w")
        split_craft_data = Craft_File_Read_Data.split(",")
        if ms_id == 1116113589456085156:
            print(n_log[10])
            if split_craft_data[1] == "Pistol":
                benzin_value = 10 * int(split_craft_data[2])
                rafin_value = 10 * int(split_craft_data[2])
                essence_value = 10 * int(split_craft_data[2])
                iron_piece_value = 5 * int(split_craft_data[2])
                black_money_value = 60000 * int(split_craft_data[2])
                if split_craft_data[0] == n_log[10]:
                    print("00000000000000")
                    print(n_log[1])
                    if n_log[1] == 'Gozashtan':
                        print("11111111111")
                        del n_log[0]
                        del n_log[len(n_log) - 2]
                        split_n_log = n_log[4].split("(")
                        split_n_log_end = split_n_log[1].replace(")", "")
                        if split_n_log[0] == 'petrol':
                            if int(split_n_log_end) == benzin_value:
                                check_item_pistol[0] = True
                                await message.reply("Sabt Shod!")
                                if all(check_item_pistol) == True:
                                    Item_Check = Craft_File_Read_Data.replace(split_craft_data[3], 'True')
                                    Craft_item_File_Write.write(Item_Check)

I Don't Want To Bot Clear data txt file

答案1

得分: 0

从我理解的情况来看,您想要追加内容到文件而不是覆盖它。在这种情况下,您应该以追加模式("a")而不是写入模式("w")打开文件。

Craft_item_File_Write = open("Craft_Request.txt", "a")
#  保持追加到文件而不是覆盖它。
英文:

From what I am understanding, you want to append to the file and not overwrite it. In that case you should open the file with appending mode ("a") and not writing mode ("w").

...
Craft_item_File_Write = open("Craft_Request.txt", "a")
#  Keeps appending to the file instead of overwriting it.
...

huangapple
  • 本文由 发表于 2023年6月27日 18:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定