I am trying to create a discord bot in python, and do an xp simulator like MEE6 or something. However, when I try and use what I currently have, I get an error. My current code is:
#A ton of imports
def unload_cache(file: str):
return open("cache/" + file, "r").read()
def reload_cache(file: str, new):
cache = open("cache/" + file, "w")
cache.write(str(new))
cache.close()
def add_xp(server_id, user_id, xp: int):
dicts = unload_cache("server_xp.txt")
try:
data = dicts[server_id]
try:
data[user_id] = data[user_id] + xp
dicts[server_id] = data
except KeyError:
data[user_id] = xp
dicts[server_id] = data
except:
dicts[server_id] = {user_id:xp}
reload_cache("server_xp.txt", dicts)
@bot.event
async def on_message(message):
server_id = int(message.server.id)
add_xp(server_id, message.author.id, 1)
print(message.author.name + " gaind 1 xp at " + message.server.id)
To summerize this, it basically opens a file which has the dictionary {ServerID:{Player1ID:xp, Player2ID:xp, etc}, etc}
and adds 1 xp every time someone talks. I have the multiple server ids for multi-server support. For some reason I get this exact error:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:\Users\User\Python\Confusion Bot\bot.py", line 83, in on_message
add_xp(server_id, message.author.id, 1)
File "C:\Users\User\Python\Confusion Bot\bot.py", line 43, in add_xp
dicts[server_id] = {user_id:xp}
IndexError: cannot fit 'int' into an index-sized integer
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\User\Python\Confusion Bot\bot.py", line 35, in add_xp
data = dicts[server_id]
IndexError: cannot fit 'int' into an index-sized integer