Search code examples
pythontextdiscord.pytxt

when my bot run, it clear data file by itself


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


Solution

  • 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.
    ...