From c0a5dda5d26e6e9f56b27ab5c9a1c81f4a388656 Mon Sep 17 00:00:00 2001 From: phixxy Date: Tue, 5 Nov 2024 17:23:21 -0800 Subject: [PATCH] start replacement of highscores.py --- cogs/message_xp.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 cogs/message_xp.py diff --git a/cogs/message_xp.py b/cogs/message_xp.py new file mode 100644 index 0000000..7405366 --- /dev/null +++ b/cogs/message_xp.py @@ -0,0 +1,57 @@ +from discord.ext import commands +import json +import os +from cogs.base_cog.bot_base_cog import BotBaseCog + +class MessageXP(BotBaseCog): + + def __init__(self, bot): + super().__init__(bot) + self.setup(__class__.__name__) + + @commands.command() + async def stats(self, ctx): + author_id = ctx.author.id + if not os.path.exists(self.data_dir + "xp.json"): + create_xp_file(self) + try: + with open(self.data_dir + "xp.json", "r") as xp_file: + xp_data = json.load(xp_file) + xp_file.close() + if author_id in xp_data: + await ctx.send(f"You have {xp_data[author_id]} XP") + else: + await ctx.send("You have 0 XP") + except: + await ctx.send("Error getting XP") + + + @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(self.data_dir + "xp.json"): + create_xp_file(self) + with open(self.data_dir + "xp.json", "r") as xp_file: + xp_data = json.load(xp_file) + xp_file.close() + if author_id in xp_data: + xp_data[author_id] += 1 + else: + xp_data[author_id] = 1 + with open(self.data_dir + "xp.json", "w") as xp_file: + json.dump(xp_data, xp_file) + xp_file.close() + +def create_xp_file(self): + with open(self.data_dir + "xp.json", "w") as xp_file: + xp_data = {} + json.dump(xp_data, xp_file) + xp_file.close() + + +async def setup(bot): + await bot.add_cog(MessageXP(bot)) \ No newline at end of file