我正在尝试创建将消息写入文本文件的事件。
这是我的代码:
@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/