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:\Users\quiny\Desktop\sauce')
@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
Use toJSON() Method to make class JSON serializable So we don't need to write custom JSONEncoder. This new toJSON() serializer method will return the JSON representation of the Object. i.e., It will convert custom Python Object to JSON string.
Conclusion # The Python "TypeError: Object of type set is not JSON serializable" occurs when we try to convert a set object to a JSON string. To solve the error, convert the set to a list before serializing it to JSON, e.g. json. dumps(list(my_set)) .
The Python "TypeError: Object of type function is not JSON serializable" occurs when we try to serialize a function to JSON. To solve the error, make sure to call the function and serialize the object that the function returns.
The json module exposes two methods for serializing Python objects into JSON format. dump() will write Python data to a file-like object. We use this when we want to serialize our Python data to an external JSON file. dumps() will write Python data to a string in JSON format.
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 = {}
I had this error and I used the json.dump
the wrong way around:
with open("Jello.json", "w") as json_file:
json.dump(object_to_be_saved, json_file)
I did it the other way around json.dump(json_file, object_to_be_saved)... Thanks Python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With