Home > Net >  I want create a command logs in txt for my discord server using python
I want create a command logs in txt for my discord server using python

Time:07-18

I want to create txt logs for my server, so I wrote a code that allows to do it except that it writes the line of logs just above the old one while I want it to be written below

my code :

@bot.event
async def on_command(ctx):
    with open('logs.txt', 'w ') as f:
        command = str(ctx.command)
        user = str(ctx.message.author)
        now = datetime.now()
        time = now.strftime("[%H:%M:%S]")
        date = now.strftime("[%d-%m-%Y]")
        f.write(f"{user} a éxécuter la command {command} a {time} le {date}\n")

Th result :

The Result :

CodePudding user response:

This should work for you :

@bot.event
async def on_command(ctx):
    with open('logs.txt', 'r') as f:
        content = f.read()
        writefile = open('logs.txt','w')
        
        command = str(ctx.command)
        user = str(ctx.message.author)
        now = datetime.now()

        time = now.strftime("[%H:%M:%S]")
        date = now.strftime("[%d-%m-%Y]")
        
        newcontent = f"{content}{user} a éxécuter la command {command} a {time} le {date}\n"

        writefile.write(newcontent)
  • Related