-2

So, im making logs for my discord bot and when i restart the program the logs are erasing itself (the logs are in a txt file) Here's the code:

import discord
from discord.ext import commands
from datetime import datetime
from datetime import date
import json

bot = commands.Bot('g4:', intents=discord.Intents.all())
bot.remove_command('help')
privat = open('C:\\Users\\pooki\\OneDrive\\Bureau\\Codes\\GBot 4\\private.json', "r")
jsondata = privat.read()
private = json.loads(jsondata)

@bot.command()
async def ping(a):
    await a.send('Pong!')
    logs = open('C:\\Users\\pooki\\OneDrive\\Bureau\\Codes\\GBot 4\\logs.txt', "w")
    logs.write(f'{date.today().strftime("%d/%m/%Y")} at {datetime.now().strftime("%H:%M:%S")}:@{a.message.author.name} said ping.\n') #type: ignore
    logs.close()

bot.run(token=str(private['token']))

When i looked on the internet it said to put the close function on my file after it writed in it but it still didn't work.

Gacha YTB
  • 17
  • 1
  • 4

1 Answers1

4

It seems like the issue you're facing is related to the way you're opening and closing the log file. The problem is that you're opening the log file in write mode ("w") every time you run the ping command, which truncates the file and erases its contents. To avoid this, you should open the log file in append mode ("a") so that new log entries are added to the end of the file without erasing its existing content.