<legend id='ntTHE'><style id='ntTHE'><dir id='ntTHE'><q id='ntTHE'></q></dir></style></legend><tfoot id='ntTHE'></tfoot>

      <small id='ntTHE'></small><noframes id='ntTHE'>

      1. <i id='ntTHE'><tr id='ntTHE'><dt id='ntTHE'><q id='ntTHE'><span id='ntTHE'><b id='ntTHE'><form id='ntTHE'><ins id='ntTHE'></ins><ul id='ntTHE'></ul><sub id='ntTHE'></sub></form><legend id='ntTHE'></legend><bdo id='ntTHE'><pre id='ntTHE'><center id='ntTHE'></center></pre></bdo></b><th id='ntTHE'></th></span></q></dt></tr></i><div id='ntTHE'><tfoot id='ntTHE'></tfoot><dl id='ntTHE'><fieldset id='ntTHE'></fieldset></dl></div>
          <bdo id='ntTHE'></bdo><ul id='ntTHE'></ul>
      2. TypeError:TextIOWrapper 类型的对象不是 JSON 可序列化

        时间:2023-10-11

          <small id='BEYXc'></small><noframes id='BEYXc'>

          • <bdo id='BEYXc'></bdo><ul id='BEYXc'></ul>
              <tbody id='BEYXc'></tbody>

            <legend id='BEYXc'><style id='BEYXc'><dir id='BEYXc'><q id='BEYXc'></q></dir></style></legend>

                <i id='BEYXc'><tr id='BEYXc'><dt id='BEYXc'><q id='BEYXc'><span id='BEYXc'><b id='BEYXc'><form id='BEYXc'><ins id='BEYXc'></ins><ul id='BEYXc'></ul><sub id='BEYXc'></sub></form><legend id='BEYXc'></legend><bdo id='BEYXc'><pre id='BEYXc'><center id='BEYXc'></center></pre></bdo></b><th id='BEYXc'></th></span></q></dt></tr></i><div id='BEYXc'><tfoot id='BEYXc'></tfoot><dl id='BEYXc'><fieldset id='BEYXc'></fieldset></dl></div>

                1. <tfoot id='BEYXc'></tfoot>
                  本文介绍了TypeError:TextIOWrapper 类型的对象不是 JSON 可序列化的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如果代码能够正常工作,那么每当有人在聊天中键入内容时,他们就会获得 5 经验,并且该信息会被放入 .json 文件中,但当有人在聊天中键入内容时会发生这种情况聊天它给了我这个错误.

                  If the code was to work properly then whenever someone types something in the chat they get 5 experience and that information gets put into a .json file, but instead what happens is whenever someone types something into the chat it gives me this error.

                  on_message users = json.dumps(f) 
                  TypeError: Object of type TextIOWrapper is not JSON serializable
                  

                  这是我正在使用的代码:

                  Here is the code that I am using:

                  import discord
                  from discord.ext import commands
                  from discord.ext.commands import Bot
                  import asyncio
                  import json
                  from json import dumps, loads, JSONEncoder, JSONDecoder
                  import os
                  
                  client = commands.Bot(command_prefix='^')
                  os.chdir(r'C:UsersquinyDesktopsauce')
                  
                  @client.event
                  async def on_ready():
                      print ("Ready when you are xd")
                      print ("I am running on " + client.user.name)
                      print ("With the ID: " + client.user.id)
                  
                  @client.event
                  async def on_member_join(member):
                      with open('users.json', 'r') as f: 
                          users = json.dumps(f)
                  
                      await update_data(users, member)
                  
                      with open('users.json', 'w') as f:
                          json.loads("users, f")
                  
                  @client.event
                  async def on_message(message):
                      with open('users.json', 'r') as f:
                          users = json.dumps(f)
                  
                      await update_data(users, message.author)
                      await add_experience(users, message.author, 5)
                      await level_up(users, message.author, message.channel)
                  
                      with open('users.json', 'w') as f:
                          json.loads("users, f")
                  
                  async def update_data(users, user):
                      if not user.id in users:
                          users[user.id] = {}
                          users[user.id]['experience'] = 0
                          users[user.id]['level'] = 1
                  
                  async def add_experience(users, user, exp):
                      users[user.id]['experience'] += exp
                  
                  async def level_up(users, user, channel):
                      experience = users[user.id]['experience']
                      lvl_start = users[user.id]['level']
                      lvl_end = int(experience ** (1/4))
                  
                      if lvl_start < lvl_end:
                          await client.send_message(channel, '{} has achieved a slightly higher 
                  level of {}, yay'.format(user.mention, lvl_end))
                          users[user.id]['level'] = lvl_end
                  

                  推荐答案

                  你有你的 加载dumps 向后,你应该使用 加载转储 代替(s 后缀表示这些函数适用于字符串.).load 从文件 dump 到文件

                  You have your loads and dumps backwards, and you should be using load and dump instead (the s suffix means those functions work on strings.). load from a file, dump to a file

                  users = {}
                  
                  @client.event
                  async def on_message(message):
                      # No need to load the dictionary, our copy is the most correct
                      await update_data(users, message.author)
                      await add_experience(users, message.author, 5)
                      await level_up(users, message.author, message.channel)
                      with open('users.json', 'w') as f:
                          json.dump(users, f)
                  
                  @client.event
                  async def on_ready():
                      print ("Ready when you are xd")
                      print ("I am running on " + client.user.name)
                      print ("With the ID: " + client.user.id)
                      # Load the json just once, when the bot starts
                      global users
                      with open('users.json') as f:
                          try:
                              users = json.load(f)
                          except:
                              users = {}
                  

                  这篇关于TypeError:TextIOWrapper 类型的对象不是 JSON 可序列化的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 discord.py 让机器人响应图像 下一篇:Discord.py 中的延迟命令

                  相关文章

                  最新文章

                  <small id='oxwJE'></small><noframes id='oxwJE'>

                  1. <legend id='oxwJE'><style id='oxwJE'><dir id='oxwJE'><q id='oxwJE'></q></dir></style></legend>

                  2. <tfoot id='oxwJE'></tfoot>
                    • <bdo id='oxwJE'></bdo><ul id='oxwJE'></ul>

                    1. <i id='oxwJE'><tr id='oxwJE'><dt id='oxwJE'><q id='oxwJE'><span id='oxwJE'><b id='oxwJE'><form id='oxwJE'><ins id='oxwJE'></ins><ul id='oxwJE'></ul><sub id='oxwJE'></sub></form><legend id='oxwJE'></legend><bdo id='oxwJE'><pre id='oxwJE'><center id='oxwJE'></center></pre></bdo></b><th id='oxwJE'></th></span></q></dt></tr></i><div id='oxwJE'><tfoot id='oxwJE'></tfoot><dl id='oxwJE'><fieldset id='oxwJE'></fieldset></dl></div>