(Discord.py)创建一个记录器,以保存机器人所在的对话 [英] (Discord.py) Make a logger which saves conversations the bot is in

查看:80
本文介绍了(Discord.py)创建一个记录器,以保存机器人所在的对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在discord.py中创建一个记录器机器人,将该对话器保存到文本文件中.

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

因此,例如,僵尸程序将所有聊天记录都保存在名为"chatlogs"的文件夹中,并在不一致的服务器A 中,每次有人说出该僵尸程序可以看到的内容时,该僵尸程序都会将其记录在名为 ServerA.txt ,并且当服务器B 添加我的漫游器时,它将生成一个名为 ServerB.txt 的文件,并保存所有服务器B >在那里的对话.

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.

推荐答案

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")

这篇关于(Discord.py)创建一个记录器,以保存机器人所在的对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆