Alex Wollan
Alex Wollan

Reputation: 49

(Discord.py) Make a logger which saves conversations the bot is in

how do I make a a logger bot in discord.py which saves conversation into a text file.

So for example the bot saves all chats in a folder called "chatlogs" and in discord Server A every time someone says something that the bot can see, the bot logs it in a file called ServerA.txt and when Server B adds my bot, it generates a file called ServerB.txt and saves all Server B conversations in there.

Upvotes: 0

Views: 4660

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 61052

In an on_message event, open the file in append mode and write the latest message.

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_message(message):
    guild = message.guild
    if guild:
        path = "chatlogs/{}.txt".format(guild.id)  
        with open(path, 'a+') as f:
            print("{0.timestamp} : {0.author.name} : {0.content}".format(message), file=f)
    await bot.process_commands(message)

bot.run("token")

Upvotes: 1

Related Questions