From 487d91ba2e9688774f6fea92d14f7317e291735d Mon Sep 17 00:00:00 2001 From: phixxy Date: Tue, 5 Nov 2024 17:36:14 -0800 Subject: [PATCH] added error logging --- cogs/message_xp.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/cogs/message_xp.py b/cogs/message_xp.py index 32f99a7..f94a4e5 100644 --- a/cogs/message_xp.py +++ b/cogs/message_xp.py @@ -29,24 +29,27 @@ class MessageXP(BotBaseCog): @commands.Cog.listener() async def on_message(self, message: discord.Message): - author_id = message.author.id - if author_id == self.bot_id: - return - else: - #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: - xp_data[author_id] += 1 + try: + author_id = message.author.id + if author_id == self.bot_id: + return else: - xp_data[author_id] = 1 + #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"), "w") as xp_file: - json.dump(xp_data, xp_file) + 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: + xp_data[author_id] += 1 + else: + xp_data[author_id] = 1 + + with open(os.path.join(self.data_dir, "xp.json"), "w") as xp_file: + json.dump(xp_data, xp_file) + except Exception as e: + self.logger.error(f"Error adding XP: {e}") def create_xp_file(self): with open(self.data_dir + "xp.json", "w") as xp_file: