python - 将消息存储到文本文件 Discord.py

标签 python discord.py

我正在尝试创建将消息写入文本文件的事件。

这是我的代码:

@bot.event
async def on_message(message):
    msg = message
    with open("data.txt", "r+") as n:
        n.write("\n"  +  "<" + str(msg)+ "|" + str(msg.author) + "|" + str(msg.author.id) + str(msg.created_at) + ">")

但是当机器人尝试将消息写入文本文件时,它不会创建新行并写入如下内容:

<<Message id=907694705016442930 channel=<TextChannel id=901027376958406659 name='testing' position=0 nsfw=False news=False category_id=901027376958406657> type=<MessageType.default: 0> author=<Member id=624327590794100779 name='-Cosmos' discriminator='3798' bot=False nick=None guild=<Guild id=901027376958406656 name='Koll' shard_id=None chunked=True member_count=6>> flags=<MessageFlags value=0>>|-Cosmos#3798|6243275907941007792021-11-09 18:14:41.828000>.158000>

最佳答案

如果您想获取消息的实际值,请使用content 属性。您还应该使用a(追加)模式在文件末尾写入(不截断它)

@bot.event
async def on_message(message):
    msg = message
    with open("data.txt", "a") as n:
        n.write("\n" + "<" + msg.content + "|" + str(msg.author) + "|" + str(msg.author.id) + str(msg.created_at) + ">")

关于python - 将消息存储到文本文件 Discord.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69903152/

相关文章:

python - 在给定的时间戳下以python播放音频

python - pandas python 根据模式排序

python - Discord.py 服务器统计命令

discord - 有没有办法让聊天过滤器机器人通过代码检测大写字母? (不和谐.py)

python - Keras Word2Vec 实现

python - selenium webdriver 的 tcp/ip 端口耗尽

python - 如何旋转数据框

python - 如何让 discord 机器人在 x 分钟不活动后离开语音 channel ?

Python、XP 到 Level 转换器

python - 编写递归随机变量的更好方法