【问题标题】:Getting Errors when trying to log Server Chat info in on_message [discord.py]尝试在 on_message [discord.py] 中记录服务器聊天信息时出错
【发布时间】:2021-02-05 04:24:38
【问题描述】:

所以,我正在制作一个服务器机器人,并试图让该机器人在用户发送文件时记录用户消息。

...
with open("messages.txt", "w") as f:
        f.write(f'{message.author.name}: "{message.content}" {time.strftime("%H:%I:%M")} {message.guild.name}\n')
...

它对某些用户有点工作,但是当其他人发送消息时我不断收到错误消息。 "UnicodeEncodeError: 'charmap' codec can't encode character '\u2615' in position 0: character maps to " 有时它只会清除文件。

谢谢!

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    使用 unicode 解析非 ascii 字符串。 ('\u2615' 在 UTF-8 中被解码为 ☕)

    open("messages.txt", "w", encoding='utf-8')
    

    【讨论】:

      猜你喜欢
      • 2015-09-25
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 2011-10-01
      • 2017-01-15
      相关资源
      最近更新 更多