str author id to see if that fixes json issues

This commit is contained in:
phixxy 2024-11-07 19:09:28 -08:00
parent 8325118a2d
commit 907c23176c

View file

@ -12,13 +12,9 @@ class MessageXP(BotBaseCog):
@commands.command() @commands.command()
async def stats(self, ctx): async def stats(self, ctx):
author_id = ctx.author.id author_id = str(ctx.author.id)
xp_data = {}
if not os.path.exists(os.path.join(self.data_dir, "xp.json")):
create_xp_file(self)
try: try:
with open(os.path.join(self.data_dir, "xp.json"), "r") as xp_file: xp_data = read_xp_file(self)
xp_data = json.load(xp_file)
if author_id in xp_data: if author_id in xp_data:
await ctx.send(f"You have {xp_data[author_id]} XP") await ctx.send(f"You have {xp_data[author_id]} XP")
else: else:
@ -36,17 +32,11 @@ class MessageXP(BotBaseCog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_message(self, message: discord.Message): async def on_message(self, message: discord.Message):
try: try:
author_id = message.author.id author_id = str(message.author.id)
if message.author.bot: if message.author.bot:
return return
else: else:
xp_data = {} xp_data = read_xp_file(self)
#check if file exists
if not os.path.exists(os.path.join(self.data_dir, "xp.json")):
create_xp_file(self)
with open(os.path.join(self.data_dir, "xp.json"), "r") as xp_file:
xp_data = json.load(xp_file)
if author_id in xp_data: if author_id in xp_data:
xp_data[author_id] += 1 xp_data[author_id] += 1
else: else:
@ -57,14 +47,14 @@ class MessageXP(BotBaseCog):
except Exception as e: except Exception as e:
self.logger.error(f"Error adding XP: {e}") self.logger.error(f"Error adding XP: {e}")
def create_xp_file(self): def read_xp_file(self):
os.makedirs(self.data_dir, exist_ok=True) # Ensure the directory exists
xp_data = {}
try: try:
with open(os.path.join(self.data_dir, "xp.json"), "w") as xp_file: with open(os.path.join(self.data_dir, "xp.json"), "r") as xp_file:
json.dump(xp_data, xp_file) xp_data = json.load(xp_file)
return xp_data
except Exception as e: except Exception as e:
self.logger.error(f"Error creating XP file: {e}") self.logger.error(f"No XP file found. Returning empty json object: {e}")
return {}
async def setup(bot): async def setup(bot):